/**
 * [MOCK] 证书Mock数据
 * AI注意: 这是Mock数据，不是真实业务数据
 * 
 * @module mock/data/certificate.mock
 * @author AI-Frontend-Team
 * @created 2026-03-19
 */

import { Certificate } from '@/types/certificate';

/**
 * 证书Mock数据列表
 */
export const mockCertificates: 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安全认证',
  },
  {
    id: '7',
    name: 'CCC认证',
    type: 'PRODUCT_CERT',
    status: 'APPROVED',
    fileUrl: '/files/ccc-cert.pdf',
    fileName: 'CCC-Certificate.pdf',
    uploadDate: '2026-01-15',
    expiryDate: '2027-01-15',
    approvedBy: 'admin',
    approvedDate: '2026-01-16',
    productId: 'P005',
    productName: '家用智能插座',
    notes: '中国强制性产品认证',
  },
  {
    id: '8',
    name: 'GS认证',
    type: 'SAFETY_CERT',
    status: 'REJECTED',
    fileUrl: '/files/gs-cert.pdf',
    fileName: 'GS-Certificate.pdf',
    uploadDate: '2026-03-10',
    expiryDate: '2027-03-10',
    productId: 'P006',
    productName: '工业控制面板',
    notes: '德国GS安全认证 - 材料不符合要求',
  },
];

/**
 * 获取证书列表
 * @param params 查询参数
 * @returns 过滤后的证书列表
 */
export function getMockCertificates(params?: {
  status?: string;
  type?: string;
  keyword?: string;
}): Certificate[] {
  let result = [...mockCertificates];

  if (params?.status) {
    result = result.filter(item => item.status === params.status);
  }

  if (params?.type) {
    result = result.filter(item => item.type === params.type);
  }

  if (params?.keyword) {
    const keyword = params.keyword.toLowerCase();
    result = result.filter(
      item =>
        item.name.toLowerCase().includes(keyword) ||
        item.productName?.toLowerCase().includes(keyword) ||
        item.notes?.toLowerCase().includes(keyword)
    );
  }

  return result;
}

/**
 * 根据ID获取证书
 * @param id 证书ID
 * @returns 证书对象或undefined
 */
export function getMockCertificateById(id: string): Certificate | undefined {
  return mockCertificates.find(item => item.id === id);
}

/**
 * 创建新证书
 * @param data 证书数据
 * @returns 创建的证书
 */
export function createMockCertificate(data: Partial<Certificate>): Certificate {
  const newCert: Certificate = {
    id: `${Date.now()}`,
    name: data.name || '',
    type: data.type || 'OTHER',
    status: 'PENDING',
    fileUrl: data.fileUrl || '/files/uploaded.pdf',
    fileName: data.fileName || 'uploaded.pdf',
    uploadDate: new Date().toISOString().split('T')[0],
    expiryDate: data.expiryDate || '',
    productId: data.productId,
    productName: data.productName,
    notes: data.notes,
  };

  mockCertificates.unshift(newCert);
  return newCert;
}

/**
 * 更新证书
 * @param id 证书ID
 * @param data 更新数据
 * @returns 更新后的证书或null
 */
export function updateMockCertificate(
  id: string,
  data: Partial<Certificate>
): Certificate | null {
  const index = mockCertificates.findIndex(item => item.id === id);
  if (index === -1) return null;

  mockCertificates[index] = { ...mockCertificates[index], ...data };
  return mockCertificates[index];
}

/**
 * 删除证书
 * @param id 证书ID
 * @returns 是否删除成功
 */
export function deleteMockCertificate(id: string): boolean {
  const index = mockCertificates.findIndex(item => item.id === id);
  if (index === -1) return false;

  mockCertificates.splice(index, 1);
  return true;
}
