/**
 * 身份认证服务
 * 处理用户身份认证申请、状态查询等功能
 */

import { api } from '../core/api';
import {
  VerificationStatusDTO,
  VerificationRecordDTO,
  SubmitVerificationRequest,
  PaginatedData,
} from '@/types/dto';

// 提交认证响应
export interface SubmitVerificationResponse {
  id: string;
  user_id: string;
  identity: string;
  status: string;
  real_name: string;
  student_id?: string;
  employee_id?: string;
  department?: string;
  proof_materials: string;
  created_at: string;
  updated_at: string;
}

class VerificationService {
  // 获取当前用户的认证状态
  async getVerificationStatus(): Promise<VerificationStatusDTO | null> {
    try {
      const response = await api.get<VerificationStatusDTO>('/verification/status');
      return response.data;
    } catch (error) {
      console.error('[VerificationService] 获取认证状态失败:', error);
      return null;
    }
  }

  // 提交认证申请
  async submitVerification(data: SubmitVerificationRequest): Promise<SubmitVerificationResponse | null> {
    try {
      const response = await api.post<SubmitVerificationResponse>('/verification/submit', data);
      return response.data;
    } catch (error: any) {
      console.error('[VerificationService] 提交认证申请失败:', error);
      throw error;
    }
  }

  // 获取认证记录列表
  async getVerificationRecords(page = 1, pageSize = 10): Promise<PaginatedData<VerificationRecordDTO> | null> {
    try {
      const response = await api.get<PaginatedData<VerificationRecordDTO>>('/verification/records', {
        page,
        page_size: pageSize,
      });
      return response.data;
    } catch (error) {
      console.error('[VerificationService] 获取认证记录失败:', error);
      return null;
    }
  }
}

// 导出服务实例
export const verificationService = new VerificationService();
