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

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

export type { Certificate } from '@/types/certificate';
export { CertificateStatus, CertificateType };

class ApiCertificateDataSource extends BaseDataSource<Certificate, CertificateQueryParams> {
  constructor() {
    super('/api/v1/certificate/certificates');
  }

  async updateStatus(id: string, status: string, approvedBy?: string): Promise<Certificate> {
    const response = await http.put(`${this.baseUrl}/${id}/status`, { status, approvedBy });
    return response.data;
  }
}

class MockCertificateDataSource extends BaseMockDataSource<Certificate, CertificateQueryParams> implements IDataSource<Certificate, CertificateQueryParams> {
  readonly __MOCK__ = true as const;
  readonly __MOCK_NAME__ = 'MockCertificateDataSource';

  protected mockData: Certificate[] = [
    {
      id: '1',
      name: 'CE认证证书',
      type: CertificateType.PRODUCT_CERT,
      status: CertificateStatus.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: CertificateType.BUSINESS_LICENSE,
      status: CertificateStatus.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: CertificateType.SAFETY_CERT,
      status: CertificateStatus.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: CertificateType.QUALITY_CERT,
      status: CertificateStatus.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: CertificateType.PRODUCT_CERT,
      status: CertificateStatus.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: CertificateType.SAFETY_CERT,
      status: CertificateStatus.PENDING,
      fileUrl: '/files/ul-cert.pdf',
      fileName: 'UL-Certificate.pdf',
      uploadDate: '2026-03-19',
      expiryDate: '2027-03-19',
      productId: 'P004',
      productName: '工业电源模块',
      notes: '美国UL安全认证',
    },
  ];

  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 CertificateStatus,
    };

    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,
});

export { __MOCK__, __DATA_SOURCE_TYPE__ } from './dataSourceFactory';
