module.exports = function(sequelize, DataTypes) {
  const booking_requests = sequelize.define(
    'booking_requests',
    {
      id: {
        type: DataTypes.UUID,
        defaultValue: DataTypes.UUIDV4,
        primaryKey: true,
      },

      request_code: {
        type: DataTypes.TEXT,
      },

      status: {
        type: DataTypes.ENUM,
        values: [
          'draft',
          'submitted',
          'in_review',
          'changes_requested',
          'approved',
          'rejected',
          'expired',
          'converted_to_reservation',
          'canceled',
        ],
      },

      check_in_at: {
        type: DataTypes.DATE,
      },

      check_out_at: {
        type: DataTypes.DATE,
      },

      preferred_bedrooms: {
        type: DataTypes.INTEGER,
      },

      guest_count: {
        type: DataTypes.INTEGER,
      },

      purpose_of_stay: {
        type: DataTypes.TEXT,
      },

      special_requirements: {
        type: DataTypes.TEXT,
      },

      budget_code: {
        type: DataTypes.TEXT,
      },

      max_budget_amount: {
        type: DataTypes.DECIMAL,
      },

      currency: {
        type: DataTypes.TEXT,
      },

      importHash: {
        type: DataTypes.STRING(255),
        allowNull: true,
        unique: true,
      },
    },
    {
      timestamps: true,
      paranoid: true,
      freezeTableName: true,
    },
  );

  booking_requests.associate = (db) => {
    db.booking_requests.hasMany(db.booking_request_travelers, {
      as: 'travelers',
      foreignKey: {
        name: 'booking_requestId',
      },
      constraints: false,
    });

    db.booking_requests.hasMany(db.booking_request_travelers, {
      as: 'travelers_filter',
      foreignKey: {
        name: 'booking_requestId',
      },
      constraints: false,
    });

    db.booking_requests.hasMany(db.approval_steps, {
      as: 'approval_steps',
      foreignKey: {
        name: 'booking_requestId',
      },
      constraints: false,
    });

    db.booking_requests.hasMany(db.approval_steps, {
      as: 'approval_steps_filter',
      foreignKey: {
        name: 'booking_requestId',
      },
      constraints: false,
    });

    db.booking_requests.hasMany(db.documents, {
      as: 'documents',
      foreignKey: {
        name: 'booking_requestId',
      },
      constraints: false,
    });

    db.booking_requests.hasMany(db.documents, {
      as: 'documents_filter',
      foreignKey: {
        name: 'booking_requestId',
      },
      constraints: false,
    });

    db.booking_requests.hasMany(db.activity_comments, {
      as: 'comments',
      foreignKey: {
        name: 'booking_requestId',
      },
      constraints: false,
    });

    db.booking_requests.hasMany(db.activity_comments, {
      as: 'comments_filter',
      foreignKey: {
        name: 'booking_requestId',
      },
      constraints: false,
    });

    db.booking_requests.hasMany(db.booking_request_travelers, {
      as: 'booking_request_travelers_booking_request',
      foreignKey: {
        name: 'booking_requestId',
      },
      constraints: false,
    });

    db.booking_requests.hasMany(db.approval_steps, {
      as: 'approval_steps_booking_request',
      foreignKey: {
        name: 'booking_requestId',
      },
      constraints: false,
    });

    db.booking_requests.hasMany(db.reservations, {
      as: 'reservations_booking_request',
      foreignKey: {
        name: 'booking_requestId',
      },
      constraints: false,
    });

    db.booking_requests.hasMany(db.documents, {
      as: 'documents_booking_request',
      foreignKey: {
        name: 'booking_requestId',
      },
      constraints: false,
    });

    db.booking_requests.hasMany(db.activity_comments, {
      as: 'activity_comments_booking_request',
      foreignKey: {
        name: 'booking_requestId',
      },
      constraints: false,
    });

    db.booking_requests.belongsTo(db.tenants, {
      as: 'tenant',
      foreignKey: {
        name: 'tenantId',
      },
      constraints: false,
    });

    db.booking_requests.belongsTo(db.organizations, {
      as: 'organization',
      foreignKey: {
        name: 'organizationId',
      },
      constraints: false,
    });

    db.booking_requests.belongsTo(db.users, {
      as: 'requested_by',
      foreignKey: {
        name: 'requested_byId',
      },
      constraints: false,
    });

    db.booking_requests.belongsTo(db.properties, {
      as: 'preferred_property',
      foreignKey: {
        name: 'preferred_propertyId',
      },
      constraints: false,
    });

    db.booking_requests.belongsTo(db.unit_types, {
      as: 'preferred_unit_type',
      foreignKey: {
        name: 'preferred_unit_typeId',
      },
      constraints: false,
    });

    db.booking_requests.belongsTo(db.users, {
      as: 'createdBy',
    });

    db.booking_requests.belongsTo(db.users, {
      as: 'updatedBy',
    });
  };

  return booking_requests;
};
