'use strict';

module.exports = {
  async up(queryInterface, Sequelize) {
    await queryInterface.createTable('qa_master', {
      id: { type: Sequelize.INTEGER, primaryKey: true, autoIncrement: true },
      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'
      },
      operation_name_id: {
        type: Sequelize.INTEGER,
        allowNull: false,
        references: { model: 'operation_name', key: 'id' },
        onUpdate: 'CASCADE',
        onDelete: 'RESTRICT'
      },
      operation_no: { type: Sequelize.INTEGER, allowNull: true },
      bom_id: {
        type: Sequelize.INTEGER,
        allowNull: false,
        references: { model: 'bom_header', key: 'id' },
        onUpdate: 'CASCADE',
        onDelete: 'RESTRICT'
      },
      item_material_id: {
        type: Sequelize.INTEGER,
        allowNull: false,
        references: { model: 'material', key: 'id' },
        onUpdate: 'CASCADE',
        onDelete: 'RESTRICT'
      },
      qa_menus: {
        type: Sequelize.TEXT,
        allowNull: true,
        comment: 'JSON array of selected QA menu keys'
      },
      status: {
        type: Sequelize.ENUM('ACTIVE', 'INACTIVE'),
        allowNull: false,
        defaultValue: 'ACTIVE'
      },
      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')
      }
    });

    await queryInterface.addIndex('qa_master', ['company_id']);
    await queryInterface.addIndex('qa_master', ['plant_id']);
    await queryInterface.addIndex('qa_master', ['operation_name_id']);
    await queryInterface.addIndex('qa_master', ['bom_id']);
    await queryInterface.addIndex('qa_master', ['item_material_id']);
    await queryInterface.addIndex('qa_master', ['status']);

    await queryInterface.createTable('qa_master_component', {
      id: { type: Sequelize.INTEGER, primaryKey: true, autoIncrement: true },
      qa_master_id: {
        type: Sequelize.INTEGER,
        allowNull: false,
        references: { model: 'qa_master', 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'
      },
      line_no: { type: Sequelize.INTEGER, allowNull: false },
      material_id: {
        type: Sequelize.INTEGER,
        allowNull: false,
        references: { model: 'material', key: 'id' },
        onUpdate: 'CASCADE',
        onDelete: 'RESTRICT'
      },
      width: { type: Sequelize.DECIMAL(15, 3), allowNull: true },
      length: { type: Sequelize.DECIMAL(15, 3), allowNull: true },
      height: { type: Sequelize.DECIMAL(15, 3), allowNull: true },
      thickness: { type: Sequelize.DECIMAL(15, 3), allowNull: true },
      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')
      }
    });

    await queryInterface.addIndex('qa_master_component', ['qa_master_id']);
    await queryInterface.addIndex('qa_master_component', ['bom_component_id']);
    await queryInterface.addIndex('qa_master_component', ['material_id']);
    await queryInterface.addIndex('qa_master_component', ['line_no']);
  },

  async down(queryInterface) {
    await queryInterface.dropTable('qa_master_component');
    await queryInterface.dropTable('qa_master');
  }
};

