module.exports = (sequelize, DataTypes) => {
  const RouteCard = sequelize.define(
    'RouteCard',
    {
      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' }
      },
      fiscal_year_id: {
        type: DataTypes.INTEGER,
        allowNull: false,
        references: { model: 'FiscalYear', key: 'id' }
      },
      route_card_number: {
        type: DataTypes.STRING(50),
        allowNull: false
      },
      work_order_id: {
        type: DataTypes.INTEGER,
        allowNull: false,
        references: { model: 'WorkOrder', key: 'id' }
      },
      previous_route_card_id: {
        type: DataTypes.INTEGER,
        allowNull: true,
        references: { model: 'RouteCard', key: 'id' }
      },
      source_production_qc_id: {
        type: DataTypes.INTEGER,
        allowNull: true,
        unique: true,
        references: { model: 'ProductionQC', key: 'id' }
      },
      production_order_id: {
        type: DataTypes.INTEGER,
        allowNull: true,
        references: { model: 'ProductionOrder', key: 'id' }
      },
      finished_material_id: {
        type: DataTypes.INTEGER,
        allowNull: true,
        references: { model: 'Material', key: 'id' }
      },
      bom_id: {
        type: DataTypes.INTEGER,
        allowNull: true,
        references: { model: 'BOMHeader', key: 'id' }
      },
      production_qty: {
        type: DataTypes.DECIMAL(18, 3),
        allowNull: false,
        defaultValue: 0
      },
      start_date: {
        type: DataTypes.DATEONLY,
        allowNull: false
      },
      status: {
        type: DataTypes.ENUM('OPEN', 'CLOSED', 'CANCELLED'),
        allowNull: false,
        defaultValue: 'OPEN'
      },
      pdf_sign_offs: {
        type: DataTypes.JSON,
        allowNull: true,
        comment:
          'Selected user ids for Route Card PDF: verified_by, accountant, qc, qa_inspector'
      },
      raw_material_process_switches: {
        type: DataTypes.JSON,
        allowNull: true,
        comment:
          'Per raw-material-row process toggles keyed by row key: turret_punch_press, laser, bending'
      },
      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',
      indexes: [
        { fields: ['company_id', 'plant_id', 'fiscal_year_id'] },
        { fields: ['route_card_number'] },
        { fields: ['work_order_id'] },
        { fields: ['previous_route_card_id'] },
        { unique: true, fields: ['source_production_qc_id'] },
        { fields: ['status'] }
      ]
    }
  );

  RouteCard.associate = function (models) {
    RouteCard.belongsTo(models.Company, { foreignKey: 'company_id', as: 'company' });
    RouteCard.belongsTo(models.Plant, { foreignKey: 'plant_id', as: 'plant' });
    RouteCard.belongsTo(models.FiscalYear, { foreignKey: 'fiscal_year_id', as: 'fiscalYear' });
    RouteCard.belongsTo(models.WorkOrder, { foreignKey: 'work_order_id', as: 'workOrder' });
    RouteCard.belongsTo(models.RouteCard, { foreignKey: 'previous_route_card_id', as: 'previousRouteCard' });
    RouteCard.belongsTo(models.ProductionQC, { foreignKey: 'source_production_qc_id', as: 'sourceProductionQC' });
    RouteCard.belongsTo(models.ProductionOrder, { foreignKey: 'production_order_id', as: 'productionOrder' });
    RouteCard.belongsTo(models.Material, { foreignKey: 'finished_material_id', as: 'finishedMaterial' });
    RouteCard.belongsTo(models.BOMHeader, { foreignKey: 'bom_id', as: 'bom' });
    RouteCard.belongsTo(models.User, { foreignKey: 'created_by', as: 'createdByUser' });
    RouteCard.belongsTo(models.User, { foreignKey: 'updated_by', as: 'updatedByUser' });
    RouteCard.hasMany(models.RouteCardOperation, { foreignKey: 'route_card_id', as: 'operations' });
    RouteCard.hasMany(models.RouteCard, { foreignKey: 'previous_route_card_id', as: 'reworkRouteCards' });
    RouteCard.hasOne(models.ProductionFinalInspection, { foreignKey: 'route_card_id', as: 'finalInspection' });
  };

  return RouteCard;
};

