'use strict';

module.exports = {
  async up(queryInterface, Sequelize) {
    // Get all companies
    const [companies] = await queryInterface.sequelize.query(
      "SELECT id FROM company"
    );

    // Get admin user for created_by
    const [users] = await queryInterface.sequelize.query(
      "SELECT id FROM user WHERE username = 'admin' LIMIT 1"
    );

    const userId = users.length > 0 ? users[0].id : null;

    // Default vendor categories
    const vendorCategories = [
      {
        category_code: 'REGULAR',
        category_name: 'Regular Vendor',
        description: 'Regular vendors for standard purchases',
        gst_reduction_percentage: 0.00,
        applies_to_raw_materials: true,
        applies_to_semi_finished: true,
        is_active: true
      },
      {
        category_code: 'SUB_CONTRACTOR',
        category_name: 'Sub Contractor',
        description: 'Sub contractors for job work and outsourcing',
        gst_reduction_percentage: 6.00,
        applies_to_raw_materials: true,
        applies_to_semi_finished: true,
        is_active: true
      },
      {
        category_code: 'JOB_WORKER',
        category_name: 'Job Worker',
        description: 'Job workers for processing and manufacturing',
        gst_reduction_percentage: 6.00,
        applies_to_raw_materials: true,
        applies_to_semi_finished: true,
        is_active: true
      },
      {
        category_code: 'CUSTOMER',
        category_name: 'Customer',
        description: 'Customer category (for business partners who are customers)',
        gst_reduction_percentage: 0.00,
        applies_to_raw_materials: false,
        applies_to_semi_finished: false,
        is_active: true
      }
    ];

    // Insert vendor categories for each company
    for (const company of companies) {
      const companyId = company.id;
      
      for (const category of vendorCategories) {
        // Check if vendor category already exists for this company
        const [existing] = await queryInterface.sequelize.query(
          `SELECT id FROM vendor_category WHERE company_id = :companyId AND category_code = :categoryCode LIMIT 1`,
          {
            replacements: { companyId, categoryCode: category.category_code },
            type: Sequelize.QueryTypes.SELECT
          }
        );

        if (!existing || existing.length === 0) {
          await queryInterface.bulkInsert('vendor_category', [{
            company_id: companyId,
            category_code: category.category_code,
            category_name: category.category_name,
            description: category.description,
            gst_reduction_percentage: category.gst_reduction_percentage,
            applies_to_raw_materials: category.applies_to_raw_materials,
            applies_to_semi_finished: category.applies_to_semi_finished,
            is_active: category.is_active,
            created_by: userId,
            created_at: new Date(),
            updated_at: new Date()
          }]);
        }
      }
    }
  },

  async down(queryInterface, Sequelize) {
    await queryInterface.bulkDelete('vendor_category', null, {});
  }
};

