import { apiRequest } from './api';

export interface StudentProfile {
  userId?: string;
  name?: string | null;
  phone?: string | null;
  avatarPreset?: 'male' | 'female';
  avatar?: {
    preset?: 'male' | 'female';
    displayUrl?: string | null;
  };
  score?: number;
  target?: {
    regionId?: string | null;
    regionName?: string | null;
    schoolId?: string | null;
    schoolName?: string | null;
    majorId?: string | null;
    majorName?: string | null;
  };
  membership?: {
    isSvip?: boolean;
    entitlement?: unknown;
  };
  stats?: Record<string, unknown>;
  recentPractices?: unknown[];
}

export interface PointActivityTask {
  id: string;
  code?: string;
  title?: string;
  description?: string | null;
  taskType?: string;
  rewardPoints?: number;
  claimLimitPerUser?: number;
  periodType?: 'once' | 'daily' | 'weekly' | 'monthly' | 'unlimited';
  validFrom?: string | null;
  validTo?: string | null;
  status?: string;
  order?: number;
  metadata?: Record<string, unknown>;
  claimCount?: number;
  currentPeriodClaimCount?: number;
  currentPeriodClaimId?: string | null;
  claimedInCurrentPeriod?: boolean;
  remainingClaims?: number;
  lastClaimedAt?: string | null;
}

export interface PointExchangeItem {
  id: string;
  code?: string;
  title?: string;
  description?: string | null;
  costPoints?: number;
  itemType?: 'coupon' | 'manual' | 'asset' | 'custom';
  couponId?: string | null;
  assetId?: string | null;
  stockTotal?: number | null;
  stockUsed?: number;
  stockRemaining?: number | null;
  perUserLimit?: number;
  validFrom?: string | null;
  validTo?: string | null;
  status?: string;
  order?: number;
  metadata?: Record<string, unknown>;
  redeemedCount?: number;
  remainingUserRedemptions?: number;
  lastExchangedAt?: string | null;
}

export interface UserNotificationItem {
  id: string;
  notificationType?: string;
  status?: 'unread' | 'read' | 'dismissed' | 'archived';
  severity?: 'info' | 'success' | 'warning' | 'error';
  title?: string;
  message?: string;
  actionLabel?: string | null;
  actionPath?: string | null;
  sourceType?: string | null;
  sourceId?: string | null;
  metadata?: Record<string, unknown>;
  readAt?: string | null;
  createdAt?: string;
  updatedAt?: string;
}

export async function loadProfile() {
  return apiRequest<{ item?: StudentProfile }>('/api/profile/me');
}

export async function updateProfile(input: {
  name?: string;
  avatarPreset?: 'male' | 'female';
  regionId?: string;
  selectedSchoolId?: string;
  selectedMajorId?: string;
}) {
  return apiRequest<{ item?: Record<string, unknown> }>('/api/profile/me', {
    method: 'PATCH',
    body: input,
  });
}

export async function checkIn() {
  return apiRequest<{ item?: Record<string, unknown> }>('/api/profile/check-in', { method: 'POST' });
}

export async function loadScoreEvents(limit = 50) {
  return apiRequest<{ items?: Record<string, unknown>[] }>('/api/profile/score-events', { query: { limit } });
}

export async function loadActivityTasks(query: { taskType?: string; limit?: number } = {}) {
  return apiRequest<{ items?: PointActivityTask[] }>('/api/profile/activity-tasks', {
    query: { ...query, limit: query.limit || 100 },
  });
}

export async function claimActivityTask(input: {
  taskId?: string;
  code?: string;
  sourceType?: string;
  sourceId?: string;
  idempotencyKey?: string;
  metadata?: Record<string, unknown>;
}) {
  return apiRequest<{ item?: Record<string, unknown> }>('/api/profile/activity-tasks/claim', {
    method: 'POST',
    body: input,
  });
}

export async function loadExchangeItems(limit = 100) {
  return apiRequest<{ items?: PointExchangeItem[] }>('/api/profile/exchange-items', { query: { limit } });
}

export async function redeemExchangeItem(input: {
  itemId?: string;
  code?: string;
  idempotencyKey?: string;
  metadata?: Record<string, unknown>;
}) {
  return apiRequest<{ item?: Record<string, unknown> }>('/api/profile/exchange-items/redeem', {
    method: 'POST',
    body: input,
  });
}

export async function loadNotifications(query: {
  status?: UserNotificationItem['status'];
  notificationType?: string;
  limit?: number;
} = {}) {
  return apiRequest<{ items?: UserNotificationItem[]; summary?: Record<string, number> }>('/api/profile/notifications', {
    query: { ...query, limit: query.limit || 50 },
  });
}

export async function updateNotificationStatus(input: {
  notificationIds: string[];
  status: 'read' | 'dismissed' | 'archived';
}) {
  return apiRequest<{ item?: Record<string, unknown> }>('/api/profile/notifications/status', {
    method: 'POST',
    body: input,
  });
}

export async function loadBadges() {
  return apiRequest<{ items?: Record<string, unknown>[] }>('/api/profile/badges');
}

export async function loadExamCountdowns() {
  return apiRequest<{ items?: Record<string, unknown>[] }>('/api/profile/exam-countdowns');
}

export async function loadFeedbacks(limit = 20) {
  return apiRequest<{ items?: Record<string, unknown>[] }>('/api/profile/feedbacks', { query: { limit } });
}

export async function submitFeedback(input: {
  questionId?: string;
  type?: 'question_error' | 'content_error' | 'video_error' | 'asset_error' | 'system_bug' | 'suggestion' | 'other';
  title?: string;
  description: string;
  priority?: 'low' | 'normal' | 'high' | 'urgent';
}) {
  return apiRequest<{ item?: Record<string, unknown> }>('/api/profile/feedbacks', {
    method: 'POST',
    body: input,
  });
}
