'use strict';

module.exports = {
  async up(queryInterface, Sequelize) {
    // Get RAM Industries company ID
    const [companies, users] = 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 userId = users[0].length > 0 ? users[0][0].id : null;

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

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

    const roles = [
      // Management Roles
      {
        company_id: companyId,
        role_code: 'CEO',
        role_name: 'Chief Executive Officer',
        description: 'Top management - Full access to all modules',
        is_system_role: false,
        status: 'ACTIVE',
        created_by: userId,
        created_at: new Date(),
        updated_at: new Date()
      },
      {
        company_id: companyId,
        role_code: 'GM',
        role_name: 'General Manager',
        description: 'General Manager - Full operational access',
        is_system_role: false,
        status: 'ACTIVE',
        created_by: userId,
        created_at: new Date(),
        updated_at: new Date()
      },
      {
        company_id: companyId,
        role_code: 'PROD_MGR',
        role_name: 'Production Manager',
        description: 'Production department manager',
        is_system_role: false,
        status: 'ACTIVE',
        created_by: userId,
        created_at: new Date(),
        updated_at: new Date()
      },
      {
        company_id: companyId,
        role_code: 'PUR_MGR',
        role_name: 'Purchase Manager',
        description: 'Purchase department manager',
        is_system_role: false,
        status: 'ACTIVE',
        created_by: userId,
        created_at: new Date(),
        updated_at: new Date()
      },
      {
        company_id: companyId,
        role_code: 'SALES_MGR',
        role_name: 'Sales Manager',
        description: 'Sales department manager',
        is_system_role: false,
        status: 'ACTIVE',
        created_by: userId,
        created_at: new Date(),
        updated_at: new Date()
      },
      {
        company_id: companyId,
        role_code: 'QA_MGR',
        role_name: 'Quality Assurance Manager',
        description: 'QA department manager',
        is_system_role: false,
        status: 'ACTIVE',
        created_by: userId,
        created_at: new Date(),
        updated_at: new Date()
      },
      {
        company_id: companyId,
        role_code: 'FIN_MGR',
        role_name: 'Finance Manager',
        description: 'Finance department manager',
        is_system_role: false,
        status: 'ACTIVE',
        created_by: userId,
        created_at: new Date(),
        updated_at: new Date()
      },
      {
        company_id: companyId,
        role_code: 'HR_MGR',
        role_name: 'HR Manager',
        description: 'Human Resources department manager',
        is_system_role: false,
        status: 'ACTIVE',
        created_by: userId,
        created_at: new Date(),
        updated_at: new Date()
      },
      {
        company_id: companyId,
        role_code: 'ENG_MGR',
        role_name: 'Engineering Manager',
        description: 'Engineering department manager',
        is_system_role: false,
        status: 'ACTIVE',
        created_by: userId,
        created_at: new Date(),
        updated_at: new Date()
      },
      
      // Production Roles
      {
        company_id: companyId,
        role_code: 'PROD_SUPERVISOR',
        role_name: 'Production Supervisor',
        description: 'Production floor supervisor',
        is_system_role: false,
        status: 'ACTIVE',
        created_by: userId,
        created_at: new Date(),
        updated_at: new Date()
      },
      {
        company_id: companyId,
        role_code: 'PROD_OPERATOR',
        role_name: 'Production Operator',
        description: 'Production operator/worker',
        is_system_role: false,
        status: 'ACTIVE',
        created_by: userId,
        created_at: new Date(),
        updated_at: new Date()
      },
      {
        company_id: companyId,
        role_code: 'WELD_SUPERVISOR',
        role_name: 'Welding Supervisor',
        description: 'Welding department supervisor',
        is_system_role: false,
        status: 'ACTIVE',
        created_by: userId,
        created_at: new Date(),
        updated_at: new Date()
      },
      {
        company_id: companyId,
        role_code: 'WELDER',
        role_name: 'Welder',
        description: 'Welding operator',
        is_system_role: false,
        status: 'ACTIVE',
        created_by: userId,
        created_at: new Date(),
        updated_at: new Date()
      },
      {
        company_id: companyId,
        role_code: 'ASSY_SUPERVISOR',
        role_name: 'Assembly Supervisor',
        description: 'Assembly department supervisor',
        is_system_role: false,
        status: 'ACTIVE',
        created_by: userId,
        created_at: new Date(),
        updated_at: new Date()
      },
      {
        company_id: companyId,
        role_code: 'ASSY_OPERATOR',
        role_name: 'Assembly Operator',
        description: 'Assembly operator',
        is_system_role: false,
        status: 'ACTIVE',
        created_by: userId,
        created_at: new Date(),
        updated_at: new Date()
      },
      
      // Quality Roles
      {
        company_id: companyId,
        role_code: 'QA_INSPECTOR',
        role_name: 'QA Inspector',
        description: 'Quality assurance inspector',
        is_system_role: false,
        status: 'ACTIVE',
        created_by: userId,
        created_at: new Date(),
        updated_at: new Date()
      },
      {
        company_id: companyId,
        role_code: 'QC_INSPECTOR',
        role_name: 'QC Inspector',
        description: 'Quality control inspector',
        is_system_role: false,
        status: 'ACTIVE',
        created_by: userId,
        created_at: new Date(),
        updated_at: new Date()
      },
      
      // Purchase Roles
      {
        company_id: companyId,
        role_code: 'PUR_EXECUTIVE',
        role_name: 'Purchase Executive',
        description: 'Purchase executive',
        is_system_role: false,
        status: 'ACTIVE',
        created_by: userId,
        created_at: new Date(),
        updated_at: new Date()
      },
      {
        company_id: companyId,
        role_code: 'PUR_ASSISTANT',
        role_name: 'Purchase Assistant',
        description: 'Purchase assistant',
        is_system_role: false,
        status: 'ACTIVE',
        created_by: userId,
        created_at: new Date(),
        updated_at: new Date()
      },
      
      // Stores & Inventory Roles
      {
        company_id: companyId,
        role_code: 'STORE_KEEPER',
        role_name: 'Store Keeper',
        description: 'Store keeper/inventory manager',
        is_system_role: false,
        status: 'ACTIVE',
        created_by: userId,
        created_at: new Date(),
        updated_at: new Date()
      },
      {
        company_id: companyId,
        role_code: 'STORE_ASSISTANT',
        role_name: 'Store Assistant',
        description: 'Store assistant',
        is_system_role: false,
        status: 'ACTIVE',
        created_by: userId,
        created_at: new Date(),
        updated_at: new Date()
      },
      
      // Sales Roles
      {
        company_id: companyId,
        role_code: 'SALES_EXECUTIVE',
        role_name: 'Sales Executive',
        description: 'Sales executive',
        is_system_role: false,
        status: 'ACTIVE',
        created_by: userId,
        created_at: new Date(),
        updated_at: new Date()
      },
      {
        company_id: companyId,
        role_code: 'SALES_ASSISTANT',
        role_name: 'Sales Assistant',
        description: 'Sales assistant',
        is_system_role: false,
        status: 'ACTIVE',
        created_by: userId,
        created_at: new Date(),
        updated_at: new Date()
      },
      
      // Engineering Roles
      {
        company_id: companyId,
        role_code: 'DESIGN_ENGINEER',
        role_name: 'Design Engineer',
        description: 'Design engineer',
        is_system_role: false,
        status: 'ACTIVE',
        created_by: userId,
        created_at: new Date(),
        updated_at: new Date()
      },
      {
        company_id: companyId,
        role_code: 'PROCESS_ENGINEER',
        role_name: 'Process Engineer',
        description: 'Process engineer',
        is_system_role: false,
        status: 'ACTIVE',
        created_by: userId,
        created_at: new Date(),
        updated_at: new Date()
      },
      
      // Finance Roles
      {
        company_id: companyId,
        role_code: 'ACCOUNTANT',
        role_name: 'Accountant',
        description: 'Accountant',
        is_system_role: false,
        status: 'ACTIVE',
        created_by: userId,
        created_at: new Date(),
        updated_at: new Date()
      },
      {
        company_id: companyId,
        role_code: 'ACCOUNTS_ASSISTANT',
        role_name: 'Accounts Assistant',
        description: 'Accounts assistant',
        is_system_role: false,
        status: 'ACTIVE',
        created_by: userId,
        created_at: new Date(),
        updated_at: new Date()
      },
      
      // HR Roles
      {
        company_id: companyId,
        role_code: 'HR_EXECUTIVE',
        role_name: 'HR Executive',
        description: 'HR executive',
        is_system_role: false,
        status: 'ACTIVE',
        created_by: userId,
        created_at: new Date(),
        updated_at: new Date()
      },
      {
        company_id: companyId,
        role_code: 'HR_ASSISTANT',
        role_name: 'HR Assistant',
        description: 'HR assistant',
        is_system_role: false,
        status: 'ACTIVE',
        created_by: userId,
        created_at: new Date(),
        updated_at: new Date()
      },
      
      // IT Roles
      {
        company_id: companyId,
        role_code: 'IT_ADMIN',
        role_name: 'IT Administrator',
        description: 'IT administrator',
        is_system_role: false,
        status: 'ACTIVE',
        created_by: userId,
        created_at: new Date(),
        updated_at: new Date()
      },
      
      // Maintenance Roles
      {
        company_id: companyId,
        role_code: 'MAINT_SUPERVISOR',
        role_name: 'Maintenance Supervisor',
        description: 'Maintenance supervisor',
        is_system_role: false,
        status: 'ACTIVE',
        created_by: userId,
        created_at: new Date(),
        updated_at: new Date()
      },
      {
        company_id: companyId,
        role_code: 'MAINT_TECHNICIAN',
        role_name: 'Maintenance Technician',
        description: 'Maintenance technician',
        is_system_role: false,
        status: 'ACTIVE',
        created_by: userId,
        created_at: new Date(),
        updated_at: new Date()
      }
    ];

    await queryInterface.bulkInsert('role', roles, {
      ignoreDuplicates: true
    });
  },

  async down(queryInterface, Sequelize) {
    await queryInterface.bulkDelete('role', {
      role_code: [
        'CEO', 'GM', 'PROD_MGR', 'PUR_MGR', 'SALES_MGR', 'QA_MGR', 'FIN_MGR', 'HR_MGR', 'ENG_MGR',
        'PROD_SUPERVISOR', 'PROD_OPERATOR', 'WELD_SUPERVISOR', 'WELDER', 'ASSY_SUPERVISOR', 'ASSY_OPERATOR',
        'QA_INSPECTOR', 'QC_INSPECTOR',
        'PUR_EXECUTIVE', 'PUR_ASSISTANT',
        'STORE_KEEPER', 'STORE_ASSISTANT',
        'SALES_EXECUTIVE', 'SALES_ASSISTANT',
        'DESIGN_ENGINEER', 'PROCESS_ENGINEER',
        'ACCOUNTANT', 'ACCOUNTS_ASSISTANT',
        'HR_EXECUTIVE', 'HR_ASSISTANT',
        'IT_ADMIN',
        'MAINT_SUPERVISOR', 'MAINT_TECHNICIAN'
      ]
    }, {});
  }
};

