import { apiClient } from './api-client';

// ── Types ──

export interface DashboardStats {
  totalUsers: number;
  totalListings: number;
  activeListings: number;
  pendingModerationCount: number;
  totalAgents: number;
  verifiedAgents: number;
  totalTransactions: number;
  newUsersLast30Days: number;
  newListingsLast30Days: number;
}

export interface RevenueStatsItem {
  period: string;
  totalRevenue: number;
  subscriptionRevenue: number;
  listingFeeRevenue: number;
  featuredListingRevenue: number;
  transactionCount: number;
}

export interface ModerationQueueItem {
  listingId: string;
  propertyTitle: string;
  propertyType: string;
  transactionType: string;
  priceVND: number;
  sellerName: string;
  sellerId: string;
  moderationScore: number | null;
  createdAt: string;
}

export interface PaginatedResult<T> {
  data: T[];
  total: number;
  page: number;
  limit: number;
  totalPages: number;
}

export interface UserListItem {
  id: string;
  email: string | null;
  phone: string;
  fullName: string;
  role: string;
  kycStatus: string;
  isActive: boolean;
  createdAt: string;
}

export interface UserDetail {
  id: string;
  email: string | null;
  phone: string;
  fullName: string;
  avatarUrl: string | null;
  role: string;
  kycStatus: string;
  kycData: unknown;
  isActive: boolean;
  createdAt: string;
  updatedAt: string;
  listingsCount: number;
  activeListingsCount: number;
  transactionsCount: number;
  subscription: {
    planTier: string;
    status: string;
    currentPeriodEnd: string;
  } | null;
  recentActivity: Array<{
    type: string;
    description: string;
    createdAt: string;
  }>;
}

export interface AiSettings {
  apiUrl: string;
  apiKeyMasked: string | null;
  model: string;
  hasApiKey: boolean;
  updatedAt: string | null;
}

export interface UpdateAiSettingsPayload {
  apiUrl?: string;
  apiKey?: string;
  model?: string;
}

export interface KycQueueItem {
  userId: string;
  fullName: string;
  email: string | null;
  phone: string;
  role: string;
  kycStatus: string;
  kycData: unknown;
  createdAt: string;
}

export interface AuditLogItem {
  id: string;
  actorId: string;
  actorName: string;
  actorRole: string;
  action: string;
  module: string;
  targetId: string | null;
  targetType: string | null;
  severity: 'info' | 'warning' | 'critical';
  before: unknown;
  after: unknown;
  ipAddress: string | null;
  createdAt: string;
}

// ── API ──

export const adminApi = {
  // Dashboard
  getDashboardStats: () =>
    apiClient.get<DashboardStats>('/admin/dashboard'),

  getRevenueStats: (startDate: string, endDate: string, groupBy: 'day' | 'month' = 'month') =>
    apiClient.get<RevenueStatsItem[]>(
      `/admin/revenue?startDate=${startDate}&endDate=${endDate}&groupBy=${groupBy}`,
    ),

  // Moderation
  getModerationQueue: (page = 1, limit = 20) =>
    apiClient.get<PaginatedResult<ModerationQueueItem>>(
      `/admin/moderation?page=${page}&limit=${limit}`,
    ),

  approveListing: (listingId: string, moderationNotes?: string) =>
    apiClient.post<{ success: boolean }>('/admin/moderation/approve', {
      listingId,
      moderationNotes,
    }),

  rejectListing: (listingId: string, reason: string) =>
    apiClient.post<{ success: boolean }>('/admin/moderation/reject', {
      listingId,
      reason,
    }),

  bulkModerate: (listingIds: string[], action: 'approve' | 'reject', reason?: string) =>
    apiClient.post<{ success: boolean }>('/admin/moderation/bulk', {
      listingIds,
      action,
      reason,
    }),

  // Users
  getUsers: (params: { page?: number; limit?: number; role?: string; isActive?: boolean; search?: string } = {}) => {
    const query = new URLSearchParams();
    if (params.page) query.set('page', String(params.page));
    if (params.limit) query.set('limit', String(params.limit));
    if (params.role) query.set('role', params.role);
    if (params.isActive !== undefined) query.set('isActive', String(params.isActive));
    if (params.search) query.set('search', params.search);
    return apiClient.get<PaginatedResult<UserListItem>>(
      `/admin/users?${query.toString()}`,
    );
  },

  getUserDetail: (userId: string) =>
    apiClient.get<UserDetail>(`/admin/users/${userId}`),

  updateUserStatus: (userId: string, isActive: boolean, reason?: string) =>
    apiClient.post<{ success: boolean }>('/admin/users/status', {
      userId,
      isActive,
      reason,
    }),

  banUser: (userId: string, reason: string, unban = false) =>
    apiClient.post<{ success: boolean }>('/admin/users/ban', {
      userId,
      reason,
      unban,
    }),

  // KYC
  getKycQueue: (page = 1, limit = 20) =>
    apiClient.get<PaginatedResult<KycQueueItem>>(
      `/admin/kyc?page=${page}&limit=${limit}`,
    ),

  approveKyc: (userId: string, notes?: string) =>
    apiClient.post<{ success: boolean }>('/admin/kyc/approve', {
      userId,
      notes,
    }),

  rejectKyc: (userId: string, reason: string) =>
    apiClient.post<{ success: boolean }>('/admin/kyc/reject', {
      userId,
      reason,
    }),

  // Audit logs
  getAuditLogs: (params: {
    page?: number;
    limit?: number;
    module?: string;
    actorId?: string;
    severity?: string;
    from?: string;
    to?: string;
  } = {}) => {
    const query = new URLSearchParams();
    if (params.page) query.set('page', String(params.page));
    if (params.limit) query.set('limit', String(params.limit));
    if (params.module) query.set('module', params.module);
    if (params.actorId) query.set('actorId', params.actorId);
    if (params.severity) query.set('severity', params.severity);
    if (params.from) query.set('from', params.from);
    if (params.to) query.set('to', params.to);
    return apiClient.get<PaginatedResult<AuditLogItem>>(
      `/admin/audit-logs?${query.toString()}`,
    );
  },

  // AI Settings
  getAiSettings: () => apiClient.get<AiSettings>('/admin/settings/ai'),

  updateAiSettings: (body: UpdateAiSettingsPayload) =>
    apiClient.patch<AiSettings>('/admin/settings/ai', body),

  // B2B account provisioning
  provisionDeveloper: (body: ProvisionDeveloperPayload) =>
    apiClient.post<ProvisionAccountResult>('/admin/accounts/developers', body),

  provisionParkOperator: (body: ProvisionParkOperatorPayload) =>
    apiClient.post<ProvisionAccountResult>('/admin/accounts/park-operators', body),
};

export interface ProvisionDeveloperPayload {
  phone: string;
  password: string;
  fullName: string;
  email?: string;
  projectIds?: string[];
}

export interface ProvisionParkOperatorPayload {
  phone: string;
  password: string;
  fullName: string;
  email?: string;
  parkIds?: string[];
}

export interface ProvisionAccountResult {
  userId: string;
  phone: string;
  email: string | null;
  fullName: string;
  linkedProjectIds?: string[];
  linkedParkIds?: string[];
}
