module.exports = (sequelize, DataTypes) => {
  const ProductionSchedule = sequelize.define('ProductionSchedule', {
    id: {
      type: DataTypes.INTEGER,
      primaryKey: true,
      autoIncrement: true
    },
    company_id: {
      type: DataTypes.INTEGER,
      allowNull: false,
      references: {
        model: 'Company',
        key: 'id'
      }
    },
    plant_id: {
      type: DataTypes.INTEGER,
      allowNull: false,
      references: {
        model: 'Plant',
        key: 'id'
      }
    },
    production_order_id: {
      type: DataTypes.INTEGER,
      allowNull: false,
      references: {
        model: 'ProductionOrder',
        key: 'id'
      }
    },
    routing_operation_id: {
      type: DataTypes.INTEGER,
      allowNull: true,
      references: {
        model: 'RoutingOperation',
        key: 'id'
      }
    },
    work_center_id: {
      type: DataTypes.INTEGER,
      allowNull: false,
      references: {
        model: 'WorkCenter',
        key: 'id'
      }
    },
    operation_number: {
      type: DataTypes.INTEGER,
      allowNull: false,
      comment: 'Operation number from routing'
    },
    scheduled_start_date: {
      type: DataTypes.DATE,
      allowNull: false,
      comment: 'Scheduled start date and time'
    },
    scheduled_end_date: {
      type: DataTypes.DATE,
      allowNull: false,
      comment: 'Scheduled end date and time'
    },
    planned_hours: {
      type: DataTypes.DECIMAL(10, 2),
      allowNull: false,
      defaultValue: 0.00,
      comment: 'Planned hours for this operation'
    },
    scheduled_quantity: {
      type: DataTypes.DECIMAL(18, 3),
      allowNull: false,
      comment: 'Quantity scheduled for this operation'
    },
    priority: {
      type: DataTypes.INTEGER,
      allowNull: true,
      defaultValue: 0,
      comment: 'Schedule priority (higher = more urgent)'
    },
    schedule_status: {
      type: DataTypes.ENUM('SCHEDULED', 'IN_PROGRESS', 'COMPLETED', 'CANCELLED', 'ON_HOLD'),
      allowNull: false,
      defaultValue: 'SCHEDULED'
    },
    schedule_type: {
      type: DataTypes.ENUM('FORWARD', 'BACKWARD', 'MANUAL'),
      allowNull: false,
      defaultValue: 'FORWARD',
      comment: 'Forward scheduling from start date, backward from due date, or manual'
    },
    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: 'production_schedule',
    indexes: [
      {
        fields: ['company_id', 'plant_id']
      },
      {
        fields: ['production_order_id']
      },
      {
        fields: ['work_center_id']
      },
      {
        fields: ['scheduled_start_date', 'scheduled_end_date']
      },
      {
        fields: ['schedule_status']
      }
    ]
  });

  ProductionSchedule.associate = function(models) {
    ProductionSchedule.belongsTo(models.Company, { foreignKey: 'company_id', as: 'company' });
    ProductionSchedule.belongsTo(models.Plant, { foreignKey: 'plant_id', as: 'plant' });
    ProductionSchedule.belongsTo(models.ProductionOrder, { foreignKey: 'production_order_id', as: 'productionOrder' });
    ProductionSchedule.belongsTo(models.RoutingOperation, { foreignKey: 'routing_operation_id', as: 'routingOperation' });
    ProductionSchedule.belongsTo(models.WorkCenter, { foreignKey: 'work_center_id', as: 'workCenter' });
    ProductionSchedule.belongsTo(models.User, { foreignKey: 'created_by', as: 'createdByUser' });
    ProductionSchedule.belongsTo(models.User, { foreignKey: 'updated_by', as: 'updatedByUser' });
  };

  return ProductionSchedule;
};

