'use strict';

module.exports = {
  async up(queryInterface, Sequelize) {
    // Get RAM Industries company ID (by name or code)
    const [companies] = await queryInterface.sequelize.query(
      "SELECT id FROM company WHERE name LIKE '%RAM Industries%' OR code = 'COMP001' LIMIT 1"
    );
    
    let companyId;
    if (companies.length === 0) {
      // Fallback to first company if RAM Industries not found
      const [fallbackCompanies] = await queryInterface.sequelize.query(
        "SELECT id FROM company LIMIT 1"
      );
      if (fallbackCompanies.length === 0) {
        console.log('No company found. Please create a company first.');
        return;
      }
      companyId = fallbackCompanies[0].id;
      console.log(`RAM Industries not found. Using company ID: ${companyId}`);
    } else {
      companyId = companies[0].id;
      console.log(`Seeding documents for RAM Industries (company ID: ${companyId})`);
    }

    // Document data from the image - LIST OF DOCUMENTS from RAM INDUSTRIES
    const documents = [
      // Manufacturing (MF) Documents
      { document_number: 'RI/MF/D/01', title: 'LIST OF MACHINES', category: 'MF', document_type: 'D', company_id: companyId, status: 'ACTIVE', created_at: new Date(), updated_at: new Date() },
      { document_number: 'RI/MF/F/01', title: 'PROCESS SHEET', category: 'MF', document_type: 'F', company_id: companyId, status: 'ACTIVE', created_at: new Date(), updated_at: new Date() },
      { document_number: 'RI/MF/F/02', title: 'DAILY WORK MANAGEMENT', category: 'MF', document_type: 'F', company_id: companyId, status: 'ACTIVE', created_at: new Date(), updated_at: new Date() },
      { document_number: 'RI/MF/F/03', title: 'BILL OF MATERIAL', category: 'MF', document_type: 'F', company_id: companyId, status: 'ACTIVE', created_at: new Date(), updated_at: new Date() },
      { document_number: 'RI/MF/F/04', title: 'JOB CARD-OUTWARD', category: 'MF', document_type: 'F', company_id: companyId, status: 'ACTIVE', created_at: new Date(), updated_at: new Date() },
      { document_number: 'RI/MF/F/05', title: 'JOB CARD WELDING', category: 'MF', document_type: 'F', company_id: companyId, status: 'ACTIVE', created_at: new Date(), updated_at: new Date() },
      { document_number: 'RI/MF/F/06', title: 'DRAWING ISSUE REGISTER', category: 'MF', document_type: 'F', company_id: companyId, status: 'ACTIVE', created_at: new Date(), updated_at: new Date() },
      { document_number: 'RI/MF/F/07', title: 'PROCESS CHANGE FORM', category: 'MF', document_type: 'F', company_id: companyId, status: 'ACTIVE', created_at: new Date(), updated_at: new Date() },
      { document_number: 'RI/MF/F/08', title: 'WIP-STATUS', category: 'MF', document_type: 'F', company_id: companyId, status: 'ACTIVE', created_at: new Date(), updated_at: new Date() },
      { document_number: 'RI/MF/F/09', title: 'TOOLS LIST', category: 'MF', document_type: 'F', company_id: companyId, status: 'ACTIVE', created_at: new Date(), updated_at: new Date() },
      { document_number: 'RI/MF/F/10', title: 'DESPATCH CLEARANCE REPORT', category: 'MF', document_type: 'F', company_id: companyId, status: 'ACTIVE', created_at: new Date(), updated_at: new Date() },
      
      // Marketing (MKT) Documents
      { document_number: 'RI/MKT/F/01', title: 'ENQUIRY/FEASIBILITY REPORT', category: 'MKT', document_type: 'F', company_id: companyId, status: 'ACTIVE', created_at: new Date(), updated_at: new Date() },
      { document_number: 'RI/MKT/F/02', title: 'WORK ORDER REGISTER', category: 'MKT', document_type: 'F', company_id: companyId, status: 'ACTIVE', created_at: new Date(), updated_at: new Date() },
      
      // Maintenance (MN) Documents
      { document_number: 'RI/MN/F/01', title: 'PREVENTIVE MAINTANENCE SCHEDULE', category: 'MN', document_type: 'F', company_id: companyId, status: 'ACTIVE', created_at: new Date(), updated_at: new Date() },
      { document_number: 'RI/MN/F/02', title: 'MACHINE HISTORY CARD', category: 'MN', document_type: 'F', company_id: companyId, status: 'ACTIVE', created_at: new Date(), updated_at: new Date() },
      
      // Purchase (PUR) Documents
      { document_number: 'RI/PUR/D/01', title: 'LIST OF APPROVED SUPPLIERS', category: 'PUR', document_type: 'D', company_id: companyId, status: 'ACTIVE', created_at: new Date(), updated_at: new Date() },
      { document_number: 'RI/PUR/F/01', title: 'SUPPLIER REGISTRATION FORM', category: 'PUR', document_type: 'F', company_id: companyId, status: 'ACTIVE', created_at: new Date(), updated_at: new Date() },
      { document_number: 'RI/PUR/F/02', title: 'PURCHASE REQUISTION SLIP', category: 'PUR', document_type: 'F', company_id: companyId, status: 'ACTIVE', created_at: new Date(), updated_at: new Date() },
      { document_number: 'RI/PUR/F/03', title: 'PURCHASE ORDER', category: 'PUR', document_type: 'F', company_id: companyId, status: 'ACTIVE', created_at: new Date(), updated_at: new Date() },
      { document_number: 'RI/PUR/F/04', title: 'STOCK REGISTER', category: 'PUR', document_type: 'F', company_id: companyId, status: 'ACTIVE', created_at: new Date(), updated_at: new Date() },
      { document_number: 'RI/PUR/F/05', title: 'PURCHASE DATA', category: 'PUR', document_type: 'F', company_id: companyId, status: 'ACTIVE', created_at: new Date(), updated_at: new Date() },
      
      // Quality Assurance (QA) Documents
      { document_number: 'RI/QA/D/01', title: 'LIST OF MEASURUING INSTRUMENTS', category: 'QA', document_type: 'D', company_id: companyId, status: 'ACTIVE', created_at: new Date(), updated_at: new Date() },
      { document_number: 'RI/QA/D/02', title: 'SAMPLING PLAN', category: 'QA', document_type: 'D', company_id: companyId, status: 'ACTIVE', created_at: new Date(), updated_at: new Date() },
      { document_number: 'RI/QA/D/03', title: 'LIST OF CUSTOMER DRAWING', category: 'QA', document_type: 'D', company_id: companyId, status: 'ACTIVE', created_at: new Date(), updated_at: new Date() },
      { document_number: 'RI/QA/F/01', title: 'INCOMING INSPECTION REPORT', category: 'QA', document_type: 'F', company_id: companyId, status: 'ACTIVE', created_at: new Date(), updated_at: new Date() },
      { document_number: 'RI/QA/F/02', title: 'DIMENSIONAL REPORT', category: 'QA', document_type: 'F', company_id: companyId, status: 'ACTIVE', created_at: new Date(), updated_at: new Date() },
      { document_number: 'RI/QA/F/03', title: 'IN PROCESS CHECK LIST', category: 'QA', document_type: 'F', company_id: companyId, status: 'ACTIVE', created_at: new Date(), updated_at: new Date() },
      { document_number: 'RI/QA/F/04', title: 'WELDING FINAL INSPECTION REPORT', category: 'QA', document_type: 'F', company_id: companyId, status: 'ACTIVE', created_at: new Date(), updated_at: new Date() },
      { document_number: 'RI/QA/F/05', title: 'ASSEMBLY FINAL INSPECTION REPORT', category: 'QA', document_type: 'F', company_id: companyId, status: 'ACTIVE', created_at: new Date(), updated_at: new Date() },
      { document_number: 'RI/QA/F/06', title: 'MEASURING INSTRUMENT HISTORY CARD', category: 'QA', document_type: 'F', company_id: companyId, status: 'ACTIVE', created_at: new Date(), updated_at: new Date() },
      { document_number: 'RI/QA/F/07', title: 'REJECTION / REWORK RECORD', category: 'QA', document_type: 'F', company_id: companyId, status: 'ACTIVE', created_at: new Date(), updated_at: new Date() },
      { document_number: 'RI/QA/F/08', title: 'CUSTOMER COMPLAINT REGISTER', category: 'QA', document_type: 'F', company_id: companyId, status: 'ACTIVE', created_at: new Date(), updated_at: new Date() },
      { document_number: 'RI/QA/F/09', title: 'CORRECTIVE ACTION REPORT', category: 'QA', document_type: 'F', company_id: companyId, status: 'ACTIVE', created_at: new Date(), updated_at: new Date() },
    ];

    // Check if documents already exist to avoid duplicates
    const existingDocs = await queryInterface.sequelize.query(
      `SELECT document_number FROM document WHERE company_id = ${companyId} AND document_number LIKE 'RI/%'`,
      { type: Sequelize.QueryTypes.SELECT }
    );
    
    const existingDocNumbers = existingDocs.map(doc => doc.document_number);
    const documentsToInsert = documents.filter(doc => !existingDocNumbers.includes(doc.document_number));
    
    if (documentsToInsert.length > 0) {
      // Insert all documents using bulkInsert
      await queryInterface.bulkInsert('document', documentsToInsert);
      console.log(`Inserted ${documentsToInsert.length} documents.`);
    } else {
      console.log('All documents already exist. Skipping insertion.');
    }
  },

  async down(queryInterface, Sequelize) {
    // Remove seeded documents
    await queryInterface.bulkDelete('document', {
      document_number: {
        [Sequelize.Op.in]: [
          'RI/MF/D/01', 'RI/MF/F/01', 'RI/MF/F/02', 'RI/MF/F/03', 'RI/MF/F/04', 'RI/MF/F/05',
          'RI/MF/F/06', 'RI/MF/F/07', 'RI/MF/F/08', 'RI/MF/F/09', 'RI/MF/F/10',
          'RI/MKT/F/01', 'RI/MKT/F/02',
          'RI/MN/F/01', 'RI/MN/F/02',
          'RI/PUR/D/01', 'RI/PUR/F/01', 'RI/PUR/F/02', 'RI/PUR/F/03', 'RI/PUR/F/04', 'RI/PUR/F/05',
          'RI/QA/D/01', 'RI/QA/D/02', 'RI/QA/D/03',
          'RI/QA/F/01', 'RI/QA/F/02', 'RI/QA/F/03', 'RI/QA/F/04', 'RI/QA/F/05',
          'RI/QA/F/06', 'RI/QA/F/07', 'RI/QA/F/08', 'RI/QA/F/09'
        ]
      }
    }, {});
  }
};

