module.exports = (sequelize, DataTypes) => {
  const RouteCardOperation = sequelize.define(
    'RouteCardOperation',
    {
      id: {
        type: DataTypes.INTEGER,
        primaryKey: true,
        autoIncrement: true
      },
      route_card_id: {
        type: DataTypes.INTEGER,
        allowNull: false,
        references: { model: 'RouteCard', key: 'id' }
      },
      operation_sequence_no: {
        type: DataTypes.INTEGER,
        allowNull: false
      },
      operation_code: {
        type: DataTypes.STRING(100),
        allowNull: false
      },
      operation_name: {
        type: DataTypes.STRING(500),
        allowNull: true
      },
      operator_id: {
        type: DataTypes.INTEGER,
        allowNull: true,
        references: { model: 'ProductionOperator', key: 'id' }
      },
      operator_ids: {
        type: DataTypes.TEXT,
        allowNull: true
      },
      department_id: {
        type: DataTypes.INTEGER,
        allowNull: true,
        references: { model: 'Department', key: 'id' }
      },
      work_center_id: {
        type: DataTypes.INTEGER,
        allowNull: false,
        references: { model: 'WorkCenter', key: 'id' }
      },
      planned_qty: {
        type: DataTypes.DECIMAL(18, 3),
        allowNull: false,
        defaultValue: 0
      },
      input_qty: {
        type: DataTypes.DECIMAL(18, 3),
        allowNull: false,
        defaultValue: 0
      },
      completed_qty: {
        type: DataTypes.DECIMAL(18, 3),
        allowNull: false,
        defaultValue: 0
      },
      checked_qty: {
        type: DataTypes.DECIMAL(18, 3),
        allowNull: false,
        defaultValue: 0
      },
      rejected_qty: {
        type: DataTypes.DECIMAL(18, 3),
        allowNull: false,
        defaultValue: 0
      },
      fr_rejected_qty: {
        type: DataTypes.DECIMAL(18, 3),
        allowNull: false,
        defaultValue: 0
      },
      mr_rejected_qty: {
        type: DataTypes.DECIMAL(18, 3),
        allowNull: false,
        defaultValue: 0
      },
      rejection_fr: {
        type: DataTypes.BOOLEAN,
        allowNull: false,
        defaultValue: false
      },
      rejection_mr: {
        type: DataTypes.BOOLEAN,
        allowNull: false,
        defaultValue: false
      },
      rework_qty: {
        type: DataTypes.DECIMAL(18, 3),
        allowNull: false,
        defaultValue: 0
      },
      actual_start: {
        type: DataTypes.DATE,
        allowNull: true
      },
      actual_end: {
        type: DataTypes.DATE,
        allowNull: true
      },
      status: {
        type: DataTypes.ENUM('PENDING', 'IN_PROGRESS', 'COMPLETED', 'SKIPPED', 'CANCELLED'),
        allowNull: false,
        defaultValue: 'PENDING'
      },
      remarks: {
        type: DataTypes.TEXT,
        allowNull: true
      },
      created_by: {
        type: DataTypes.INTEGER,
        allowNull: true,
        references: { model: 'User', key: 'id' }
      },
      updated_by: {
        type: DataTypes.INTEGER,
        allowNull: true,
        references: { model: 'User', key: 'id' }
      }
    },
    {
      tableName: 'route_card_operation',
      indexes: [
        { fields: ['route_card_id'] },
        { fields: ['operator_id'] },
        { fields: ['department_id'] },
        { fields: ['work_center_id'] },
        { fields: ['status'] },
        { unique: true, fields: ['route_card_id', 'operation_sequence_no'] }
      ]
    }
  );

  RouteCardOperation.associate = function (models) {
    RouteCardOperation.belongsTo(models.RouteCard, { foreignKey: 'route_card_id', as: 'routeCard' });
    RouteCardOperation.belongsTo(models.ProductionOperator, { foreignKey: 'operator_id', as: 'operator' });
    RouteCardOperation.belongsTo(models.Department, { foreignKey: 'department_id', as: 'department' });
    RouteCardOperation.belongsTo(models.WorkCenter, { foreignKey: 'work_center_id', as: 'workCenter' });
    RouteCardOperation.hasOne(models.ProductionQC, { foreignKey: 'route_card_operation_id', as: 'productionQC' });
    RouteCardOperation.hasMany(models.RouteCardOperationMaterialConsumption, {
      foreignKey: 'route_card_operation_id',
      as: 'materialConsumptions',
    });
    RouteCardOperation.hasMany(models.RouteCardOperationComponentTracking, {
      foreignKey: 'route_card_operation_id',
      as: 'componentTrackings',
    });
    RouteCardOperation.belongsTo(models.User, { foreignKey: 'created_by', as: 'createdByUser' });
    RouteCardOperation.belongsTo(models.User, { foreignKey: 'updated_by', as: 'updatedByUser' });
  };

  return RouteCardOperation;
};

