'use strict';

module.exports = {
  async up(queryInterface, Sequelize) {
    await queryInterface.createTable('work_order', {
      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'
      },
      work_order_number: {
        type: Sequelize.STRING(50),
        allowNull: false
      },
      production_order_id: {
        type: Sequelize.INTEGER,
        allowNull: false,
        references: { model: 'production_order', key: 'id' },
        onUpdate: 'CASCADE',
        onDelete: 'RESTRICT'
      },
      customer_id: {
        type: Sequelize.INTEGER,
        allowNull: true,
        references: { model: 'business_partner', 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'
      },
      order_quantity: {
        type: Sequelize.DECIMAL(18, 3),
        allowNull: true
      },
      batch_quantity: {
        type: Sequelize.DECIMAL(18, 3),
        allowNull: true
      },
      work_order_quantity: {
        type: Sequelize.DECIMAL(18, 3),
        allowNull: false,
        defaultValue: 0
      },
      planned_start_date: {
        type: Sequelize.DATEONLY,
        allowNull: true
      },
      planned_end_date: {
        type: Sequelize.DATEONLY,
        allowNull: true
      },
      priority: {
        type: Sequelize.ENUM('LOW', 'MEDIUM', 'HIGH'),
        allowNull: false,
        defaultValue: 'MEDIUM'
      },
      status: {
        type: Sequelize.ENUM('PLANNED', 'RELEASED', 'IN_PROGRESS', 'COMPLETED', 'CLOSED'),
        allowNull: false,
        defaultValue: 'PLANNED'
      },
      created_by: {
        type: Sequelize.INTEGER,
        allowNull: true,
        references: { model: 'user', key: 'id' },
        onUpdate: 'CASCADE',
        onDelete: 'SET NULL'
      },
      approved_by: {
        type: Sequelize.INTEGER,
        allowNull: true,
        references: { model: 'user', key: 'id' },
        onUpdate: 'CASCADE',
        onDelete: 'SET NULL'
      },
      remarks: {
        type: Sequelize.TEXT,
        allowNull: true
      },
      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('work_order', ['company_id', 'plant_id', 'fiscal_year_id'], {
      name: 'idx_work_order_company_plant_fy'
    });
    await queryInterface.addIndex('work_order', ['work_order_number'], {
      name: 'idx_work_order_number'
    });
    await queryInterface.addIndex('work_order', ['production_order_id'], {
      name: 'idx_work_order_production_order'
    });
    await queryInterface.addIndex('work_order', ['status'], {
      name: 'idx_work_order_status'
    });

    await queryInterface.createTable('work_order_operation_plan', {
      id: {
        allowNull: false,
        autoIncrement: true,
        primaryKey: true,
        type: Sequelize.INTEGER
      },
      work_order_id: {
        type: Sequelize.INTEGER,
        allowNull: false,
        references: { model: 'work_order', key: 'id' },
        onUpdate: 'CASCADE',
        onDelete: 'CASCADE'
      },
      operation_sequence_no: {
        type: Sequelize.INTEGER,
        allowNull: false
      },
      operation_name: {
        type: Sequelize.STRING(500),
        allowNull: false
      },
      work_center_id: {
        type: Sequelize.INTEGER,
        allowNull: false,
        references: { model: 'work_center', key: 'id' },
        onUpdate: 'CASCADE',
        onDelete: 'RESTRICT'
      },
      machine_id: {
        type: Sequelize.INTEGER,
        allowNull: true,
        references: { model: 'machine', key: 'id' },
        onUpdate: 'CASCADE',
        onDelete: 'SET NULL'
      },
      planned_qty: {
        type: Sequelize.DECIMAL(18, 3),
        allowNull: false,
        defaultValue: 0
      },
      target_date: {
        type: Sequelize.DATEONLY,
        allowNull: true
      },
      standard_time_minutes: {
        type: Sequelize.INTEGER,
        allowNull: true,
        comment: 'Standard time (minutes)'
      },
      setup_time_minutes: {
        type: Sequelize.INTEGER,
        allowNull: true,
        comment: 'Setup time (minutes)'
      },
      status: {
        type: Sequelize.ENUM('PLANNED', 'RELEASED', 'IN_PROGRESS', 'COMPLETED', 'CLOSED'),
        allowNull: false,
        defaultValue: 'PLANNED'
      },
      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('work_order_operation_plan', ['work_order_id'], {
      name: 'idx_wo_op_work_order'
    });
    await queryInterface.addIndex('work_order_operation_plan', ['work_center_id'], {
      name: 'idx_wo_op_work_center'
    });
    await queryInterface.addIndex('work_order_operation_plan', ['status'], {
      name: 'idx_wo_op_status'
    });
    await queryInterface.addIndex('work_order_operation_plan', ['work_order_id', 'operation_sequence_no'], {
      unique: true,
      name: 'uniq_wo_op_sequence'
    });
  },

  async down(queryInterface) {
    await queryInterface.dropTable('work_order_operation_plan');
    await queryInterface.dropTable('work_order');
  }
};

