module.exports = (sequelize, DataTypes) => {
  const PurchaseRequisition = sequelize.define('PurchaseRequisition', {
    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'
      }
    },
    pr_number: {
      type: DataTypes.STRING(50),
      allowNull: false
    },
    pr_date: {
      type: DataTypes.DATEONLY,
      allowNull: false
    },
    required_date: {
      type: DataTypes.DATEONLY,
      allowNull: true
    },
    department_id: {
      type: DataTypes.INTEGER,
      allowNull: true,
      references: {
        model: 'Department',
        key: 'id'
      }
    },
    cost_center_id: {
      type: DataTypes.INTEGER,
      allowNull: true,
      references: {
        model: 'CostCenter',
        key: 'id'
      }
    },
    requested_by: {
      type: DataTypes.INTEGER,
      allowNull: true,
      references: {
        model: 'User',
        key: 'id'
      }
    },
    status: {
      type: DataTypes.ENUM('DRAFT', 'SUBMITTED', 'APPROVED', 'REJECTED', 'CLOSED'),
      allowNull: false,
      defaultValue: 'DRAFT'
    },
    remarks: {
      type: DataTypes.TEXT,
      allowNull: true
    },
    total_amount: {
      type: DataTypes.DECIMAL(18, 2),
      allowNull: false,
      defaultValue: 0
    },
    submitted_at: {
      type: DataTypes.DATE,
      allowNull: true
    },
    submitted_by: {
      type: DataTypes.INTEGER,
      allowNull: true,
      references: {
        model: 'User',
        key: 'id'
      }
    },
    approved_at: {
      type: DataTypes.DATE,
      allowNull: true
    },
    approved_by: {
      type: DataTypes.INTEGER,
      allowNull: true,
      references: {
        model: 'User',
        key: 'id'
      }
    },
    rejected_at: {
      type: DataTypes.DATE,
      allowNull: true
    },
    rejected_by: {
      type: DataTypes.INTEGER,
      allowNull: true,
      references: {
        model: 'User',
        key: 'id'
      }
    },
    rejection_reason: {
      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: 'purchase_requisition',
    indexes: [
      {
        unique: true,
        fields: ['company_id', 'pr_number']
      },
      {
        fields: ['plant_id']
      },
      {
        fields: ['fiscal_year_id']
      },
      {
        fields: ['status']
      },
      {
        fields: ['pr_date']
      }
    ]
  });

  PurchaseRequisition.associate = function(models) {
    PurchaseRequisition.belongsTo(models.Company, { foreignKey: 'company_id', as: 'company' });
    PurchaseRequisition.belongsTo(models.Plant, { foreignKey: 'plant_id', as: 'plant' });
    PurchaseRequisition.belongsTo(models.FiscalYear, { foreignKey: 'fiscal_year_id', as: 'fiscalYear' });
    PurchaseRequisition.belongsTo(models.Department, { foreignKey: 'department_id', as: 'department' });
    PurchaseRequisition.belongsTo(models.CostCenter, { foreignKey: 'cost_center_id', as: 'costCenter' });
    PurchaseRequisition.hasMany(models.PurchaseRequisitionItem, { foreignKey: 'pr_id', as: 'items' });
    PurchaseRequisition.belongsTo(models.User, { foreignKey: 'requested_by', as: 'requestedByUser' });
    PurchaseRequisition.belongsTo(models.User, { foreignKey: 'submitted_by', as: 'submittedByUser' });
    PurchaseRequisition.belongsTo(models.User, { foreignKey: 'approved_by', as: 'approvedByUser' });
    PurchaseRequisition.belongsTo(models.User, { foreignKey: 'rejected_by', as: 'rejectedByUser' });
    PurchaseRequisition.belongsTo(models.User, { foreignKey: 'created_by', as: 'createdByUser' });
    PurchaseRequisition.belongsTo(models.User, { foreignKey: 'updated_by', as: 'updatedByUser' });
  };

  return PurchaseRequisition;
};

