'use strict';

module.exports = {
  async up(queryInterface, Sequelize) {
    await queryInterface.createTable('production_order_routing_execution', {
      id: {
        allowNull: false,
        autoIncrement: true,
        primaryKey: true,
        type: Sequelize.INTEGER
      },
      production_order_id: {
        type: Sequelize.INTEGER,
        allowNull: false,
        references: {
          model: 'production_order',
          key: 'id'
        },
        onUpdate: 'CASCADE',
        onDelete: 'CASCADE'
      },
      routing_operation_id: {
        type: Sequelize.INTEGER,
        allowNull: false,
        references: {
          model: 'routing_operation',
          key: 'id'
        },
        onUpdate: 'CASCADE',
        onDelete: 'RESTRICT'
      },
      operation_number: {
        type: Sequelize.INTEGER,
        allowNull: false,
        comment: 'Operation number from routing'
      },
      work_center_id: {
        type: Sequelize.INTEGER,
        allowNull: false,
        references: {
          model: 'work_center',
          key: 'id'
        },
        onUpdate: 'CASCADE',
        onDelete: 'RESTRICT'
      },
      planned_start_date: {
        type: Sequelize.DATE,
        allowNull: true,
        comment: 'Planned start date/time for this operation'
      },
      planned_end_date: {
        type: Sequelize.DATE,
        allowNull: true,
        comment: 'Planned end date/time for this operation'
      },
      actual_start_date: {
        type: Sequelize.DATE,
        allowNull: true,
        comment: 'Actual start date/time for this operation'
      },
      actual_end_date: {
        type: Sequelize.DATE,
        allowNull: true,
        comment: 'Actual end date/time for this operation'
      },
      planned_setup_time: {
        type: Sequelize.INTEGER,
        allowNull: false,
        defaultValue: 0,
        comment: 'Planned setup time in minutes'
      },
      planned_machine_time: {
        type: Sequelize.DECIMAL(10, 2),
        allowNull: false,
        defaultValue: 0.00,
        comment: 'Planned machine time per unit'
      },
      planned_labor_time: {
        type: Sequelize.DECIMAL(10, 2),
        allowNull: false,
        defaultValue: 0.00,
        comment: 'Planned labor time per unit'
      },
      actual_setup_time: {
        type: Sequelize.INTEGER,
        allowNull: true,
        comment: 'Actual setup time in minutes'
      },
      actual_machine_time: {
        type: Sequelize.DECIMAL(10, 2),
        allowNull: true,
        comment: 'Actual machine time per unit'
      },
      actual_labor_time: {
        type: Sequelize.DECIMAL(10, 2),
        allowNull: true,
        comment: 'Actual labor time per unit'
      },
      confirmed_quantity: {
        type: Sequelize.DECIMAL(15, 3),
        allowNull: false,
        defaultValue: 0.000,
        comment: 'Confirmed quantity for this operation'
      },
      status: {
        type: Sequelize.ENUM('PENDING', 'IN_PROGRESS', 'COMPLETED', 'SKIPPED', 'CANCELLED'),
        allowNull: false,
        defaultValue: 'PENDING',
        comment: 'Operation execution status'
      },
      remarks: {
        type: Sequelize.TEXT,
        allowNull: true,
        comment: 'Remarks or notes for this operation'
      },
      started_by: {
        type: Sequelize.INTEGER,
        allowNull: true,
        references: {
          model: 'user',
          key: 'id'
        },
        onUpdate: 'CASCADE',
        onDelete: 'SET NULL',
        comment: 'User who started this operation'
      },
      completed_by: {
        type: Sequelize.INTEGER,
        allowNull: true,
        references: {
          model: 'user',
          key: 'id'
        },
        onUpdate: 'CASCADE',
        onDelete: 'SET NULL',
        comment: 'User who completed this operation'
      },
      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')
      }
    });

    // Add indexes
    await queryInterface.addIndex('production_order_routing_execution', ['production_order_id'], {
      name: 'po_routing_exec_po_id_idx'
    });
    await queryInterface.addIndex('production_order_routing_execution', ['routing_operation_id'], {
      name: 'po_routing_exec_routing_op_id_idx'
    });
    await queryInterface.addIndex('production_order_routing_execution', ['work_center_id'], {
      name: 'po_routing_exec_work_center_id_idx'
    });
    await queryInterface.addIndex('production_order_routing_execution', ['status'], {
      name: 'po_routing_exec_status_idx'
    });
    await queryInterface.addIndex('production_order_routing_execution', ['planned_start_date', 'planned_end_date'], {
      name: 'po_routing_exec_dates_idx'
    });
    await queryInterface.addIndex('production_order_routing_execution', ['production_order_id', 'operation_number'], {
      unique: true,
      name: 'po_routing_exec_po_op_unique_idx'
    });
  },

  async down(queryInterface, Sequelize) {
    await queryInterface.dropTable('production_order_routing_execution');
  }
};

