module.exports = (sequelize, DataTypes) => {
  const VendorQuotationRequest = sequelize.define('VendorQuotationRequest', {
    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_id: {
      type: DataTypes.INTEGER,
      allowNull: false,
      references: {
        model: 'RequestForQuotation',
        key: 'id'
      }
    },
    vendor_id: {
      type: DataTypes.INTEGER,
      allowNull: false,
      references: {
        model: 'BusinessPartner',
        key: 'id'
      }
    },
    vqr_number: {
      type: DataTypes.STRING(50),
      allowNull: false
    },
    vqr_date: {
      type: DataTypes.DATEONLY,
      allowNull: false
    },
    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', 'SENT', 'RESPONDED', 'CLOSED'),
      allowNull: false,
      defaultValue: 'DRAFT'
    },
    remarks: {
      type: DataTypes.TEXT,
      allowNull: true
    },
    total_amount: {
      type: DataTypes.DECIMAL(18, 2),
      allowNull: false,
      defaultValue: 0
    },
    sent_at: {
      type: DataTypes.DATE,
      allowNull: true
    },
    sent_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: 'vendor_quotation_request',
    // This table was migrated with camelCase timestamp columns (createdAt/updatedAt).
    // Override the global Sequelize setting (underscored: true) for this model only,
    // otherwise Sequelize will query created_at/updated_at which don't exist.
    timestamps: true,
    underscored: false,
    createdAt: 'createdAt',
    updatedAt: 'updatedAt',
    indexes: [
      {
        unique: true,
        fields: ['company_id', 'vqr_number']
      },
      {
        fields: ['rfq_id']
      },
      {
        fields: ['vendor_id']
      },
      {
        fields: ['status']
      }
    ]
  });

  VendorQuotationRequest.associate = function(models) {
    VendorQuotationRequest.belongsTo(models.Company, { foreignKey: 'company_id', as: 'company' });
    VendorQuotationRequest.belongsTo(models.Plant, { foreignKey: 'plant_id', as: 'plant' });
    VendorQuotationRequest.belongsTo(models.FiscalYear, { foreignKey: 'fiscal_year_id', as: 'fiscalYear' });
    VendorQuotationRequest.belongsTo(models.RequestForQuotation, { foreignKey: 'rfq_id', as: 'rfq' });
    VendorQuotationRequest.belongsTo(models.BusinessPartner, { foreignKey: 'vendor_id', as: 'vendor' });
    VendorQuotationRequest.belongsTo(models.Currency, { foreignKey: 'currency_id', as: 'currency' });
    VendorQuotationRequest.hasMany(models.VendorQuotationRequestItem, { foreignKey: 'vqr_id', as: 'items' });
    VendorQuotationRequest.belongsTo(models.User, { foreignKey: 'sent_by', as: 'sentByUser' });
    VendorQuotationRequest.belongsTo(models.User, { foreignKey: 'created_by', as: 'createdByUser' });
    VendorQuotationRequest.belongsTo(models.User, { foreignKey: 'updated_by', as: 'updatedByUser' });
  };

  return VendorQuotationRequest;
};

