const sequelize = require("../config/database");
const User = require("./User");
const Item = require("./Item");
const Rental = require("./Rental");
const Message = require("./Message");
const ForumPost = require("./ForumPost");
const ForumComment = require("./ForumComment");
const PostTag = require("./PostTag");
const UserAddress = require("./UserAddress");
const ConditionCheck = require("./ConditionCheck");
const AlphaInvitation = require("./AlphaInvitation");
const Feedback = require("./Feedback");
const ImageMetadata = require("./ImageMetadata");

User.hasMany(Item, { as: "ownedItems", foreignKey: "ownerId" });
Item.belongsTo(User, { as: "owner", foreignKey: "ownerId" });
Item.belongsTo(User, { as: "deleter", foreignKey: "deletedBy" });

User.hasMany(Rental, { as: "rentalsAsRenter", foreignKey: "renterId" });
User.hasMany(Rental, { as: "rentalsAsOwner", foreignKey: "ownerId" });

Item.hasMany(Rental, { as: "rentals", foreignKey: "itemId" });
Rental.belongsTo(Item, { as: "item", foreignKey: "itemId" });
Rental.belongsTo(User, { as: "renter", foreignKey: "renterId" });
Rental.belongsTo(User, { as: "owner", foreignKey: "ownerId" });

User.hasMany(Message, { as: "sentMessages", foreignKey: "senderId" });
User.hasMany(Message, { as: "receivedMessages", foreignKey: "receiverId" });
Message.belongsTo(User, { as: "sender", foreignKey: "senderId" });
Message.belongsTo(User, { as: "receiver", foreignKey: "receiverId" });

// Forum associations
User.hasMany(ForumPost, { as: "forumPosts", foreignKey: "authorId" });
ForumPost.belongsTo(User, { as: "author", foreignKey: "authorId" });
ForumPost.belongsTo(User, { as: "closer", foreignKey: "closedBy" });

User.hasMany(ForumComment, { as: "forumComments", foreignKey: "authorId" });
ForumComment.belongsTo(User, { as: "author", foreignKey: "authorId" });

ForumPost.hasMany(ForumComment, { as: "comments", foreignKey: "postId" });
ForumComment.belongsTo(ForumPost, { as: "post", foreignKey: "postId" });

// Self-referential association for nested comments
ForumComment.hasMany(ForumComment, { as: "replies", foreignKey: "parentCommentId" });
ForumComment.belongsTo(ForumComment, { as: "parentComment", foreignKey: "parentCommentId" });

ForumPost.hasMany(PostTag, { as: "tags", foreignKey: "postId" });
PostTag.belongsTo(ForumPost, { as: "post", foreignKey: "postId" });

User.hasMany(UserAddress, { as: "addresses", foreignKey: "userId" });
UserAddress.belongsTo(User, { as: "user", foreignKey: "userId" });

// ConditionCheck associations
Rental.hasMany(ConditionCheck, {
  as: "conditionChecks",
  foreignKey: "rentalId",
});
ConditionCheck.belongsTo(Rental, { as: "rental", foreignKey: "rentalId" });
User.hasMany(ConditionCheck, {
  as: "conditionChecks",
  foreignKey: "submittedBy",
});
ConditionCheck.belongsTo(User, {
  as: "submittedByUser",
  foreignKey: "submittedBy",
});

// AlphaInvitation associations
AlphaInvitation.belongsTo(User, {
  as: "user",
  foreignKey: "usedBy",
});
User.hasMany(AlphaInvitation, {
  as: "alphaInvitations",
  foreignKey: "usedBy",
});

// Feedback associations
User.hasMany(Feedback, { as: "feedbacks", foreignKey: "userId" });
Feedback.belongsTo(User, { as: "user", foreignKey: "userId" });

module.exports = {
  sequelize,
  User,
  Item,
  Rental,
  Message,
  ForumPost,
  ForumComment,
  PostTag,
  UserAddress,
  ConditionCheck,
  AlphaInvitation,
  Feedback,
  ImageMetadata,
};
