'use strict';

module.exports = {
  async up(queryInterface, Sequelize) {
    // Get RAM Industries company ID
    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 machines for RAM Industries (company ID: ${companyId})`);
    }

    // Machine data from the image - LIST OF MACHINES from RAM INDUSTRIES
    const machines = [
      // SARA INDUSTRIES Section
      {
        company_id: companyId,
        machine_code: 'LSR 01',
        machine_description: 'LASER HG',
        specification: '3000-3015',
        make: 'MARVEL',
        installed_on: 2017,
        history_card_no: 'HC-07',
        location: 'SARA INDUSTRIES',
        status: 'WORKING',
        created_at: new Date(),
        updated_at: new Date()
      },
      {
        company_id: companyId,
        machine_code: 'PBR 02',
        machine_description: 'PRESS BRAKE 02',
        specification: '100 TON 4000X500',
        make: 'ACCUPRESS',
        installed_on: 2022,
        history_card_no: 'HC-03',
        location: 'SARA INDUSTRIES',
        status: 'WORKING',
        created_at: new Date(),
        updated_at: new Date()
      },
      {
        company_id: companyId,
        machine_code: 'PBR 03',
        machine_description: 'PRESS BRAKE 03',
        specification: '60 TON 2500X500',
        make: 'ACCUPRESS',
        installed_on: 2022,
        history_card_no: 'HC-03',
        location: 'SARA INDUSTRIES',
        status: 'WORKING',
        created_at: new Date(),
        updated_at: new Date()
      },
      {
        company_id: companyId,
        machine_code: 'PP 02',
        machine_description: 'POWER PRESS 02',
        specification: '20TON',
        make: 'AMT',
        installed_on: 2022,
        history_card_no: 'HC-03',
        location: 'SARA INDUSTRIES',
        status: 'WORKING',
        created_at: new Date(),
        updated_at: new Date()
      },
      {
        company_id: companyId,
        machine_code: 'IW 01',
        machine_description: 'HYDRAULIC IRON WORKING',
        specification: '60 TON',
        make: 'SUNRISE',
        installed_on: 2018,
        history_card_no: 'HC-07',
        location: 'SARA INDUSTRIES',
        status: 'WORKING',
        created_at: new Date(),
        updated_at: new Date()
      },
      {
        company_id: companyId,
        machine_code: 'TPM 01',
        machine_description: 'TAPPING MACHINE',
        specification: 'M3-M16',
        make: 'MRCM',
        installed_on: 2019,
        history_card_no: 'HC-06',
        location: 'SARA INDUSTRIES',
        status: 'WORKING',
        created_at: new Date(),
        updated_at: new Date()
      },
      {
        company_id: companyId,
        machine_code: 'SM 01',
        machine_description: 'SHEARING MACHINE',
        specification: '0-4 MM',
        make: 'ANHUI DONGHAI',
        installed_on: 2019,
        history_card_no: 'HC-06',
        location: 'SARA INDUSTRIES',
        status: 'WORKING',
        created_at: new Date(),
        updated_at: new Date()
      },
      {
        company_id: companyId,
        machine_code: 'SPW 01',
        machine_description: 'NUT SPOT WELDING',
        specification: '1.8A',
        make: 'ELECTROWELD',
        installed_on: 2016,
        history_card_no: 'HC-06',
        location: 'SARA INDUSTRIES',
        status: 'WORKING',
        created_at: new Date(),
        updated_at: new Date()
      },
      {
        company_id: companyId,
        machine_code: 'AC 04',
        machine_description: 'SCREW COMPRESSOR',
        specification: '3HP,12BAR',
        make: 'FRANK',
        installed_on: 2018,
        history_card_no: 'HC-07',
        location: 'SARA INDUSTRIES',
        status: 'WORKING',
        created_at: new Date(),
        updated_at: new Date()
      },
      {
        company_id: companyId,
        machine_code: 'TPP 01',
        machine_description: 'TURRET PUNCH PRESS',
        specification: '2500X1250 30TON',
        make: 'JFY',
        installed_on: 2013,
        history_card_no: 'HC-12',
        location: 'SARA INDUSTRIES',
        status: 'WORKING',
        created_at: new Date(),
        updated_at: new Date()
      },
      {
        company_id: companyId,
        machine_code: 'DM 01',
        machine_description: 'BENCH DRILLING MACHINE',
        specification: '1A',
        make: 'MITSUI',
        installed_on: 2010,
        history_card_no: 'HC-15',
        location: 'SARA INDUSTRIES',
        status: 'WORKING',
        created_at: new Date(),
        updated_at: new Date()
      },
      {
        company_id: companyId,
        machine_code: 'PM 01',
        machine_description: 'PRESS MACHINE',
        specification: '15-200+T(415V)',
        make: 'PRESS N PRESSTECH',
        installed_on: 2018,
        history_card_no: 'HC-07',
        location: 'SARA INDUSTRIES',
        status: 'WORKING',
        created_at: new Date(),
        updated_at: new Date()
      },
      
      // RAM INDUSTRIES Section
      {
        company_id: companyId,
        machine_code: 'DW 01',
        machine_description: 'WELDING MACHINE 3',
        specification: '250A',
        make: 'WIM',
        installed_on: 2015,
        history_card_no: 'HC-10',
        location: 'RAM INDUSTRIES',
        status: 'WORKING',
        created_at: new Date(),
        updated_at: new Date()
      },
      {
        company_id: companyId,
        machine_code: 'SW 02',
        machine_description: 'WELDING MACHINE 4',
        specification: '250A',
        make: 'NATIONAL',
        installed_on: 2018,
        history_card_no: 'HC-07',
        location: 'RAM INDUSTRIES',
        status: 'WORKING',
        created_at: new Date(),
        updated_at: new Date()
      },
      {
        company_id: companyId,
        machine_code: 'MW 05',
        machine_description: 'WELDING MACHINE 5',
        specification: '250A',
        make: 'TRANSWELD',
        installed_on: 2016,
        history_card_no: 'HC-06',
        location: 'RAM INDUSTRIES',
        status: 'WORKING',
        created_at: new Date(),
        updated_at: new Date()
      },
      {
        company_id: companyId,
        machine_code: 'MW 01',
        machine_description: 'WELDING MACHINE 6',
        specification: '250A',
        make: 'PANASONIC',
        installed_on: 2017,
        history_card_no: 'HC-08',
        location: 'RAM INDUSTRIES',
        status: 'WORKING',
        created_at: new Date(),
        updated_at: new Date()
      },
      {
        company_id: companyId,
        machine_code: 'MW 06',
        machine_description: 'WELDING MACHINE 7',
        specification: '250A',
        make: 'MICATRONICS',
        installed_on: 2010,
        history_card_no: 'HC-15',
        location: 'RAM INDUSTRIES',
        status: 'WORKING',
        created_at: new Date(),
        updated_at: new Date()
      },
      {
        company_id: companyId,
        machine_code: 'DM 02',
        machine_description: 'BENCH DRILLING MACHINE',
        specification: '1A',
        make: 'MITSUI',
        installed_on: 2010,
        history_card_no: 'HC-15',
        location: 'RAM INDUSTRIES',
        status: 'WORKING',
        created_at: new Date(),
        updated_at: new Date()
      },
      {
        company_id: companyId,
        machine_code: 'LSR 02',
        machine_description: 'LASER WELDING',
        specification: '3.3KW 3000X1500',
        make: 'AMADA',
        installed_on: 2018,
        history_card_no: 'HC-07',
        location: 'RAM INDUSTRIES',
        status: 'WORKING',
        created_at: new Date(),
        updated_at: new Date()
      },
      {
        company_id: companyId,
        machine_code: 'RM 01',
        machine_description: 'ROLLING MACHINE',
        specification: '0-2MM',
        make: 'DURMA',
        installed_on: 2019,
        history_card_no: 'HC-06',
        location: 'RAM INDUSTRIES',
        status: 'WORKING',
        created_at: new Date(),
        updated_at: new Date()
      },
      {
        company_id: companyId,
        machine_code: 'DM 03',
        machine_description: 'BENCH DRILLING MACHINE 01',
        specification: '1A',
        make: 'MITSUI',
        installed_on: 2010,
        history_card_no: 'HC-15',
        location: 'RAM INDUSTRIES',
        status: 'WORKING',
        created_at: new Date(),
        updated_at: new Date()
      },
      {
        company_id: companyId,
        machine_code: 'DM 04',
        machine_description: 'BENCH DRILLING MACHINE 02',
        specification: '1A',
        make: 'MITSUI',
        installed_on: 2010,
        history_card_no: 'HC-15',
        location: 'RAM INDUSTRIES',
        status: 'WORKING',
        created_at: new Date(),
        updated_at: new Date()
      },
      {
        company_id: companyId,
        machine_code: 'AC 01',
        machine_description: 'AIR COMPRESSOR 1',
        specification: '-3HP 12BAR',
        make: 'MEENA COMP',
        installed_on: 2013,
        history_card_no: 'HC-12',
        location: 'RAM INDUSTRIES',
        status: 'WORKING',
        created_at: new Date(),
        updated_at: new Date()
      },
      {
        company_id: companyId,
        machine_code: 'AC 02',
        machine_description: 'AIR COMPRESSOR 2',
        specification: '3HP 12BAR',
        make: 'ASHWATH',
        installed_on: 2018,
        history_card_no: 'HC-07',
        location: 'RAM INDUSTRIES',
        status: 'WORKING',
        created_at: new Date(),
        updated_at: new Date()
      },
      {
        company_id: companyId,
        machine_code: 'PP 01',
        machine_description: 'POWER PRESS 01',
        specification: '20 TON',
        make: 'AMT',
        installed_on: 2014,
        history_card_no: 'HC-11',
        location: 'RAM INDUSTRIES',
        status: 'WORKING',
        created_at: new Date(),
        updated_at: new Date()
      },
      {
        company_id: companyId,
        machine_code: 'RI-01-01',
        machine_description: 'COATING OVEN',
        specification: '0-200',
        make: 'KAVERI',
        installed_on: 2019,
        history_card_no: 'HC-06',
        location: 'RAM INDUSTRIES',
        status: 'WORKING',
        created_at: new Date(),
        updated_at: new Date()
      },
    ];

    // Check if machines already exist to avoid duplicates
    const existingMachines = await queryInterface.sequelize.query(
      `SELECT machine_code FROM machine WHERE company_id = ${companyId} AND machine_code IN (${machines.map(m => `'${m.machine_code}'`).join(',')})`,
      { type: Sequelize.QueryTypes.SELECT }
    );
    
    const existingMachineCodes = existingMachines.map(m => m.machine_code);
    const machinesToInsert = machines.filter(m => !existingMachineCodes.includes(m.machine_code));
    
    if (machinesToInsert.length > 0) {
      // Insert all machines using bulkInsert
      await queryInterface.bulkInsert('machine', machinesToInsert);
      console.log(`Inserted ${machinesToInsert.length} machines.`);
    } else {
      console.log('All machines already exist. Skipping insertion.');
    }
  },

  async down(queryInterface, Sequelize) {
    // Remove seeded machines
    await queryInterface.bulkDelete('machine', {
      machine_code: {
        [Sequelize.Op.in]: [
          'LSR 01', 'PBR 02', 'PBR 03', 'PP 02', 'IW 01', 'TPM 01', 'SM 01', 'SPW 01', 'AC 04', 'TPP 01', 'DM 01', 'PM 01',
          'DW 01', 'SW 02', 'MW 05', 'MW 01', 'MW 06', 'DM 02', 'LSR 02', 'RM 01', 'DM 03', 'DM 04', 'AC 01', 'AC 02', 'PP 01', 'RI-01-01'
        ]
      }
    }, {});
  }
};

