module.exports = (sequelize, DataTypes) => {
  const ProductionConfirmation = sequelize.define('ProductionConfirmation', {
    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'
      }
    },
    confirmation_number: {
      type: DataTypes.STRING(50),
      allowNull: false
    },
    confirmation_date: {
      type: DataTypes.DATEONLY,
      allowNull: false
    },
    production_order_id: {
      type: DataTypes.INTEGER,
      allowNull: false,
      references: {
        model: 'ProductionOrder',
        key: 'id'
      }
    },
    confirmed_quantity: {
      type: DataTypes.DECIMAL(18, 3),
      allowNull: false
    },
    uom_id: {
      type: DataTypes.INTEGER,
      allowNull: false,
      references: {
        model: 'UOM',
        key: 'id'
      }
    },
    work_center_id: {
      type: DataTypes.INTEGER,
      allowNull: true,
      references: {
        model: 'WorkCenter',
        key: 'id'
      }
    },
    operation_number: {
      type: DataTypes.INTEGER,
      allowNull: true
    },
    production_order_routing_execution_id: {
      type: DataTypes.INTEGER,
      allowNull: true,
      references: {
        model: 'ProductionOrderRoutingExecution',
        key: 'id'
      },
      comment: 'Optional link to routing execution record for traceability'
    },
    status: {
      type: DataTypes.ENUM('DRAFT', 'POSTED', 'CANCELLED'),
      allowNull: false,
      defaultValue: 'DRAFT'
    },
    remarks: {
      type: DataTypes.TEXT,
      allowNull: true
    },
    posted_at: {
      type: DataTypes.DATE,
      allowNull: true
    },
    posted_by: {
      type: DataTypes.INTEGER,
      allowNull: true,
      references: {
        model: 'User',
        key: 'id'
      }
    },
    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_confirmation',
    indexes: [
      {
        fields: ['company_id', 'plant_id', 'fiscal_year_id']
      },
      {
        fields: ['confirmation_number']
      },
      {
        fields: ['production_order_id']
      },
      {
        fields: ['status']
      }
    ]
  });

  ProductionConfirmation.associate = function(models) {
    ProductionConfirmation.belongsTo(models.Company, { foreignKey: 'company_id', as: 'company' });
    ProductionConfirmation.belongsTo(models.Plant, { foreignKey: 'plant_id', as: 'plant' });
    ProductionConfirmation.belongsTo(models.FiscalYear, { foreignKey: 'fiscal_year_id', as: 'fiscalYear' });
    ProductionConfirmation.belongsTo(models.ProductionOrder, { foreignKey: 'production_order_id', as: 'productionOrder' });
    ProductionConfirmation.belongsTo(models.WorkCenter, { foreignKey: 'work_center_id', as: 'workCenter' });
    ProductionConfirmation.belongsTo(models.ProductionOrderRoutingExecution, {
      foreignKey: 'production_order_routing_execution_id',
      as: 'routingExecution'
    });
    ProductionConfirmation.belongsTo(models.User, { foreignKey: 'posted_by', as: 'postedByUser' });
    ProductionConfirmation.belongsTo(models.User, { foreignKey: 'created_by', as: 'createdByUser' });
    ProductionConfirmation.belongsTo(models.User, { foreignKey: 'updated_by', as: 'updatedByUser' });
  };

  return ProductionConfirmation;
};

