import { http } from './index';

// ==================== 类型定义 ====================

export interface TenantQueryParams {
  page?: number;
  pageSize?: number;
  keyword?: string;
  status?: string;
  packageType?: string;
}

export interface Tenant {
  id: number;
  name: string;
  loginAccount: string;
  address?: string;
  contactPerson?: string;
  contactPhone?: string;
  logoUrl?: string;
  packageType: string;
  teacherQuota: number;
  studentQuota: number;
  storageQuota?: number;
  teacherCount: number;
  studentCount: number;
  storageUsed?: number;
  startDate: string;
  expireDate: string;
  status: string;
  createdAt: string;
  updatedAt?: string;
}

export interface TenantDetail extends Tenant {
  teachers?: Array<{
    id: number;
    name: string;
    phone: string;
    email?: string;
    status: string;
    lessonCount: number;
  }>;
  students?: Array<{
    id: number;
    name: string;
    classId: number;
    gender?: string;
    readingCount: number;
  }>;
  classes?: Array<{
    id: number;
    name: string;
    grade: string;
    studentCount: number;
  }>;
  _count?: {
    teachers: number;
    students: number;
    classes: number;
    lessons: number;
  };
}

export interface CreateTenantDto {
  name: string;
  loginAccount: string;
  password?: string;
  address?: string;
  contactPerson?: string;
  contactPhone?: string;
  packageType?: string;
  teacherQuota?: number;
  studentQuota?: number;
  startDate?: string;
  expireDate?: string;
}

export interface UpdateTenantDto {
  name?: string;
  address?: string;
  contactPerson?: string;
  contactPhone?: string;
  packageType?: string;
  teacherQuota?: number;
  studentQuota?: number;
  startDate?: string;
  expireDate?: string;
  status?: string;
}

export interface UpdateTenantQuotaDto {
  packageType?: string;
  teacherQuota?: number;
  studentQuota?: number;
}

export interface AdminStats {
  tenantCount: number;
  activeTenantCount: number;
  courseCount: number;
  publishedCourseCount: number;
  studentCount: number;
  teacherCount: number;
  lessonCount: number;
  monthlyLessons: number;
}

export interface TrendData {
  month: string;
  tenantCount: number;
  lessonCount: number;
  studentCount: number;
}

export interface ActiveTenant {
  id: number;
  name: string;
  lessonCount: number;
  teacherCount: number;
  studentCount: number;
}

export interface PopularCourse {
  id: number;
  name: string;
  usageCount: number;
  teacherCount: number;
}

export interface AdminSettings {
  // Basic settings
  systemName?: string;
  systemDesc?: string;
  contactPhone?: string;
  contactEmail?: string;
  systemLogo?: string;

  // Security settings
  passwordStrength?: string;
  maxLoginAttempts?: number;
  tokenExpire?: string;
  forceHttps?: boolean;

  // Notification settings
  emailEnabled?: boolean;
  smtpHost?: string;
  smtpPort?: number;
  smtpUser?: string;
  smtpPassword?: string;
  fromEmail?: string;
  smsEnabled?: boolean;

  // Storage settings
  storageType?: string;
  maxFileSize?: number;
  allowedTypes?: string;

  // Tenant defaults
  defaultTeacherQuota?: number;
  defaultStudentQuota?: number;
  enableAutoExpire?: boolean;
  notifyBeforeDays?: number;
}

// ==================== 租户管理 ====================

export const getTenants = (params: TenantQueryParams) =>
  http.get<{ items: Tenant[]; total: number; page: number; pageSize: number; totalPages: number }>(
    '/admin/tenants',
    { params }
  );

export const getTenant = (id: number) =>
  http.get<TenantDetail>(`/admin/tenants/${id}`);

export const createTenant = (data: CreateTenantDto) =>
  http.post<Tenant & { tempPassword: string }>('/admin/tenants', data);

export const updateTenant = (id: number, data: UpdateTenantDto) =>
  http.put<Tenant>(`/admin/tenants/${id}`, data);

export const updateTenantQuota = (id: number, data: UpdateTenantQuotaDto) =>
  http.put<Tenant>(`/admin/tenants/${id}/quota`, data);

export const updateTenantStatus = (id: number, status: string) =>
  http.put<{ id: number; name: string; status: string }>(`/admin/tenants/${id}/status`, { status });

export const resetTenantPassword = (id: number) =>
  http.post<{ tempPassword: string }>(`/admin/tenants/${id}/reset-password`);

export const deleteTenant = (id: number) =>
  http.delete<{ success: boolean }>(`/admin/tenants/${id}`);

// ==================== 统计数据 ====================

export const getAdminStats = () =>
  http.get<AdminStats>('/admin/stats');

export const getTrendData = () =>
  http.get<TrendData[]>('/admin/stats/trend');

export const getActiveTenants = (limit?: number) =>
  http.get<ActiveTenant[]>('/admin/stats/tenants/active', { params: { limit } });

export const getPopularCourses = (limit?: number) =>
  http.get<PopularCourse[]>('/admin/stats/courses/popular', { params: { limit } });

// ==================== 系统设置 ====================

export const getAdminSettings = () =>
  http.get<AdminSettings>('/admin/settings');

export const updateAdminSettings = (data: Record<string, any>) =>
  http.put<AdminSettings>('/admin/settings', data);
