'use strict';

module.exports = {
  async up(queryInterface, Sequelize) {
    await queryInterface.createTable('welding_visual_inspection_report', {
      id: { type: Sequelize.INTEGER, primaryKey: true, autoIncrement: true, allowNull: false },
      company_id: { type: Sequelize.INTEGER, allowNull: false },
      plant_id: { type: Sequelize.INTEGER, allowNull: false },
      fiscal_year_id: { type: Sequelize.INTEGER, allowNull: false },
      wvir_no: { type: Sequelize.STRING(50), allowNull: false, unique: true },
      report_date: { type: Sequelize.DATEONLY, allowNull: false },
      doc_no: { type: Sequelize.STRING(50), allowNull: true, defaultValue: 'RI/QA/F/10' },
      rev_no: { type: Sequelize.STRING(20), allowNull: true, defaultValue: '0' },
      work_order_id: { type: Sequelize.INTEGER, allowNull: true },
      won: { type: Sequelize.STRING(80), allowNull: true },
      item_code: { type: Sequelize.STRING(120), allowNull: true },
      item_name: { type: Sequelize.STRING(250), allowNull: true },
      customer_id: { type: Sequelize.INTEGER, allowNull: true },
      customer_name: { type: Sequelize.STRING(250), allowNull: true },
      work_order_qty: { type: Sequelize.DECIMAL(18, 3), allowNull: true, defaultValue: 0 },
      sample_qty: { type: Sequelize.DECIMAL(18, 3), allowNull: true, defaultValue: 0 },
      inspected_by: { type: Sequelize.STRING(120), allowNull: true },
      inspected_date: { type: Sequelize.DATEONLY, allowNull: true },
      remarks: { type: Sequelize.TEXT, allowNull: true },
      status: {
        type: Sequelize.ENUM('DRAFT', 'SUBMITTED', 'APPROVED', 'CANCELLED'),
        allowNull: false,
        defaultValue: 'DRAFT'
      },
      created_by: { type: Sequelize.INTEGER, allowNull: true },
      updated_by: { type: Sequelize.INTEGER, 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('welding_visual_inspection_report', ['company_id']);
    await queryInterface.addIndex('welding_visual_inspection_report', ['plant_id']);
    await queryInterface.addIndex('welding_visual_inspection_report', ['report_date']);
    await queryInterface.addIndex('welding_visual_inspection_report', ['work_order_id']);
  },

  async down(queryInterface) {
    await queryInterface.dropTable('welding_visual_inspection_report');
    await queryInterface.sequelize.query("DROP TYPE IF EXISTS enum_welding_visual_inspection_report_status;");
  }
};

