'use strict';
const bcrypt = require('bcryptjs');

module.exports = {
  async up(queryInterface, Sequelize) {
    // Get RAM Industries company ID
    const [companies, adminUser] = await Promise.all([
      queryInterface.sequelize.query(
        "SELECT id FROM company WHERE name LIKE '%RAM Industries%' OR code = 'RI' OR code = 'COMP001' LIMIT 1"
      ),
      queryInterface.sequelize.query("SELECT id FROM user WHERE username = 'admin' LIMIT 1")
    ]);

    const companyId = companies[0].length > 0 ? companies[0][0].id : null;
    const adminUserId = adminUser[0].length > 0 ? adminUser[0][0].id : null;

    if (!companyId) {
      console.log('Company not found. Skipping user seeding.');
      return;
    }

    console.log(`Seeding users for company ID: ${companyId}`);

    const defaultPassword = await bcrypt.hash('Password@123', 10);

    const users = [
      // Management
      {
        company_id: companyId,
        username: 'ceo.ram',
        email: 'ceo@ramindustries.com',
        password_hash: defaultPassword,
        first_name: 'Rajesh',
        last_name: 'Kumar',
        phone: '+91-9876543210',
        is_super_admin: false,
        is_active: true,
        is_locked: false,
        status: 'ACTIVE',
        created_at: new Date(),
        updated_at: new Date()
      },
      {
        company_id: companyId,
        username: 'gm.ram',
        email: 'gm@ramindustries.com',
        password_hash: defaultPassword,
        first_name: 'Suresh',
        last_name: 'Sharma',
        phone: '+91-9876543211',
        is_super_admin: false,
        is_active: true,
        is_locked: false,
        status: 'ACTIVE',
        created_at: new Date(),
        updated_at: new Date()
      },
      
      // Production
      {
        company_id: companyId,
        username: 'prod.mgr',
        email: 'prod.mgr@ramindustries.com',
        password_hash: defaultPassword,
        first_name: 'Vikram',
        last_name: 'Singh',
        phone: '+91-9876543212',
        is_super_admin: false,
        is_active: true,
        is_locked: false,
        status: 'ACTIVE',
        created_at: new Date(),
        updated_at: new Date()
      },
      {
        company_id: companyId,
        username: 'prod.sup1',
        email: 'prod.sup1@ramindustries.com',
        password_hash: defaultPassword,
        first_name: 'Amit',
        last_name: 'Patel',
        phone: '+91-9876543213',
        is_super_admin: false,
        is_active: true,
        is_locked: false,
        status: 'ACTIVE',
        created_at: new Date(),
        updated_at: new Date()
      },
      {
        company_id: companyId,
        username: 'prod.op1',
        email: 'prod.op1@ramindustries.com',
        password_hash: defaultPassword,
        first_name: 'Ramesh',
        last_name: 'Yadav',
        phone: '+91-9876543214',
        is_super_admin: false,
        is_active: true,
        is_locked: false,
        status: 'ACTIVE',
        created_at: new Date(),
        updated_at: new Date()
      },
      
      // Welding
      {
        company_id: companyId,
        username: 'weld.sup1',
        email: 'weld.sup1@ramindustries.com',
        password_hash: defaultPassword,
        first_name: 'Mahesh',
        last_name: 'Kumar',
        phone: '+91-9876543215',
        is_super_admin: false,
        is_active: true,
        is_locked: false,
        status: 'ACTIVE',
        created_at: new Date(),
        updated_at: new Date()
      },
      {
        company_id: companyId,
        username: 'welder1',
        email: 'welder1@ramindustries.com',
        password_hash: defaultPassword,
        first_name: 'Sunil',
        last_name: 'Verma',
        phone: '+91-9876543216',
        is_super_admin: false,
        is_active: true,
        is_locked: false,
        status: 'ACTIVE',
        created_at: new Date(),
        updated_at: new Date()
      },
      
      // Assembly
      {
        company_id: companyId,
        username: 'assy.sup1',
        email: 'assy.sup1@ramindustries.com',
        password_hash: defaultPassword,
        first_name: 'Naresh',
        last_name: 'Gupta',
        phone: '+91-9876543217',
        is_super_admin: false,
        is_active: true,
        is_locked: false,
        status: 'ACTIVE',
        created_at: new Date(),
        updated_at: new Date()
      },
      {
        company_id: companyId,
        username: 'assy.op1',
        email: 'assy.op1@ramindustries.com',
        password_hash: defaultPassword,
        first_name: 'Pradeep',
        last_name: 'Mishra',
        phone: '+91-9876543218',
        is_super_admin: false,
        is_active: true,
        is_locked: false,
        status: 'ACTIVE',
        created_at: new Date(),
        updated_at: new Date()
      },
      
      // Quality
      {
        company_id: companyId,
        username: 'qa.mgr',
        email: 'qa.mgr@ramindustries.com',
        password_hash: defaultPassword,
        first_name: 'Anil',
        last_name: 'Joshi',
        phone: '+91-9876543219',
        is_super_admin: false,
        is_active: true,
        is_locked: false,
        status: 'ACTIVE',
        created_at: new Date(),
        updated_at: new Date()
      },
      {
        company_id: companyId,
        username: 'qa.insp1',
        email: 'qa.insp1@ramindustries.com',
        password_hash: defaultPassword,
        first_name: 'Ravi',
        last_name: 'Shankar',
        phone: '+91-9876543220',
        is_super_admin: false,
        is_active: true,
        is_locked: false,
        status: 'ACTIVE',
        created_at: new Date(),
        updated_at: new Date()
      },
      {
        company_id: companyId,
        username: 'qc.insp1',
        email: 'qc.insp1@ramindustries.com',
        password_hash: defaultPassword,
        first_name: 'Kiran',
        last_name: 'Reddy',
        phone: '+91-9876543221',
        is_super_admin: false,
        is_active: true,
        is_locked: false,
        status: 'ACTIVE',
        created_at: new Date(),
        updated_at: new Date()
      },
      
      // Purchase
      {
        company_id: companyId,
        username: 'pur.mgr',
        email: 'pur.mgr@ramindustries.com',
        password_hash: defaultPassword,
        first_name: 'Mohan',
        last_name: 'Das',
        phone: '+91-9876543222',
        is_super_admin: false,
        is_active: true,
        is_locked: false,
        status: 'ACTIVE',
        created_at: new Date(),
        updated_at: new Date()
      },
      {
        company_id: companyId,
        username: 'pur.exec1',
        email: 'pur.exec1@ramindustries.com',
        password_hash: defaultPassword,
        first_name: 'Deepak',
        last_name: 'Agarwal',
        phone: '+91-9876543223',
        is_super_admin: false,
        is_active: true,
        is_locked: false,
        status: 'ACTIVE',
        created_at: new Date(),
        updated_at: new Date()
      },
      {
        company_id: companyId,
        username: 'pur.asst1',
        email: 'pur.asst1@ramindustries.com',
        password_hash: defaultPassword,
        first_name: 'Sandeep',
        last_name: 'Malhotra',
        phone: '+91-9876543224',
        is_super_admin: false,
        is_active: true,
        is_locked: false,
        status: 'ACTIVE',
        created_at: new Date(),
        updated_at: new Date()
      },
      
      // Stores
      {
        company_id: companyId,
        username: 'store.keeper1',
        email: 'store.keeper1@ramindustries.com',
        password_hash: defaultPassword,
        first_name: 'Raj',
        last_name: 'Bansal',
        phone: '+91-9876543225',
        is_super_admin: false,
        is_active: true,
        is_locked: false,
        status: 'ACTIVE',
        created_at: new Date(),
        updated_at: new Date()
      },
      {
        company_id: companyId,
        username: 'store.asst1',
        email: 'store.asst1@ramindustries.com',
        password_hash: defaultPassword,
        first_name: 'Vijay',
        last_name: 'Chauhan',
        phone: '+91-9876543226',
        is_super_admin: false,
        is_active: true,
        is_locked: false,
        status: 'ACTIVE',
        created_at: new Date(),
        updated_at: new Date()
      },
      
      // Sales
      {
        company_id: companyId,
        username: 'sales.mgr',
        email: 'sales.mgr@ramindustries.com',
        password_hash: defaultPassword,
        first_name: 'Rahul',
        last_name: 'Mehta',
        phone: '+91-9876543227',
        is_super_admin: false,
        is_active: true,
        is_locked: false,
        status: 'ACTIVE',
        created_at: new Date(),
        updated_at: new Date()
      },
      {
        company_id: companyId,
        username: 'sales.exec1',
        email: 'sales.exec1@ramindustries.com',
        password_hash: defaultPassword,
        first_name: 'Arjun',
        last_name: 'Kapoor',
        phone: '+91-9876543228',
        is_super_admin: false,
        is_active: true,
        is_locked: false,
        status: 'ACTIVE',
        created_at: new Date(),
        updated_at: new Date()
      },
      {
        company_id: companyId,
        username: 'sales.asst1',
        email: 'sales.asst1@ramindustries.com',
        password_hash: defaultPassword,
        first_name: 'Karan',
        last_name: 'Sethi',
        phone: '+91-9876543229',
        is_super_admin: false,
        is_active: true,
        is_locked: false,
        status: 'ACTIVE',
        created_at: new Date(),
        updated_at: new Date()
      },
      
      // Engineering
      {
        company_id: companyId,
        username: 'eng.mgr',
        email: 'eng.mgr@ramindustries.com',
        password_hash: defaultPassword,
        first_name: 'Sachin',
        last_name: 'Iyer',
        phone: '+91-9876543230',
        is_super_admin: false,
        is_active: true,
        is_locked: false,
        status: 'ACTIVE',
        created_at: new Date(),
        updated_at: new Date()
      },
      {
        company_id: companyId,
        username: 'design.eng1',
        email: 'design.eng1@ramindustries.com',
        password_hash: defaultPassword,
        first_name: 'Priya',
        last_name: 'Nair',
        phone: '+91-9876543231',
        is_super_admin: false,
        is_active: true,
        is_locked: false,
        status: 'ACTIVE',
        created_at: new Date(),
        updated_at: new Date()
      },
      {
        company_id: companyId,
        username: 'process.eng1',
        email: 'process.eng1@ramindustries.com',
        password_hash: defaultPassword,
        first_name: 'Aditya',
        last_name: 'Menon',
        phone: '+91-9876543232',
        is_super_admin: false,
        is_active: true,
        is_locked: false,
        status: 'ACTIVE',
        created_at: new Date(),
        updated_at: new Date()
      },
      
      // Finance
      {
        company_id: companyId,
        username: 'fin.mgr',
        email: 'fin.mgr@ramindustries.com',
        password_hash: defaultPassword,
        first_name: 'Nikhil',
        last_name: 'Pillai',
        phone: '+91-9876543233',
        is_super_admin: false,
        is_active: true,
        is_locked: false,
        status: 'ACTIVE',
        created_at: new Date(),
        updated_at: new Date()
      },
      {
        company_id: companyId,
        username: 'accountant1',
        email: 'accountant1@ramindustries.com',
        password_hash: defaultPassword,
        first_name: 'Lakshmi',
        last_name: 'Rao',
        phone: '+91-9876543234',
        is_super_admin: false,
        is_active: true,
        is_locked: false,
        status: 'ACTIVE',
        created_at: new Date(),
        updated_at: new Date()
      },
      {
        company_id: companyId,
        username: 'acc.asst1',
        email: 'acc.asst1@ramindustries.com',
        password_hash: defaultPassword,
        first_name: 'Ganesh',
        last_name: 'Krishnan',
        phone: '+91-9876543235',
        is_super_admin: false,
        is_active: true,
        is_locked: false,
        status: 'ACTIVE',
        created_at: new Date(),
        updated_at: new Date()
      },
      
      // HR
      {
        company_id: companyId,
        username: 'hr.mgr',
        email: 'hr.mgr@ramindustries.com',
        password_hash: defaultPassword,
        first_name: 'Meera',
        last_name: 'Venkatesh',
        phone: '+91-9876543236',
        is_super_admin: false,
        is_active: true,
        is_locked: false,
        status: 'ACTIVE',
        created_at: new Date(),
        updated_at: new Date()
      },
      {
        company_id: companyId,
        username: 'hr.exec1',
        email: 'hr.exec1@ramindustries.com',
        password_hash: defaultPassword,
        first_name: 'Divya',
        last_name: 'Subramanian',
        phone: '+91-9876543237',
        is_super_admin: false,
        is_active: true,
        is_locked: false,
        status: 'ACTIVE',
        created_at: new Date(),
        updated_at: new Date()
      },
      
      // IT
      {
        company_id: companyId,
        username: 'it.admin',
        email: 'it.admin@ramindustries.com',
        password_hash: defaultPassword,
        first_name: 'Rohit',
        last_name: 'Sundaram',
        phone: '+91-9876543238',
        is_super_admin: false,
        is_active: true,
        is_locked: false,
        status: 'ACTIVE',
        created_at: new Date(),
        updated_at: new Date()
      },
      
      // Maintenance
      {
        company_id: companyId,
        username: 'maint.sup1',
        email: 'maint.sup1@ramindustries.com',
        password_hash: defaultPassword,
        first_name: 'Bharat',
        last_name: 'Narayanan',
        phone: '+91-9876543239',
        is_super_admin: false,
        is_active: true,
        is_locked: false,
        status: 'ACTIVE',
        created_at: new Date(),
        updated_at: new Date()
      },
      {
        company_id: companyId,
        username: 'maint.tech1',
        email: 'maint.tech1@ramindustries.com',
        password_hash: defaultPassword,
        first_name: 'Suresh',
        last_name: 'Krishnamurthy',
        phone: '+91-9876543240',
        is_super_admin: false,
        is_active: true,
        is_locked: false,
        status: 'ACTIVE',
        created_at: new Date(),
        updated_at: new Date()
      }
    ];

    await queryInterface.bulkInsert('user', users, {
      ignoreDuplicates: true
    });

    console.log(`Inserted ${users.length} users`);
  },

  async down(queryInterface, Sequelize) {
    await queryInterface.bulkDelete('user', {
      username: [
        'ceo.ram', 'gm.ram', 'prod.mgr', 'prod.sup1', 'prod.op1',
        'weld.sup1', 'welder1', 'assy.sup1', 'assy.op1',
        'qa.mgr', 'qa.insp1', 'qc.insp1',
        'pur.mgr', 'pur.exec1', 'pur.asst1',
        'store.keeper1', 'store.asst1',
        'sales.mgr', 'sales.exec1', 'sales.asst1',
        'eng.mgr', 'design.eng1', 'process.eng1',
        'fin.mgr', 'accountant1', 'acc.asst1',
        'hr.mgr', 'hr.exec1',
        'it.admin',
        'maint.sup1', 'maint.tech1'
      ]
    }, {});
  }
};

