'use strict';

module.exports = {
  up: async (queryInterface, Sequelize) => {
    await queryInterface.createTable('assembly_final_inspection_report', {
      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'
      },
      fiscal_year_id: {
        type: Sequelize.INTEGER,
        allowNull: false,
        references: { model: 'fiscal_year', key: 'id' },
        onUpdate: 'CASCADE',
        onDelete: 'RESTRICT'
      },
      report_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/05'
      },
      rev_no: {
        type: Sequelize.STRING(50),
        allowNull: true,
        defaultValue: '0 / 05-10-2018'
      },
      model: { type: Sequelize.STRING(200), allowNull: true },
      work_order_no: { type: Sequelize.STRING(100), allowNull: true },
      customer_id: {
        type: Sequelize.INTEGER,
        allowNull: true,
        references: { model: 'business_partner', key: 'id' },
        onUpdate: 'CASCADE',
        onDelete: 'SET NULL'
      },
      customer_name: { type: Sequelize.STRING(200), allowNull: true },
      total_qty: { type: Sequelize.DECIMAL(15, 3), allowNull: true },
      sample_qty: { type: Sequelize.DECIMAL(15, 3), allowNull: true },
      remarks: { type: Sequelize.TEXT, allowNull: true },
      charge_sign_by: {
        type: Sequelize.INTEGER,
        allowNull: true,
        references: { model: 'user', key: 'id' },
        onUpdate: 'CASCADE',
        onDelete: 'SET NULL'
      },
      inspected_by: {
        type: Sequelize.INTEGER,
        allowNull: true,
        references: { model: 'user', key: 'id' },
        onUpdate: 'CASCADE',
        onDelete: 'SET NULL'
      },
      status: {
        type: Sequelize.ENUM('DRAFT', 'SUBMITTED', 'APPROVED', 'CANCELLED'),
        allowNull: false,
        defaultValue: 'DRAFT'
      },
      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')
      }
    }, { charset: 'utf8mb4', collate: 'utf8mb4_unicode_ci' });

    await queryInterface.createTable('assembly_final_inspection_report_detail', {
      id: {
        type: Sequelize.INTEGER,
        primaryKey: true,
        autoIncrement: true
      },
      report_id: {
        type: Sequelize.INTEGER,
        allowNull: false,
        references: { model: 'assembly_final_inspection_report', key: 'id' },
        onUpdate: 'CASCADE',
        onDelete: 'CASCADE'
      },
      line_no: { type: Sequelize.INTEGER, allowNull: false },
      item_description: { type: Sequelize.STRING(500), allowNull: true },
      specification: { type: Sequelize.STRING(300), allowNull: true },
      qty_per_set: { type: Sequelize.DECIMAL(15, 3), allowNull: true },
      frame_1: { type: Sequelize.STRING(50), allowNull: true },
      frame_2: { type: Sequelize.STRING(50), allowNull: true },
      frame_3: { type: Sequelize.STRING(50), allowNull: true },
      frame_4: { type: Sequelize.STRING(50), allowNull: true },
      frame_5: { type: Sequelize.STRING(50), allowNull: true },
      frame_6: { type: Sequelize.STRING(50), allowNull: true },
      frame_7: { type: Sequelize.STRING(50), allowNull: true },
      frame_8: { type: Sequelize.STRING(50), allowNull: true },
      frame_9: { type: Sequelize.STRING(50), allowNull: true },
      frame_10: { type: Sequelize.STRING(50), allowNull: true }
    }, { charset: 'utf8mb4', collate: 'utf8mb4_unicode_ci' });

    await queryInterface.addIndex('assembly_final_inspection_report', ['company_id'], { name: 'idx_afir_company_id' });
    await queryInterface.addIndex('assembly_final_inspection_report', ['plant_id'], { name: 'idx_afir_plant_id' });
    await queryInterface.addIndex('assembly_final_inspection_report', ['report_no'], { name: 'idx_afir_report_no', unique: true });
    await queryInterface.addIndex('assembly_final_inspection_report', ['report_date'], { name: 'idx_afir_report_date' });
    await queryInterface.addIndex('assembly_final_inspection_report', ['customer_id'], { name: 'idx_afir_customer_id' });
    await queryInterface.addIndex('assembly_final_inspection_report', ['status'], { name: 'idx_afir_status' });
    await queryInterface.addIndex('assembly_final_inspection_report_detail', ['report_id'], { name: 'idx_afir_detail_report_id' });
    await queryInterface.addIndex('assembly_final_inspection_report_detail', ['line_no'], { name: 'idx_afir_detail_line_no' });
  },

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