'use strict';

module.exports = {
  async up(queryInterface, Sequelize) {
    await queryInterface.createTable('route_card', {
      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'
      },
      route_card_number: {
        type: Sequelize.STRING(50),
        allowNull: false
      },
      work_order_id: {
        type: Sequelize.INTEGER,
        allowNull: false,
        references: { model: 'work_order', key: 'id' },
        onUpdate: 'CASCADE',
        onDelete: 'RESTRICT'
      },
      production_order_id: {
        type: Sequelize.INTEGER,
        allowNull: true,
        references: { model: 'production_order', key: 'id' },
        onUpdate: 'CASCADE',
        onDelete: 'SET NULL'
      },
      finished_material_id: {
        type: Sequelize.INTEGER,
        allowNull: true,
        references: { model: 'material', key: 'id' },
        onUpdate: 'CASCADE',
        onDelete: 'SET NULL'
      },
      bom_id: {
        type: Sequelize.INTEGER,
        allowNull: true,
        references: { model: 'bom_header', key: 'id' },
        onUpdate: 'CASCADE',
        onDelete: 'SET NULL'
      },
      production_qty: {
        type: Sequelize.DECIMAL(18, 3),
        allowNull: false,
        defaultValue: 0
      },
      start_date: {
        type: Sequelize.DATEONLY,
        allowNull: false
      },
      status: {
        type: Sequelize.ENUM('OPEN', 'CLOSED', 'CANCELLED'),
        allowNull: false,
        defaultValue: 'OPEN'
      },
      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('route_card', ['company_id', 'plant_id', 'fiscal_year_id'], { name: 'idx_rc_company_plant_fy' });
    await queryInterface.addIndex('route_card', ['route_card_number'], { name: 'idx_rc_number' });
    await queryInterface.addIndex('route_card', ['work_order_id'], { unique: true, name: 'uniq_rc_work_order' });
    await queryInterface.addIndex('route_card', ['status'], { name: 'idx_rc_status' });

    await queryInterface.createTable('route_card_operation', {
      id: {
        allowNull: false,
        autoIncrement: true,
        primaryKey: true,
        type: Sequelize.INTEGER
      },
      route_card_id: {
        type: Sequelize.INTEGER,
        allowNull: false,
        references: { model: 'route_card', key: 'id' },
        onUpdate: 'CASCADE',
        onDelete: 'CASCADE'
      },
      operation_sequence_no: {
        type: Sequelize.INTEGER,
        allowNull: false
      },
      operation_code: {
        type: Sequelize.STRING(100),
        allowNull: false
      },
      work_center_id: {
        type: Sequelize.INTEGER,
        allowNull: false,
        references: { model: 'work_center', key: 'id' },
        onUpdate: 'CASCADE',
        onDelete: 'RESTRICT'
      },
      planned_qty: {
        type: Sequelize.DECIMAL(18, 3),
        allowNull: false,
        defaultValue: 0
      },
      input_qty: {
        type: Sequelize.DECIMAL(18, 3),
        allowNull: false,
        defaultValue: 0,
        comment: 'Quantity available to process at this operation'
      },
      completed_qty: {
        type: Sequelize.DECIMAL(18, 3),
        allowNull: false,
        defaultValue: 0
      },
      rejected_qty: {
        type: Sequelize.DECIMAL(18, 3),
        allowNull: false,
        defaultValue: 0
      },
      actual_start: {
        type: Sequelize.DATE,
        allowNull: true
      },
      actual_end: {
        type: Sequelize.DATE,
        allowNull: true
      },
      status: {
        type: Sequelize.ENUM('PENDING', 'IN_PROGRESS', 'COMPLETED', 'SKIPPED', 'CANCELLED'),
        allowNull: false,
        defaultValue: 'PENDING'
      },
      remarks: {
        type: Sequelize.TEXT,
        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('route_card_operation', ['route_card_id'], { name: 'idx_rc_op_rc' });
    await queryInterface.addIndex('route_card_operation', ['work_center_id'], { name: 'idx_rc_op_wc' });
    await queryInterface.addIndex('route_card_operation', ['status'], { name: 'idx_rc_op_status' });
    await queryInterface.addIndex('route_card_operation', ['route_card_id', 'operation_sequence_no'], {
      unique: true,
      name: 'uniq_rc_op_sequence'
    });
  },

  async down(queryInterface) {
    await queryInterface.dropTable('route_card_operation');
    await queryInterface.dropTable('route_card');
  }
};

