
import { 
  IUser, IProject, IAsset, ITicket, IKPI, IAuditLog, IFinancialData, 
  IComplianceDoc, IHSEIncident, IRiskAssessment, IReport, ISystemSettings,
  IContract, IInventoryItem, IWorkOrder, IPMSchedule,
  UserRole, AssetStatus, TicketStatus, TicketType 
} from '../../types';

// Mock IDs
const PID_1 = 's1'; // Project 1
const PID_2 = 's2'; // Project 2

export const INITIAL_DATA: Record<string, any> = {
  users: [
    {
      _id: 'u1',
      email: 'admin@vuphong.vn',
      name: 'Admin System',
      role: UserRole.ADMIN,
      department: 'HQ',
      status: 'Active',
      active: true,
      scope: ['Tất cả']
    },
    {
      _id: 'u2',
      email: 'manager@vuphong.vn',
      name: 'Nguyễn Văn A',
      role: UserRole.OM_MANAGER,
      department: 'Phòng O&M',
      status: 'Active',
      active: true,
      scope: [PID_1, PID_2]
    },
    {
      _id: 'u3',
      email: 'tech@vuphong.vn',
      name: 'Lê Thị B',
      role: UserRole.TECHNICIAN,
      department: 'Vận hành hiện trường',
      status: 'Active',
      active: true,
      scope: [PID_1]
    }
  ],
  companies: [
    {
      _id: 'c1',
      name: 'VuPhong Energy Group',
      code: 'VPEG',
      taxCode: '0123456789',
      address: '123 Energy Street, Ho Chi Minh City',
      phone: '+84-28-1234-5678',
      email: 'info@vuphong.com',
      status: 'active'
    }
  ],
  projects: [
    {
      _id: PID_1,
      companyId: 'c1',
      name: 'VP Solar Farm Ninh Thuận 1',
      code: 'VP-NT1',
      capacityMWp: 50,
      location: {
        address: 'Ninh Thuận, Việt Nam',
        coordinates: { lat: 11.56, lng: 108.99 }
      },
      commissioningDate: '2020-06-15',
      status: 'operational',
      managerId: 'u2'
    },
    {
      _id: PID_2,
      companyId: 'c1',
      name: 'VP Rooftop KCN B',
      code: 'VP-RT-B',
      capacityMWp: 3.5,
      location: {
        address: 'Bình Dương, Việt Nam',
        coordinates: { lat: 11.12, lng: 106.60 }
      },
      commissioningDate: '2021-02-10',
      status: 'operational',
      managerId: 'u2'
    }
  ],
  assets: [
    // Site 1 Assets
    { _id: 'a1', projectId: PID_1, parentAssetId: null, assetType: 'Plant', name: 'Trạm điều khiển trung tâm', code: 'PLANT-01', status: AssetStatus.OPERATIONAL, manufacturer: 'Vũ Phong', installDate: '2020-06-01', specifications: {} },
    { _id: 'a2', projectId: PID_1, parentAssetId: 'a1', assetType: 'Block', name: 'Block A', code: 'BLK-01', serialNumber: 'BLK-001', status: AssetStatus.OPERATIONAL, manufacturer: 'N/A', installDate: '2020-06-01', specifications: {} },
    { _id: 'a3', projectId: PID_1, parentAssetId: 'a2', assetType: 'Inverter', name: 'Inverter 01', code: 'INV-01', serialNumber: 'INV-HUA-001', status: AssetStatus.OPERATIONAL, manufacturer: 'Huawei', installDate: '2020-06-01', specifications: { power: '185kW' } },
    { _id: 'a4', projectId: PID_1, parentAssetId: 'a2', assetType: 'Inverter', name: 'Inverter 02', code: 'INV-02', serialNumber: 'INV-HUA-002', status: AssetStatus.FAULTY, manufacturer: 'Huawei', installDate: '2020-06-01', specifications: { power: '185kW' } },
    { _id: 'a5', projectId: PID_1, parentAssetId: 'a3', assetType: 'String', name: 'String 01.01', code: 'STR-01', serialNumber: 'STR-001', status: AssetStatus.OPERATIONAL, manufacturer: 'N/A', installDate: '2020-06-01', specifications: {} },
    // Site 2 Assets
    { _id: 'a6', projectId: PID_2, parentAssetId: null, assetType: 'Plant', name: 'Hệ thống Rooftop Tòa nhà A', code: 'RT-PLANT-01', serialNumber: 'RT-001', status: AssetStatus.OPERATIONAL, manufacturer: 'Vũ Phong', installDate: '2021-02-01', specifications: {} },
    { _id: 'a7', projectId: PID_2, parentAssetId: 'a6', assetType: 'Inverter', name: 'Inverter Tổng', code: 'RT-INV-01', serialNumber: 'INV-SMA-001', status: AssetStatus.OPERATIONAL, manufacturer: 'SMA', installDate: '2021-02-01', specifications: { power: '110kW' } },
  ],
  tickets: [
    {
      _id: 't1',
      projectId: PID_1,
      assetId: 'a4',
      contractId: 'c2',
      code: 'TK-2023-001',
      title: 'Cảnh báo quá nhiệt Inverter',
      description: 'AI phát hiện nhiệt độ tăng bất thường tại Inverter 02 trong giờ cao điểm.',
      ticketType: TicketType.PREDICTIVE,
      priority: 'High',
      status: TicketStatus.OPEN,
      assignedTo: 'u3',
      reportedBy: 'System',
      createdAt: '2023-10-25T09:00:00Z',
      slaDeadline: '2023-10-26T09:00:00Z'
    },
    {
      _id: 't2',
      projectId: PID_1,
      assetId: 'a2',
      contractId: 'c2',
      code: 'TK-2023-002',
      title: 'Vệ sinh định kỳ Block A',
      description: 'Lịch vệ sinh tấm pin cho Block A. Tỷ lệ bẩn (Soiling loss) > 3%.',
      ticketType: TicketType.CLEANING,
      priority: 'Medium',
      status: TicketStatus.IN_PROGRESS,
      assignedTo: 'u3',
      reportedBy: 'u2',
      createdAt: '2023-10-24T08:00:00Z',
      slaDeadline: '2023-10-28T17:00:00Z'
    }
  ],
  workOrders: [
    { 
      _id: 'wo1', 
      woCode: 'WO-2023-1001', 
      ticketId: 't1', 
      projectId: PID_1, 
      assetId: 'a4', 
      contractId: 'c2',
      title: 'Thay thế quạt Inverter 02', 
      workOrderType: 'CM', 
      status: 'In Progress', 
      priority: 'High', 
      assignedTechnicianId: 'u3', 
      assignedTo: 'u3', // Legacy
      scheduledStart: '2023-10-26T08:00:00Z',
      scheduledDate: '2023-10-26', // Legacy
      checklistProgress: 50, 
      costLabor: 500000, 
      costMaterial: 2000000,
      costExternal: 0,
      totalCost: 2500000
    },
    { 
      _id: 'wo2', 
      woCode: 'WO-2023-1002', 
      projectId: PID_1, 
      assetId: 'a2', 
      contractId: 'c2',
      title: 'Bảo trì định kỳ Block A (Q4)', 
      workOrderType: 'PM', 
      status: 'Draft', 
      priority: 'Medium', 
      assignedTechnicianId: 'u3',
      assignedTo: 'u3', // Legacy
      scheduledStart: '2023-11-01T08:00:00Z',
      scheduledDate: '2023-11-01', // Legacy
      checklistProgress: 0, 
      costLabor: 0, 
      costMaterial: 0,
      costExternal: 0,
      totalCost: 0
    },
  ],
  pmSchedules: [
    { _id: 'pm1', projectId: PID_1, assetId: 'a1', taskName: 'Kiểm tra trạm biến áp', pmType: 'Inspection', frequencyMonths: 3, frequency: 'Quarterly', lastPerformedDate: '2023-07-15', lastPerformed: '2023-07-15', nextDueDate: '2023-10-15', nextDue: '2023-10-15', status: 'active', assignedRole: UserRole.TECHNICIAN },
    { _id: 'pm2', projectId: PID_1, assetId: 'a3', taskName: 'Vệ sinh Filter Inverter', pmType: 'Cleaning', frequencyMonths: 1, frequency: 'Monthly', lastPerformedDate: '2023-09-20', lastPerformed: '2023-09-20', nextDueDate: '2023-10-20', nextDue: '2023-10-20', status: 'active', assignedRole: UserRole.TECHNICIAN },
  ],
  kpis: Array.from({ length: 30 }, (_, i) => {
    const date = new Date();
    date.setDate(date.getDate() - (29 - i));
    return {
      _id: `kpi-${i}`,
      siteId: PID_1,
      date: date.toISOString().split('T')[0],
      pr: 75 + Math.random() * 10,
      availability: 98 + Math.random() * 2,
      specificYield: 3.5 + Math.random() * 1.5,
      irradiation: 4 + Math.random() * 2,
      production: 150 + Math.random() * 50
    };
  }),
  logs: [
    {
      _id: 'l1',
      userId: 'u1',
      action: 'LOGIN',
      targetCollection: 'users',
      targetId: 'u1',
      timestamp: new Date().toISOString(),
      details: 'Người dùng đăng nhập thành công'
    }
  ],
  financials: [
    { month: 'Th01', revenue: 3.2, opex: 0.5, netProfit: 2.7, cumulativeROI: 12.5 },
    { month: 'Th02', revenue: 3.5, opex: 0.6, netProfit: 2.9, cumulativeROI: 13.1 },
    { month: 'Th03', revenue: 4.1, opex: 0.5, netProfit: 3.6, cumulativeROI: 14.0 },
    { month: 'Th04', revenue: 4.5, opex: 0.7, netProfit: 3.8, cumulativeROI: 15.2 },
    { month: 'Th05', revenue: 4.8, opex: 0.5, netProfit: 4.3, cumulativeROI: 16.5 },
    { month: 'Th06', revenue: 5.0, opex: 0.6, netProfit: 4.4, cumulativeROI: 17.8 },
    { month: 'Th07', revenue: 4.9, opex: 0.5, netProfit: 4.4, cumulativeROI: 19.0 },
    { month: 'Th08', revenue: 4.6, opex: 0.8, netProfit: 3.8, cumulativeROI: 20.1 },
    { month: 'Th09', revenue: 3.8, opex: 0.5, netProfit: 3.3, cumulativeROI: 20.9 },
    { month: 'Th10', revenue: 3.5, opex: 0.5, netProfit: 3.0, cumulativeROI: 21.5 },
    { month: 'Th11', revenue: 3.1, opex: 0.6, netProfit: 2.5, cumulativeROI: 22.0 },
    { month: 'Th12', revenue: 2.9, opex: 0.5, netProfit: 2.4, cumulativeROI: 22.4 },
  ],
  compliance: [
    { _id: 'd1', siteId: PID_1, name: 'Giấy phép hoạt động điện lực', type: 'License', issueDate: '2020-01-01', expiryDate: '2030-01-01', status: 'Valid', issuingAuthority: 'Bộ Công Thương' },
    { _id: 'd2', siteId: PID_1, name: 'Giấy chứng nhận PCCC', type: 'Permit', issueDate: '2020-05-15', expiryDate: '2023-12-31', status: 'Expiring Soon', issuingAuthority: 'Cảnh sát PCCC' },
    { _id: 'd3', siteId: PID_1, name: 'Chứng chỉ ISO 45001:2018', type: 'ISO Certificate', issueDate: '2021-06-01', expiryDate: '2024-06-01', status: 'Valid', issuingAuthority: 'TUV Rheinland' },
    { _id: 'd4', siteId: PID_1, name: 'Hợp đồng bảo hiểm tài sản', type: 'Contract', issueDate: '2023-01-01', expiryDate: '2024-01-01', status: 'Valid', issuingAuthority: 'PVI Insurance' }
  ],
  incidents: [
    { _id: 'i1', siteId: PID_1, date: '2023-09-10', type: 'Near Miss', description: 'Công nhân suýt trượt ngã khi vệ sinh pin do sàn ướt', severity: 'Low', status: 'Closed', mitigationAction: 'Tăng cường giày chống trượt và dây an toàn' },
    { _id: 'i2', siteId: PID_1, date: '2023-10-05', type: 'Hazard', description: 'Phát hiện tổ ong lớn tại tủ Combiner Box 04', severity: 'Medium', status: 'Open', mitigationAction: 'Liên hệ đội xử lý côn trùng' }
  ],
  risks: [
    { _id: 'r1', riskCategory: 'Financial', description: 'Thay đổi chính sách giá FIT/thị trường điện', probability: 'Medium', impact: 'High', riskLevel: 'High', mitigationStrategy: 'Đa dạng hóa danh mục đầu tư, ký PPA dài hạn' },
    { _id: 'r2', riskCategory: 'Technical', description: 'Hỏng hóc Inverter ngoài thời gian bảo hành', probability: 'Low', impact: 'Medium', riskLevel: 'Medium', mitigationStrategy: 'Dự phòng ngân sách thay thế, bảo trì dự báo' },
    { _id: 'r3', riskCategory: 'HSE', description: 'Tai nạn điện giật khi bảo trì', probability: 'Low', impact: 'High', riskLevel: 'High', mitigationStrategy: 'Tuân thủ nghiêm ngặt quy trình LOTO, đào tạo an toàn định kỳ' }
  ],
  contracts: [
    { 
      _id: 'c1', 
      projectId: PID_1, 
      contractNumber: 'PPA-2020-001', 
      contractType: 'PPA', 
      partyA: 'EVN', 
      partyB: 'Vu Phong Energy', 
      startDate: '2020-06-15', 
      endDate: '2040-06-15', 
      value: 2000000000000, 
      status: 'active', 
      type: 'PPA',
      customerGroup: 'TTE',
      customerCode: 'CUST-001',
      siteAddress: 'Ninh Thuận',
      capacityKWp: 50000,
      isFree: false,
      notes: 'Hợp đồng bán điện dài hạn với EVN'
    },
    { 
      _id: 'c2', 
      projectId: PID_1, 
      contractNumber: 'OM-2020-001', 
      contractType: 'O&M', 
      partyA: 'Vu Phong Energy', 
      partyB: 'VP O&M Services', 
      startDate: '2020-06-15', 
      endDate: '2025-06-15', 
      value: 5000000000, 
      status: 'active', 
      type: 'O&M',
      customerGroup: 'Assetco',
      customerCode: 'CUST-002',
      capacityKWp: 50000,
      isFree: false,
      paymentCycle: '30_days',
      serviceTotal: 60,
      servicePerformed: 38,
      serviceFrequency: 'Hàng tháng',
      debtAmount: 0,
      revenuePlan: [
        { year: 2023, q1: 1250000000, q2: 1250000000, q3: 1250000000, q4: 1250000000 },
        { year: 2024, q1: 1300000000, q2: 1300000000, q3: 1300000000, q4: 1300000000 }
      ]
    },
    { 
      _id: 'c3', 
      projectId: PID_1, 
      contractNumber: 'INS-2023-099', 
      contractType: 'Insurance', 
      partyA: 'PVI Insurance', 
      partyB: 'Vu Phong Energy', 
      startDate: '2023-01-01', 
      endDate: '2024-01-01', 
      value: 200000000, 
      status: 'Expiring', 
      type: 'Insurance',
      customerGroup: 'Other'
    },
    {
      _id: 'c4',
      projectId: PID_2,
      contractNumber: 'OM-EPC-001',
      contractType: 'O&M',
      partyA: 'KCN B Owner',
      partyB: 'VP O&M Services',
      startDate: '2021-02-10',
      endDate: '2023-02-10',
      value: 0,
      status: 'active',
      customerGroup: 'EPC',
      isFree: true,
      serviceTotal: 8,
      servicePerformed: 7,
      serviceFrequency: 'Quý (3 tháng/lần)',
      notes: 'Bảo hành O&M miễn phí 2 năm đầu theo hợp đồng EPC'
    },
    {
      _id: 'c5',
      projectId: PID_2,
      contractNumber: 'OM-EXT-002',
      contractType: 'O&M',
      partyA: 'KCN B Owner',
      partyB: 'VP O&M Services',
      startDate: '2023-02-11',
      endDate: '2025-02-11',
      value: 1200000000,
      status: 'negotiating',
      customerGroup: 'Eco',
      isFree: false,
      notes: 'Đang thương thảo gia hạn sau khi hết hạn bảo hành EPC'
    }
  ],
  inventory: [
    { _id: 'inv1', itemCode: 'SP-INV-FAN-01', name: 'Quạt tản nhiệt Inverter Huawei', category: 'CriticalSpare', quantity: 5, currentStock: 5, unit: 'Cái', minStock: 2, minStockLevel: 2, location: 'Kho Ninh Thuận', supplier: 'Huawei Vietnam' },
    { _id: 'inv2', itemCode: 'SP-MC4-CONN', name: 'Đầu nối MC4', category: 'Consumable', quantity: 200, currentStock: 200, unit: 'Cặp', minStock: 50, minStockLevel: 50, location: 'Kho Ninh Thuận', supplier: 'Stäubli' },
    { _id: 'inv3', itemCode: 'TOOL-THERMO', name: 'Camera nhiệt FLIR', category: 'Tool', quantity: 2, currentStock: 2, unit: 'Bộ', minStock: 1, minStockLevel: 1, location: 'Phòng Kỹ thuật', supplier: 'FLIR Systems' },
  ],
  reports: [
    { _id: 'rep1', title: 'Báo cáo vận hành Tháng 9/2023', type: 'Monthly', generatedDate: '2023-10-01T08:00:00Z', createdBy: 'Hệ thống', format: 'PDF', size: '2.5 MB', status: 'Ready', downloadUrl: '#' },
    { _id: 'rep2', title: 'Báo cáo tài chính Quý 3/2023', type: 'Financial', generatedDate: '2023-10-05T09:30:00Z', createdBy: 'Nguyễn Văn A', format: 'Excel', size: '1.2 MB', status: 'Ready', downloadUrl: '#' },
    { _id: 'rep3', title: 'Nhật ký vận hành 25/10/2023', type: 'Daily', generatedDate: '2023-10-26T06:00:00Z', createdBy: 'Hệ thống', format: 'PDF', size: '0.8 MB', status: 'Ready', downloadUrl: '#' },
    { _id: 'rep4', title: 'Báo cáo sự cố Inverter 02', type: 'Custom', generatedDate: '2023-10-25T14:00:00Z', createdBy: 'Lê Thị B', format: 'PDF', size: '1.5 MB', status: 'Ready', downloadUrl: '#' }
  ],
  settings: {
    systemName: 'Hệ thống VPEG AM & O&M',
    logoUrl: '',
    maintenanceMode: false,
    smtp: {
      host: 'smtp.gmail.com',
      port: 587,
      user: 'notifications@vuphong.vn',
      pass: '**********',
      secure: true,
      senderEmail: 'no-reply@vuphong.vn'
    },
    retentionPolicy: {
      logDays: 90,
      reportDays: 365
    }
  },
  alerts: [
    {
      _id: 'alt-1',
      type: 'System',
      message: 'Hệ thống khởi động thành công',
      severity: 'Info',
      createdAt: new Date().toISOString(),
      isRead: true
    }
  ]
};
