'use strict';

module.exports = {
  async up(queryInterface, Sequelize) {
    await queryInterface.addColumn('production_order', 'is_sent_to_nesting', {
      type: Sequelize.BOOLEAN,
      allowNull: false,
      defaultValue: false
    });

    await queryInterface.addColumn('production_order', 'sent_to_nesting_at', {
      type: Sequelize.DATE,
      allowNull: true
    });

    await queryInterface.addColumn('production_order', 'sent_to_nesting_by', {
      type: Sequelize.INTEGER,
      allowNull: true,
      references: { model: 'user', key: 'id' },
      onUpdate: 'CASCADE',
      onDelete: 'SET NULL'
    });

    await queryInterface.createTable('production_nesting', {
      id: {
        allowNull: false,
        autoIncrement: true,
        primaryKey: true,
        type: Sequelize.INTEGER
      },
      company_id: {
        type: Sequelize.INTEGER,
        allowNull: false,
        references: { model: 'company', key: 'id' },
        onUpdate: 'CASCADE',
        onDelete: 'RESTRICT'
      },
      plant_id: {
        type: Sequelize.INTEGER,
        allowNull: false,
        references: { model: 'plant', key: 'id' },
        onUpdate: 'CASCADE',
        onDelete: 'RESTRICT'
      },
      fiscal_year_id: {
        type: Sequelize.INTEGER,
        allowNull: false,
        references: { model: 'fiscal_year', key: 'id' },
        onUpdate: 'CASCADE',
        onDelete: 'RESTRICT'
      },
      production_order_id: {
        type: Sequelize.INTEGER,
        allowNull: false,
        references: { model: 'production_order', key: 'id' },
        onUpdate: 'CASCADE',
        onDelete: 'CASCADE'
      },
      production_order_component_id: {
        type: Sequelize.INTEGER,
        allowNull: false,
        references: { model: 'production_order_component', key: 'id' },
        onUpdate: 'CASCADE',
        onDelete: 'CASCADE'
      },
      bom_component_id: {
        type: Sequelize.INTEGER,
        allowNull: true,
        references: { model: 'bom_component', key: 'id' },
        onUpdate: 'CASCADE',
        onDelete: 'SET NULL'
      },
      material_id: {
        type: Sequelize.INTEGER,
        allowNull: false,
        references: { model: 'material', key: 'id' },
        onUpdate: 'CASCADE',
        onDelete: 'RESTRICT'
      },
      required_quantity: {
        type: Sequelize.DECIMAL(18, 3),
        allowNull: false
      },
      scrap_percentage: {
        type: Sequelize.DECIMAL(7, 3),
        allowNull: false,
        defaultValue: 0
      },
      scrap_quantity: {
        type: Sequelize.DECIMAL(18, 3),
        allowNull: false,
        defaultValue: 0
      },
      total_required_quantity: {
        type: Sequelize.DECIMAL(18, 3),
        allowNull: false
      },
      thickness: {
        type: Sequelize.DECIMAL(15, 3),
        allowNull: true
      },
      length: {
        type: Sequelize.DECIMAL(15, 3),
        allowNull: true
      },
      width: {
        type: Sequelize.DECIMAL(15, 3),
        allowNull: true
      },
      height: {
        type: Sequelize.DECIMAL(15, 3),
        allowNull: true
      },
      dimension_unit: {
        type: Sequelize.STRING(10),
        allowNull: true
      },
      status: {
        type: Sequelize.ENUM('PENDING', 'IN_PROGRESS', 'COMPLETED', 'CANCELLED'),
        allowNull: false,
        defaultValue: 'PENDING'
      },
      remarks: {
        type: Sequelize.STRING(500),
        allowNull: true
      },
      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: {
        allowNull: false,
        type: Sequelize.DATE,
        defaultValue: Sequelize.literal('CURRENT_TIMESTAMP')
      },
      updated_at: {
        allowNull: false,
        type: Sequelize.DATE,
        defaultValue: Sequelize.literal('CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP')
      }
    });

    await queryInterface.addIndex('production_order', ['is_sent_to_nesting'], { name: 'idx_production_order_is_sent_to_nesting' });
    await queryInterface.addIndex('production_nesting', ['company_id', 'plant_id', 'fiscal_year_id'], { name: 'idx_production_nesting_context' });
    await queryInterface.addIndex('production_nesting', ['production_order_id'], { name: 'idx_production_nesting_po' });
    await queryInterface.addIndex('production_nesting', ['production_order_component_id'], { name: 'idx_production_nesting_po_component' });
    await queryInterface.addIndex('production_nesting', ['material_id'], { name: 'idx_production_nesting_material' });
    await queryInterface.addIndex('production_nesting', ['status'], { name: 'idx_production_nesting_status' });
  },

  async down(queryInterface) {
    await queryInterface.removeIndex('production_nesting', 'idx_production_nesting_status');
    await queryInterface.removeIndex('production_nesting', 'idx_production_nesting_material');
    await queryInterface.removeIndex('production_nesting', 'idx_production_nesting_po_component');
    await queryInterface.removeIndex('production_nesting', 'idx_production_nesting_po');
    await queryInterface.removeIndex('production_nesting', 'idx_production_nesting_context');
    await queryInterface.dropTable('production_nesting');

    await queryInterface.removeIndex('production_order', 'idx_production_order_is_sent_to_nesting');
    await queryInterface.removeColumn('production_order', 'sent_to_nesting_by');
    await queryInterface.removeColumn('production_order', 'sent_to_nesting_at');
    await queryInterface.removeColumn('production_order', 'is_sent_to_nesting');
  }
};
