'use strict';

module.exports = {
  up: async (queryInterface, Sequelize) => {
    await queryInterface.createTable('material_sheet_type', {
      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'
      },
      sheet_type_code: {
        type: Sequelize.STRING(20),
        allowNull: false,
        comment: 'Unique code for sheet type (e.g., SHEET, COIL, PLATE)'
      },
      sheet_type_name: {
        type: Sequelize.STRING(100),
        allowNull: false,
        comment: 'Name of sheet type (e.g., Sheet, Coil, Plate)'
      },
      description: {
        type: Sequelize.STRING(500),
        allowNull: true,
        comment: 'Description of sheet type'
      },
      standard_width: {
        type: Sequelize.DECIMAL(10, 3),
        allowNull: true,
        comment: 'Standard width in mm (if applicable)'
      },
      standard_length: {
        type: Sequelize.DECIMAL(10, 3),
        allowNull: true,
        comment: 'Standard length in mm (if applicable)'
      },
      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_sheet_type', ['company_id', 'sheet_type_code'], {
      unique: true,
      name: 'uk_sheet_type_company_code'
    });
    await queryInterface.addIndex('material_sheet_type', ['company_id', 'is_active'], {
      name: 'idx_sheet_type_company_active'
    });

    // Insert default sheet types
    await queryInterface.bulkInsert('material_sheet_type', [
      { company_id: 1, sheet_type_code: 'SHEET', sheet_type_name: 'Sheet', description: 'Flat metal sheet', is_active: true, sort_order: 1, created_at: new Date(), updated_at: new Date() },
      { company_id: 1, sheet_type_code: 'COIL', sheet_type_name: 'Coil', description: 'Rolled metal coil', is_active: true, sort_order: 2, created_at: new Date(), updated_at: new Date() },
      { company_id: 1, sheet_type_code: 'PLATE', sheet_type_name: 'Plate', description: 'Thick metal plate', is_active: true, sort_order: 3, created_at: new Date(), updated_at: new Date() },
      { company_id: 1, sheet_type_code: 'STRIP', sheet_type_name: 'Strip', description: 'Narrow metal strip', is_active: true, sort_order: 4, created_at: new Date(), updated_at: new Date() },
      { company_id: 1, sheet_type_code: 'ROLL', sheet_type_name: 'Roll', description: 'Rolled metal', is_active: true, sort_order: 5, created_at: new Date(), updated_at: new Date() },
      { company_id: 1, sheet_type_code: 'FOIL', sheet_type_name: 'Foil', description: 'Thin metal foil', is_active: true, sort_order: 6, created_at: new Date(), updated_at: new Date() },
      { company_id: 1, sheet_type_code: 'PIPE', sheet_type_name: 'Pipe', description: 'Hollow cylindrical pipe', is_active: true, sort_order: 7, created_at: new Date(), updated_at: new Date() },
      { company_id: 1, sheet_type_code: 'TUBE', sheet_type_name: 'Tube', description: 'Hollow cylindrical tube', is_active: true, sort_order: 8, created_at: new Date(), updated_at: new Date() },
      { company_id: 1, sheet_type_code: 'ANGLE', sheet_type_name: 'Angle', description: 'L-shaped angle section', is_active: true, sort_order: 9, created_at: new Date(), updated_at: new Date() },
      { company_id: 1, sheet_type_code: 'CHANNEL', sheet_type_name: 'Channel', description: 'C-shaped channel section', is_active: true, sort_order: 10, created_at: new Date(), updated_at: new Date() },
      { company_id: 1, sheet_type_code: 'ROUND_BAR', sheet_type_name: 'Round Bar', description: 'Solid round bar', is_active: true, sort_order: 11, created_at: new Date(), updated_at: new Date() },
      { company_id: 1, sheet_type_code: 'FLAT_BAR', sheet_type_name: 'Flat Bar', description: 'Solid flat bar', is_active: true, sort_order: 12, created_at: new Date(), updated_at: new Date() },
      { company_id: 1, sheet_type_code: 'SQUARE_BAR', sheet_type_name: 'Square Bar', description: 'Solid square bar', is_active: true, sort_order: 13, created_at: new Date(), updated_at: new Date() }
    ]);
  },

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



