'use strict';

module.exports = {
  up: async (queryInterface, Sequelize) => {
    await queryInterface.createTable('material_grade', {
      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'
      },
      grade_code: {
        type: Sequelize.STRING(20),
        allowNull: false,
        comment: 'Unique code for material grade (e.g., MS, GI, SS304)'
      },
      grade_name: {
        type: Sequelize.STRING(100),
        allowNull: false,
        comment: 'Name of material grade (e.g., Mild Steel, Galvanized Iron)'
      },
      description: {
        type: Sequelize.STRING(500),
        allowNull: true,
        comment: 'Detailed description of the grade'
      },
      specification: {
        type: Sequelize.STRING(200),
        allowNull: true,
        comment: 'Industry specification (e.g., IS 2062, ASTM A36)'
      },
      properties: {
        type: Sequelize.TEXT,
        allowNull: true,
        comment: 'Material properties in JSON format'
      },
      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_grade', ['company_id', 'grade_code'], {
      unique: true,
      name: 'uk_grade_company_code'
    });
    await queryInterface.addIndex('material_grade', ['company_id', 'is_active'], {
      name: 'idx_grade_company_active'
    });

    // Insert default material grades
    await queryInterface.bulkInsert('material_grade', [
      { company_id: 1, grade_code: 'MS', grade_name: 'Mild Steel', description: 'Low carbon steel, easy to weld and form', specification: 'IS 2062', is_active: true, sort_order: 1, created_at: new Date(), updated_at: new Date() },
      { company_id: 1, grade_code: 'GI', grade_name: 'Galvanized Iron', description: 'Zinc-coated steel for corrosion resistance', specification: 'IS 277', is_active: true, sort_order: 2, created_at: new Date(), updated_at: new Date() },
      { company_id: 1, grade_code: 'SS304', grade_name: 'Stainless Steel 304', description: 'Austenitic stainless steel, general purpose', specification: 'ASTM A240', is_active: true, sort_order: 3, created_at: new Date(), updated_at: new Date() },
      { company_id: 1, grade_code: 'SS316', grade_name: 'Stainless Steel 316', description: 'Austenitic stainless steel, superior corrosion resistance', specification: 'ASTM A240', is_active: true, sort_order: 4, created_at: new Date(), updated_at: new Date() },
      { company_id: 1, grade_code: 'AL', grade_name: 'Aluminum', description: 'Lightweight metal, good conductivity', specification: 'IS 737', is_active: true, sort_order: 5, created_at: new Date(), updated_at: new Date() },
      { company_id: 1, grade_code: 'CRCA', grade_name: 'Cold Rolled Close Annealed', description: 'Cold rolled steel, smooth finish', specification: 'IS 513', is_active: true, sort_order: 6, created_at: new Date(), updated_at: new Date() },
      { company_id: 1, grade_code: 'HRCA', grade_name: 'Hot Rolled Close Annealed', description: 'Hot rolled steel, rough finish', specification: 'IS 1079', is_active: true, sort_order: 7, created_at: new Date(), updated_at: new Date() },
      { company_id: 1, grade_code: 'CU', grade_name: 'Copper', description: 'High conductivity metal', specification: 'IS 191', is_active: true, sort_order: 8, created_at: new Date(), updated_at: new Date() },
      { company_id: 1, grade_code: 'BR', grade_name: 'Brass', description: 'Copper-zinc alloy', specification: 'IS 319', is_active: true, sort_order: 9, created_at: new Date(), updated_at: new Date() },
      { company_id: 1, grade_code: 'SS201', grade_name: 'Stainless Steel 201', description: 'Austenitic stainless steel, economical', specification: 'ASTM A240', is_active: true, sort_order: 10, created_at: new Date(), updated_at: new Date() }
    ]);
  },

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



