const {
  BOOLEAN,
  DECIMAL,
  DOUBLE,
  ENUM,
  INTEGER,
  JSON,
  Sequelize,
  STRING,
  TEXT,
  TIME,
  UUID,
  UUIDV4,
} = require('sequelize');

module.exports = (sequelize, DataTypes) => {
  const Listings = sequelize.define(
    'Listings',
    {
      id: {
        type: UUID,
        defaultValue: UUIDV4,
        allowNull: false,
        primaryKey: true,
      },
      host_id: {
        type: UUID,
        allowNull: false,
      },
      name: {
        type: STRING(50),
        allowNull: false,
      },
      description: {
        type: TEXT,
        allowNull: true,
      },
      longitude: {
        type: DOUBLE,
        allowNull: false,
      },
      latitude: {
        type: DOUBLE,
        allowNull: false,
      },
      price: {
        type: DECIMAL(10, 2),
        allowNull: false,
        defaultValue: 0.0,
      },
      min_capacity: {
        type: INTEGER,
        allowNull: false,
        defaultValue: 1,
      },
      max_capacity: {
        type: INTEGER,
        allowNull: false,
        defaultValue: 1,
      },
      amenities: {
        type: JSON,
        allowNull: true,
      },
      services: {
        type: JSON,
        allowNull: true,
      },
      check_in_time: {
        type: TIME,
        allowNull: false,
      },
      check_out_time: {
        type: TIME,
        allowNull: false,
      },
      status: {
        type: ENUM('For Approval', 'Approved', 'Declined', 'Cancelled', 'Active', 'Inactive'),
        allowNull: false,
        defaultValue: 'For Approval',
      },
      is_deleted: {
        type: BOOLEAN,
        defaultValue: false,
      },
      fullname: {
        type: STRING,
        allowNull: true,
      },
      business_type: {
        type: STRING,
        allowNull: true,
      },
      business_name: {
        type: STRING,
        allowNull: true,
      },
      business_address: {
        type: STRING,
        allowNull: true,
      },
      listing_type: {
        type: ENUM('Public', 'Private'),
        allowNull: true,
      },
      location: {
        type: STRING,
        allowNull: true,
      },
      extra_guest_fee: {
        type: DECIMAL(10, 2),
        allowNull: true,
      },
      cleaning_fee: {
        type: DECIMAL(10, 2),
        allowNull: true,
      },
      smoking_policy: {
        type: TEXT,
        allowNull: true,
      },
      pets_policy: {
        type: TEXT,
        allowNull: true,
      },
      quiet_hours_start: {
        type: TIME,
        allowNull: true,
      },
      quiet_hours_end: {
        type: TIME,
        allowNull: true,
      },
      security_deposit: {
        type: BOOLEAN,
        allowNull: true,
      },
      security_deposit_price: {
        type: DECIMAL(10, 2),
        allowNull: true,
      },
      tents: {
        type: INTEGER,
        allowNull: false,
        defaultValue: 0,
      },
      vans: {
        type: INTEGER,
        allowNull: false,
        defaultValue: 0,
      },
      approval: {
        type: ENUM('Auto', 'Manual'),
        allowNull: true,
      },
      slots: {
        type: INTEGER,
        allowNull: true,
        defaultValue: 1,
      },
      address_country: {
        type: STRING,
        allowNull: false,
      },
      address_street: {
        type: STRING,
        allowNull: false,
      },
      address_apt: {
        type: STRING,
        allowNull: true,
      },
      address_city: {
        type: STRING,
        allowNull: false,
      },
      address_state: {
        type: STRING,
        allowNull: false,
      },
      address_zip: {
        type: STRING,
        allowNull: false,
      },
      safety_and_security: {
        type: TEXT,
        allowNull: true,
      },
      hosting_type: {
        type: STRING,
        allowNull: true,
      },
      notify_host: {
        type: BOOLEAN,
        allowNull: false,
        defaultValue: 0,
      },
      birth_date: {
        type: DataTypes.DATEONLY,
        allowNull: true,
      },
      birth_place: {
        type: DataTypes.STRING,
        allowNull: true,
      },
      createdAt: {
        type: DataTypes.DATE,
        allowNull: false,
        defaultValue: Sequelize.literal('CURRENT_TIMESTAMP'),
      },
    },
    {
      tableName: 'listings',
      timestamps: true,
    },
  );

  Listings.hasMany(sequelize.models.ListingImages, {
    foreignKey: 'listing_id',
    onDelete: 'CASCADE',
    as: 'listingImages',
  });

  Listings.hasMany(sequelize.models.ListingReviews, {
    foreignKey: 'listing_id',
    onDelete: 'CASCADE',
    as: 'listingReviews',
  });

  Listings.associate = function (models) {
    Listings.belongsTo(models.Users, {
      foreignKey: 'host_id',
      as: 'host',
    });

    Listings.hasMany(models.ListingSpots, {
      foreignKey: 'listing_id',
      as: 'listingSpots',
    });

    Listings.hasMany(models.ListingFiles, {
      foreignKey: 'listing_id',
      as: 'files',
    });

    Listings.hasMany(models.ListingAddOns, {
      foreignKey: 'listing_id',
      as: 'addOns',
    });

    Listings.hasMany(models.ListingDiscounts, {
      foreignKey: 'listing_id',
      as: 'discounts',
    });
  };

  return Listings;
};
