/**
 * [MOCK-002] 证书数据源抽象层
 * 通过环境变量自动切换Mock/真实API
 * AI注意: 这是唯一入口，业务代码必须调用此层
 * 
 * @module services/certificateDataSource
 * @author AI-Frontend-Team
 * @created 2026-03-19
 */

import { Certificate } from '@/types/certificate';
import { IDataSource, CertificateQueryParams } from '@/types/datasource';
import { BaseDataSource, BaseMockDataSource, DataSourceFactory } from './dataSourceFactory';

// ============================================
// 真实API实现
// ============================================

/**
 * 证书API数据源
 * 调用真实后端API
 */
class ApiCertificateDataSource extends BaseDataSource<Certificate, CertificateQueryParams> {
  constructor() {
    super('/api/v1/certificate/certificates');
  }

  /**
   * 更新证书状态（审核）
   * @param id 证书ID
   * @param status 新状态
   * @param approvedBy 审核人
   */
  async updateStatus(id: string, status: string, approvedBy?: string): Promise<Certificate> {
    const response = await fetch(`${this.baseUrl}/${id}/status`, {
      method: 'PUT',
      headers: {
        'Content-Type': 'application/json',
      },
      body: JSON.stringify({ status, approvedBy }),
    });

    if (!response.ok) {
      throw new Error(`API Error: ${response.status}`);
    }

    const result = await response.json();
    
    // 更新成功后获取完整数据
    const updated = await this.detail(id);
    if (updated) return updated;
    
    return result.data;
  }
}

// ============================================
// Mock实现 (完全独立文件)
// ============================================

/**
 * [MOCK] 证书Mock数据源
 * AI注意: 这是Mock实现，不是真实业务逻辑
 * 仅在REACT_APP_USE_MOCK=true时启用
 */
class MockCertificateDataSource extends BaseMockDataSource<Certificate, CertificateQueryParams> {
  /** Mock数据源名称 */
  readonly __MOCK_NAME__ = 'MockCertificateDataSource';

  /** Mock数据 */
  protected mockData: Certificate[] = [
    {
      id: '1',
      name: 'CE认证证书',
      type: 'PRODUCT_CERT',
      status: 'APPROVED',
      fileUrl: '/files/ce-cert.pdf',
      fileName: 'CE-Certificate-2026.pdf',
      uploadDate: '2026-03-15',
      expiryDate: '2027-03-15',
      approvedBy: 'admin',
      approvedDate: '2026-03-16',
      productId: 'P001',
      productName: '工业温度传感器',
      notes: '欧盟市场准入认证',
    },
    {
      id: '2',
      name: '营业执照',
      type: 'BUSINESS_LICENSE',
      status: 'APPROVED',
      fileUrl: '/files/business-license.pdf',
      fileName: 'Business-License-2026.pdf',
      uploadDate: '2026-01-10',
      expiryDate: '2027-01-10',
      approvedBy: 'admin',
      approvedDate: '2026-01-11',
      notes: '企业营业执照',
    },
    {
      id: '3',
      name: 'FCC认证',
      type: 'SAFETY_CERT',
      status: 'PENDING',
      fileUrl: '/files/fcc-cert.pdf',
      fileName: 'FCC-Certificate.pdf',
      uploadDate: '2026-03-18',
      expiryDate: '2027-03-18',
      productId: 'P002',
      productName: 'PLC控制器',
      notes: '美国FCC认证',
    },
    {
      id: '4',
      name: 'ISO9001',
      type: 'QUALITY_CERT',
      status: 'EXPIRED',
      fileUrl: '/files/iso9001.pdf',
      fileName: 'ISO9001-2025.pdf',
      uploadDate: '2025-01-01',
      expiryDate: '2026-01-01',
      approvedBy: 'admin',
      approvedDate: '2025-01-02',
      notes: '质量管理体系认证，已过期需要更新',
    },
    {
      id: '5',
      name: 'RoHS认证',
      type: 'PRODUCT_CERT',
      status: 'APPROVED',
      fileUrl: '/files/rohs-cert.pdf',
      fileName: 'RoHS-Certificate.pdf',
      uploadDate: '2026-02-20',
      expiryDate: '2027-02-20',
      approvedBy: 'admin',
      approvedDate: '2026-02-21',
      productId: 'P003',
      productName: '环保型传感器',
      notes: '欧盟RoHS环保认证',
    },
    {
      id: '6',
      name: 'UL认证',
      type: 'SAFETY_CERT',
      status: 'PENDING',
      fileUrl: '/files/ul-cert.pdf',
      fileName: 'UL-Certificate.pdf',
      uploadDate: '2026-03-19',
      expiryDate: '2027-03-19',
      productId: 'P004',
      productName: '工业电源模块',
      notes: '美国UL安全认证',
    },
  ];

  /**
   * 更新证书状态（审核）
   * @param id 证书ID
   * @param status 新状态
   * @param approvedBy 审核人
   */
  async updateStatus(id: string, status: string, approvedBy?: string): Promise<Certificate> {
    await this.delay(300);

    const index = this.mockData.findIndex(item => item.id === id);
    if (index === -1) {
      throw new Error('Certificate not found');
    }

    const updates: Partial<Certificate> = {
      status: status as any,
    };

    if (status === 'APPROVED') {
      updates.approvedBy = approvedBy || 'admin';
      updates.approvedDate = new Date().toISOString().split('T')[0];
    }

    this.mockData[index] = { ...this.mockData[index], ...updates };
    return this.mockData[index];
  }
}

// ============================================
// 导出数据源
// ============================================

export const certificateDataSource = DataSourceFactory.createWithMethods<
  Certificate,
  CertificateQueryParams,
  {
    updateStatus(id: string, status: string, approvedBy?: string): Promise<Certificate>;
  }
>({
  apiDataSource: ApiCertificateDataSource,
  mockDataSource: MockCertificateDataSource,
});

/**
 * Mock状态标记
 * 用于调试和开发环境识别
 */
export { __MOCK__, __DATA_SOURCE_TYPE__ } from './dataSourceFactory';
