module.exports = (sequelize, DataTypes) => {
  const RequestForQuotation = sequelize.define('RequestForQuotation', {
    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'
      }
    },
    rfq_number: {
      type: DataTypes.STRING(50),
      allowNull: false
    },
    rfq_date: {
      type: DataTypes.DATEONLY,
      allowNull: false
    },
    pr_id: {
      type: DataTypes.INTEGER,
      allowNull: true,
      references: {
        model: 'PurchaseRequisition',
        key: 'id'
      }
    },
    valid_until: {
      type: DataTypes.DATEONLY,
      allowNull: true
    },
    currency_id: {
      type: DataTypes.INTEGER,
      allowNull: true,
      references: {
        model: 'Currency',
        key: 'id'
      }
    },
    payment_terms: {
      type: DataTypes.STRING(200),
      allowNull: true
    },
    delivery_terms: {
      type: DataTypes.STRING(200),
      allowNull: true
    },
    status: {
      type: DataTypes.ENUM('DRAFT', 'SUBMITTED', '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'
      }
    },
    closed_at: {
      type: DataTypes.DATE,
      allowNull: true
    },
    closed_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: 'request_for_quotation',
    indexes: [
      {
        unique: true,
        fields: ['company_id', 'rfq_number']
      },
      {
        fields: ['plant_id']
      },
      {
        fields: ['fiscal_year_id']
      },
      {
        fields: ['pr_id']
      },
      {
        fields: ['status']
      },
      {
        fields: ['rfq_date']
      }
    ]
  });

  RequestForQuotation.associate = function(models) {
    RequestForQuotation.belongsTo(models.Company, { foreignKey: 'company_id', as: 'company' });
    RequestForQuotation.belongsTo(models.Plant, { foreignKey: 'plant_id', as: 'plant' });
    RequestForQuotation.belongsTo(models.FiscalYear, { foreignKey: 'fiscal_year_id', as: 'fiscalYear' });
    RequestForQuotation.belongsTo(models.PurchaseRequisition, { foreignKey: 'pr_id', as: 'purchaseRequisition' });
    RequestForQuotation.belongsTo(models.Currency, { foreignKey: 'currency_id', as: 'currency' });
    RequestForQuotation.hasMany(models.RequestForQuotationItem, { foreignKey: 'rfq_id', as: 'items' });
    RequestForQuotation.belongsTo(models.User, { foreignKey: 'submitted_by', as: 'submittedByUser' });
    RequestForQuotation.belongsTo(models.User, { foreignKey: 'closed_by', as: 'closedByUser' });
    RequestForQuotation.belongsTo(models.User, { foreignKey: 'created_by', as: 'createdByUser' });
    RequestForQuotation.belongsTo(models.User, { foreignKey: 'updated_by', as: 'updatedByUser' });
  };

  return RequestForQuotation;
};

