const sequelize = require('../config/database');
const User = require('./User');
const Book = require('./Book');
const Order = require('./Order');
const OrderItem = require('./OrderItem');
const Voucher = require('./Voucher');
const Review = require('./Review');
const Wishlist = require('./Wishlist');
const Purchase = require('./Purchase');
const PurchaseItem = require('./PurchaseItem');
const StoreSetting = require('./StoreSetting');

// Associations
const Address = require('./Address'); // Add this
const Notification = require('./Notification');
const Banner = require('./Banner');
const Message = require('./Message');

// ... (User, Order, etc. associations remain unchanged)
User.hasMany(Address, { foreignKey: 'user_id' });
Address.belongsTo(User, { foreignKey: 'user_id' });

User.hasMany(Notification, { foreignKey: 'user_id' });
Notification.belongsTo(User, { foreignKey: 'user_id' });

// Messages - User as sender
User.hasMany(Message, { foreignKey: 'user_id', as: 'sentMessages' });
Message.belongsTo(User, { foreignKey: 'user_id', as: 'sender' });

module.exports = {
    sequelize,
    User,
    Book,
    Order,
    OrderItem,
    Voucher,
    Review,
    Wishlist,
    Purchase,
    PurchaseItem,
    StoreSetting,
    Address,
    Notification,
    Banner,
    Message
};

// Order - User
User.hasMany(Order, { foreignKey: 'user_id' });
Order.belongsTo(User, { foreignKey: 'user_id' });

// Order - OrderItem
Order.hasMany(OrderItem, { foreignKey: 'order_id' });
OrderItem.belongsTo(Order, { foreignKey: 'order_id' });

// Book - OrderItem
Book.hasMany(OrderItem, { foreignKey: 'book_id' });
OrderItem.belongsTo(Book, { foreignKey: 'book_id' });

// Reviews
User.hasMany(Review, { foreignKey: 'user_id' });
Review.belongsTo(User, { foreignKey: 'user_id' });
Book.hasMany(Review, { foreignKey: 'book_id' });
Review.belongsTo(Book, { foreignKey: 'book_id' });

// Wishlist
User.hasMany(Wishlist, { foreignKey: 'user_id' });
Wishlist.belongsTo(User, { foreignKey: 'user_id' });
Book.hasMany(Wishlist, { foreignKey: 'book_id' });
Wishlist.belongsTo(Book, { foreignKey: 'book_id' });

// Purchases
Purchase.hasMany(PurchaseItem, { foreignKey: 'purchase_id' });
PurchaseItem.belongsTo(Purchase, { foreignKey: 'purchase_id' });

Book.hasMany(PurchaseItem, { foreignKey: 'book_id' });
PurchaseItem.belongsTo(Book, { foreignKey: 'book_id' });

module.exports = {
    sequelize,
    User,
    Book,
    Order,
    OrderItem,
    Voucher,
    Review,
    Wishlist,
    Purchase,
    PurchaseItem,
    StoreSetting,
    Address,
    Notification,
    Banner
};
