'use strict';

module.exports = {
  up: async (queryInterface, Sequelize) => {
    // Create in_process_checklist table
    await queryInterface.createTable('in_process_checklist', {
      id: {
        type: Sequelize.INTEGER,
        primaryKey: true,
        autoIncrement: true
      },
      company_id: {
        type: Sequelize.INTEGER,
        allowNull: false,
        references: {
          model: 'company',
          key: 'id'
        },
        onUpdate: 'CASCADE',
        onDelete: 'RESTRICT',
        comment: 'Company'
      },
      plant_id: {
        type: Sequelize.INTEGER,
        allowNull: false,
        references: {
          model: 'plant',
          key: 'id'
        },
        onUpdate: 'CASCADE',
        onDelete: 'RESTRICT',
        comment: 'Plant'
      },
      fiscal_year_id: {
        type: Sequelize.INTEGER,
        allowNull: false,
        references: {
          model: 'fiscal_year',
          key: 'id'
        },
        onUpdate: 'CASCADE',
        onDelete: 'RESTRICT',
        comment: 'Fiscal year'
      },
      ipc_no: {
        type: Sequelize.STRING(50),
        allowNull: false,
        unique: true,
        comment: 'In-Process CheckList Number'
      },
      ipc_date: {
        type: Sequelize.DATEONLY,
        allowNull: false,
        comment: 'CheckList Date'
      },
      doc_no: {
        type: Sequelize.STRING(50),
        allowNull: true,
        defaultValue: 'RI/QA/F/09',
        comment: 'Document Number'
      },
      rev_no: {
        type: Sequelize.STRING(20),
        allowNull: true,
        defaultValue: '0 / 05.10.2018',
        comment: 'Revision Number'
      },
      work_order_no: {
        type: Sequelize.STRING(100),
        allowNull: true,
        comment: 'Work Order Number (WON)'
      },
      customer_id: {
        type: Sequelize.INTEGER,
        allowNull: true,
        references: {
          model: 'business_partner',
          key: 'id'
        },
        onUpdate: 'CASCADE',
        onDelete: 'SET NULL',
        comment: 'Customer'
      },
      customer_name: {
        type: Sequelize.STRING(200),
        allowNull: true,
        comment: 'Customer name (cached)'
      },
      model: {
        type: Sequelize.STRING(200),
        allowNull: true,
        comment: 'Model/Product'
      },
      total_qty: {
        type: Sequelize.DECIMAL(15, 3),
        allowNull: true,
        comment: 'Total Quantity'
      },
      status: {
        type: Sequelize.ENUM('DRAFT', 'SUBMITTED', 'APPROVED', 'CANCELLED'),
        allowNull: false,
        defaultValue: 'DRAFT',
        comment: 'Document status'
      },
      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',
      comment: 'In-Process CheckList (RI/QA/F/09)'
    });

    // Create in_process_checklist_detail table
    await queryInterface.createTable('in_process_checklist_detail', {
      id: {
        type: Sequelize.INTEGER,
        primaryKey: true,
        autoIncrement: true
      },
      ipc_id: {
        type: Sequelize.INTEGER,
        allowNull: false,
        references: {
          model: 'in_process_checklist',
          key: 'id'
        },
        onUpdate: 'CASCADE',
        onDelete: 'CASCADE',
        comment: 'In-Process CheckList ID'
      },
      line_no: {
        type: Sequelize.INTEGER,
        allowNull: false,
        comment: 'Serial Number'
      },
      item_description: {
        type: Sequelize.STRING(500),
        allowNull: true,
        comment: 'Item Description'
      },
      drawing_no: {
        type: Sequelize.STRING(100),
        allowNull: true,
        comment: 'Drawing Number'
      },
      model: {
        type: Sequelize.STRING(200),
        allowNull: true,
        comment: 'Model'
      },
      qty_per_set: {
        type: Sequelize.DECIMAL(15, 3),
        allowNull: true,
        comment: 'Quantity per Set'
      },
      total_qty: {
        type: Sequelize.DECIMAL(15, 3),
        allowNull: true,
        comment: 'Total Quantity'
      },
      qc_status: {
        type: Sequelize.STRING(50),
        allowNull: true,
        comment: 'QC Status (OK/NOT OK)'
      },
      remarks: {
        type: Sequelize.TEXT,
        allowNull: true,
        comment: 'Remarks'
      }
    }, {
      charset: 'utf8mb4',
      collate: 'utf8mb4_unicode_ci',
      comment: 'In-Process CheckList Detail Lines'
    });

    // Create indexes for in_process_checklist
    await queryInterface.addIndex('in_process_checklist', ['company_id'], {
      name: 'idx_ipc_company_id'
    });
    await queryInterface.addIndex('in_process_checklist', ['plant_id'], {
      name: 'idx_ipc_plant_id'
    });
    await queryInterface.addIndex('in_process_checklist', ['ipc_no'], {
      name: 'idx_ipc_no',
      unique: true
    });
    await queryInterface.addIndex('in_process_checklist', ['ipc_date'], {
      name: 'idx_ipc_date'
    });
    await queryInterface.addIndex('in_process_checklist', ['customer_id'], {
      name: 'idx_ipc_customer_id'
    });
    await queryInterface.addIndex('in_process_checklist', ['status'], {
      name: 'idx_ipc_status'
    });

    // Create indexes for in_process_checklist_detail
    await queryInterface.addIndex('in_process_checklist_detail', ['ipc_id'], {
      name: 'idx_ipc_detail_ipc_id'
    });
    await queryInterface.addIndex('in_process_checklist_detail', ['line_no'], {
      name: 'idx_ipc_detail_line_no'
    });
  },

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