'use strict';

module.exports = {
  up: async (queryInterface, Sequelize) => {
    await queryInterface.createTable('material_thickness', {
      id: {
        type: Sequelize.INTEGER,
        primaryKey: true,
        autoIncrement: true,
        allowNull: false
      },
      company_id: {
        type: Sequelize.INTEGER,
        allowNull: false,
        references: {
          model: 'company',
          key: 'id'
        },
        onUpdate: 'CASCADE',
        onDelete: 'RESTRICT'
      },
      thickness_code: {
        type: Sequelize.STRING(20),
        allowNull: false,
        comment: 'Unique code for thickness (e.g., T1.0, T1.5, T2.0)'
      },
      thickness_value: {
        type: Sequelize.DECIMAL(10, 3),
        allowNull: false,
        comment: 'Thickness value in mm'
      },
      thickness_unit: {
        type: Sequelize.STRING(10),
        allowNull: false,
        defaultValue: 'mm',
        comment: 'Unit of measurement (default: mm)'
      },
      description: {
        type: Sequelize.STRING(200),
        allowNull: true,
        comment: 'Description (e.g., 1.0mm thickness)'
      },
      is_active: {
        type: Sequelize.BOOLEAN,
        allowNull: false,
        defaultValue: true
      },
      sort_order: {
        type: Sequelize.INTEGER,
        allowNull: true,
        comment: 'Display order in dropdowns'
      },
      created_by: {
        type: Sequelize.INTEGER,
        allowNull: true,
        references: {
          model: 'user',
          key: 'id'
        },
        onUpdate: 'CASCADE',
        onDelete: 'SET NULL'
      },
      updated_by: {
        type: Sequelize.INTEGER,
        allowNull: true,
        references: {
          model: 'user',
          key: 'id'
        },
        onUpdate: 'CASCADE',
        onDelete: 'SET NULL'
      },
      created_at: {
        type: Sequelize.DATE,
        allowNull: false,
        defaultValue: Sequelize.literal('CURRENT_TIMESTAMP')
      },
      updated_at: {
        type: Sequelize.DATE,
        allowNull: false,
        defaultValue: Sequelize.literal('CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP')
      }
    });

    // Add indexes
    await queryInterface.addIndex('material_thickness', ['company_id', 'thickness_code'], {
      unique: true,
      name: 'uk_thickness_company_code'
    });
    await queryInterface.addIndex('material_thickness', ['company_id', 'is_active'], {
      name: 'idx_thickness_company_active'
    });
    await queryInterface.addIndex('material_thickness', ['thickness_value'], {
      name: 'idx_thickness_value'
    });

    // Insert default thickness values
    await queryInterface.bulkInsert('material_thickness', [
      { company_id: 1, thickness_code: 'T0.5', thickness_value: 0.500, thickness_unit: 'mm', description: '0.5mm thickness', is_active: true, sort_order: 1, created_at: new Date(), updated_at: new Date() },
      { company_id: 1, thickness_code: 'T0.8', thickness_value: 0.800, thickness_unit: 'mm', description: '0.8mm thickness', is_active: true, sort_order: 2, created_at: new Date(), updated_at: new Date() },
      { company_id: 1, thickness_code: 'T1.0', thickness_value: 1.000, thickness_unit: 'mm', description: '1.0mm thickness', is_active: true, sort_order: 3, created_at: new Date(), updated_at: new Date() },
      { company_id: 1, thickness_code: 'T1.2', thickness_value: 1.200, thickness_unit: 'mm', description: '1.2mm thickness', is_active: true, sort_order: 4, created_at: new Date(), updated_at: new Date() },
      { company_id: 1, thickness_code: 'T1.5', thickness_value: 1.500, thickness_unit: 'mm', description: '1.5mm thickness', is_active: true, sort_order: 5, created_at: new Date(), updated_at: new Date() },
      { company_id: 1, thickness_code: 'T2.0', thickness_value: 2.000, thickness_unit: 'mm', description: '2.0mm thickness', is_active: true, sort_order: 6, created_at: new Date(), updated_at: new Date() },
      { company_id: 1, thickness_code: 'T2.5', thickness_value: 2.500, thickness_unit: 'mm', description: '2.5mm thickness', is_active: true, sort_order: 7, created_at: new Date(), updated_at: new Date() },
      { company_id: 1, thickness_code: 'T3.0', thickness_value: 3.000, thickness_unit: 'mm', description: '3.0mm thickness', is_active: true, sort_order: 8, created_at: new Date(), updated_at: new Date() },
      { company_id: 1, thickness_code: 'T4.0', thickness_value: 4.000, thickness_unit: 'mm', description: '4.0mm thickness', is_active: true, sort_order: 9, created_at: new Date(), updated_at: new Date() },
      { company_id: 1, thickness_code: 'T5.0', thickness_value: 5.000, thickness_unit: 'mm', description: '5.0mm thickness', is_active: true, sort_order: 10, created_at: new Date(), updated_at: new Date() },
      { company_id: 1, thickness_code: 'T6.0', thickness_value: 6.000, thickness_unit: 'mm', description: '6.0mm thickness', is_active: true, sort_order: 11, created_at: new Date(), updated_at: new Date() },
      { company_id: 1, thickness_code: 'T8.0', thickness_value: 8.000, thickness_unit: 'mm', description: '8.0mm thickness', is_active: true, sort_order: 12, created_at: new Date(), updated_at: new Date() },
      { company_id: 1, thickness_code: 'T10.0', thickness_value: 10.000, thickness_unit: 'mm', description: '10.0mm thickness', is_active: true, sort_order: 13, created_at: new Date(), updated_at: new Date() },
      { company_id: 1, thickness_code: 'T12.0', thickness_value: 12.000, thickness_unit: 'mm', description: '12.0mm thickness', is_active: true, sort_order: 14, created_at: new Date(), updated_at: new Date() },
      { company_id: 1, thickness_code: 'T15.0', thickness_value: 15.000, thickness_unit: 'mm', description: '15.0mm thickness', is_active: true, sort_order: 15, created_at: new Date(), updated_at: new Date() },
      { company_id: 1, thickness_code: 'T20.0', thickness_value: 20.000, thickness_unit: 'mm', description: '20.0mm thickness', is_active: true, sort_order: 16, created_at: new Date(), updated_at: new Date() }
    ]);
  },

  down: async (queryInterface, Sequelize) => {
    await queryInterface.dropTable('material_thickness');
  }
};



