const { sequelize } = require('../config/database');
const User = require('./User');
const Category = require('./Category');
const Service = require('./Service');
const Appointment = require('./Appointment');
const Invitation = require('./Invitation');
const Withdrawal = require('./Withdrawal');
const Notification = require('./Notification');
const Admin = require('./Admin');
const LoginHistory = require('./LoginHistory');
const Translation = require('./Translation');
const Config = require('./Config');
const HotService = require('./HotService');
const PaymentOrder = require('./PaymentOrder');
const Commission = require('./Commission');
const BookingRule = require('./BookingRule');

/**
 * Define model associations
 */

// User associations
User.hasMany(Appointment, { foreignKey: 'user_id', as: 'appointments' });
User.hasMany(Invitation, { foreignKey: 'inviter_id', as: 'invitations' });
User.hasMany(Invitation, { foreignKey: 'invitee_id', as: 'invitedUsers' });
User.hasMany(Withdrawal, { foreignKey: 'user_id', as: 'withdrawals' });
User.hasMany(Notification, { foreignKey: 'user_id', as: 'notifications' });
User.belongsTo(User, { foreignKey: 'invited_by', as: 'inviter' });

// Category associations
Category.hasMany(Service, { foreignKey: 'category_id', as: 'services' });

// Service associations
Service.belongsTo(Category, { foreignKey: 'category_id', as: 'category' });
Service.hasMany(Appointment, { foreignKey: 'service_id', as: 'appointments' });

// Appointment associations
Appointment.belongsTo(User, { foreignKey: 'user_id', as: 'user' });
Appointment.belongsTo(Service, { foreignKey: 'service_id', as: 'service' });
Appointment.belongsTo(HotService, { foreignKey: 'hot_service_id', as: 'hotService' });

// HotService associations
HotService.hasMany(Appointment, { foreignKey: 'hot_service_id', as: 'appointments' });

// Invitation associations
Invitation.belongsTo(User, { foreignKey: 'inviter_id', as: 'inviter' });
Invitation.belongsTo(User, { foreignKey: 'invitee_id', as: 'invitee' });

// Withdrawal associations
Withdrawal.belongsTo(User, { foreignKey: 'user_id', as: 'user' });
Withdrawal.belongsTo(Admin, { foreignKey: 'reviewed_by', as: 'reviewer' });

// Notification associations
Notification.belongsTo(User, { foreignKey: 'user_id', as: 'user' });

// Admin associations
Admin.hasMany(Withdrawal, { foreignKey: 'reviewed_by', as: 'reviewedWithdrawals' });

// LoginHistory associations
LoginHistory.belongsTo(User, { foreignKey: 'user_id', as: 'user' });

// PaymentOrder associations
PaymentOrder.belongsTo(User, { foreignKey: 'user_id', as: 'user' });
PaymentOrder.belongsTo(Appointment, { foreignKey: 'appointment_id', as: 'appointment' });
PaymentOrder.belongsTo(Admin, { foreignKey: 'created_by', as: 'creator' });
PaymentOrder.hasMany(Commission, { foreignKey: 'payment_order_id', as: 'commissions' });
User.hasMany(PaymentOrder, { foreignKey: 'user_id', as: 'paymentOrders' });
Admin.hasMany(PaymentOrder, { foreignKey: 'created_by', as: 'createdPaymentOrders' });

// Commission associations
Commission.belongsTo(User, { foreignKey: 'inviter_id', as: 'inviter' });
Commission.belongsTo(User, { foreignKey: 'invitee_id', as: 'invitee' });
Commission.belongsTo(PaymentOrder, { foreignKey: 'payment_order_id', as: 'paymentOrder' });
User.hasMany(Commission, { foreignKey: 'inviter_id', as: 'earnedCommissions' });
User.hasMany(Commission, { foreignKey: 'invitee_id', as: 'generatedCommissions' });

/**
 * Sync database (create tables if they don't exist)
 * @param {Object} options - Sequelize sync options
 */
const syncDatabase = async (options = {}) => {
  try {
    await sequelize.sync(options);
    return true;
  } catch (error) {
    throw error;
  }
};

module.exports = {
  sequelize,
  User,
  Category,
  Service,
  Appointment,
  Invitation,
  Withdrawal,
  Notification,
  Admin,
  LoginHistory,
  Translation,
  Config,
  HotService,
  PaymentOrder,
  Commission,
  BookingRule,
  syncDatabase,
};
