"use strict";

const cdnHelper = require("../../helpers/cdn.helper");

module.exports = function (sequelize, DataTypes) {
  const Venue = sequelize.define(
    "Venue",
    {
      id: {
        type: DataTypes.UUID,
        defaultValue: DataTypes.UUIDV4,
        primaryKey: true,
      },
      name: {
        type: DataTypes.STRING,
        allowNull: false,
      },
      description: {
        type: DataTypes.STRING,
      },
      address: {
        type: DataTypes.STRING,
        allowNull: false,
      },
      city: {
        type: DataTypes.STRING,
        allowNull: false,
      },
      country: {
        type: DataTypes.STRING,
      },
      state: {
        type: DataTypes.STRING,
      },
      postal_code: {
        type: DataTypes.STRING,
      },
      accessibility: {
        type: DataTypes.STRING,
      },
      gmt: {
        type: DataTypes.INTEGER,
        allowNull: false,
        defaultValue: 1,
      },
      latitude: {
        type: DataTypes.DECIMAL(10, 8),
      },
      longitude: {
        type: DataTypes.DECIMAL(11, 8),
      },
      image_url: {
        type: DataTypes.VIRTUAL,
        get: function () {
          const city = this.get("city");
          return cdnHelper.getCDNCityMediaUrl(city);
        },
      },
      tz: {
        type: DataTypes.STRING,
      },
      capacity: {
        type: DataTypes.INTEGER,
      },
    },
    {
      tableName: "venues",
      freezeTableName: true,
      timestamps: true,
    }
  );

  Venue.associate = function (models) {
    Venue.Events = Venue.hasMany(models.Event, { foreignKey: "venueId", as: "events" });
  };

  return Venue;
};
