const { sequelize } = require('../config/database');
const User = require('./User');
const Route = require('./Route');
const Vehicle = require('./Vehicle');
const Schedule = require('./Schedule');
const Customer = require('./Customer');
const Booking = require('./Booking');
const Payment = require('./Payment');
const Notification = require('./Notification');
const AuditLog = require('./AuditLog');
const Setting = require('./Setting');
const Driver = require('./Driver');
const Review = require('./Review');
const Promotion = require('./Promotion');
const PushToken = require('./PushToken');
const UserNotification = require('./UserNotification');
const Stop = require('./Stop');
const RouteStop = require('./RouteStop');
const SegmentPrice = require('./SegmentPrice');
const BookingSegment = require('./BookingSegment');
const ScheduleStop = require('./ScheduleStop');

Route.hasMany(Schedule, { foreignKey: 'route_id' });
Schedule.belongsTo(Route, { foreignKey: 'route_id' });

Vehicle.hasMany(Schedule, { foreignKey: 'vehicle_id' });
Vehicle.hasMany(Driver, { foreignKey: 'vehicle_id' });
Schedule.belongsTo(Vehicle, { foreignKey: 'vehicle_id' });

Schedule.hasMany(Booking, { foreignKey: 'schedule_id' });
Booking.belongsTo(Schedule, { foreignKey: 'schedule_id' });

Customer.hasMany(Booking, { foreignKey: 'customer_id' });
Customer.hasMany(Review, { foreignKey: 'customer_id' });
Booking.belongsTo(Customer, { foreignKey: 'customer_id' });

Booking.hasMany(Payment, { foreignKey: 'booking_id' });
Payment.belongsTo(Booking, { foreignKey: 'booking_id' });

Booking.hasMany(Notification, { foreignKey: 'booking_id' });
Notification.belongsTo(Booking, { foreignKey: 'booking_id' });

Booking.hasMany(Review, { foreignKey: 'booking_id' });
Review.belongsTo(Booking, { foreignKey: 'booking_id' });
Review.belongsTo(Customer, { foreignKey: 'customer_id' });

Driver.belongsTo(Vehicle, { foreignKey: 'vehicle_id' });

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

Route.belongsToMany(Stop, { through: RouteStop, foreignKey: 'route_id', otherKey: 'stop_id' });
Stop.belongsToMany(Route, { through: RouteStop, foreignKey: 'stop_id', otherKey: 'route_id' });
RouteStop.belongsTo(Route, { foreignKey: 'route_id' });
RouteStop.belongsTo(Stop, { foreignKey: 'stop_id' });

Route.hasMany(SegmentPrice, { foreignKey: 'route_id' });
SegmentPrice.belongsTo(Route, { foreignKey: 'route_id' });
SegmentPrice.belongsTo(Stop, { as: 'fromStop', foreignKey: 'from_stop_id' });
SegmentPrice.belongsTo(Stop, { as: 'toStop', foreignKey: 'to_stop_id' });

Booking.hasMany(BookingSegment, { foreignKey: 'booking_id' });
BookingSegment.belongsTo(Booking, { foreignKey: 'booking_id' });
BookingSegment.belongsTo(Stop, { as: 'fromStop', foreignKey: 'from_stop_id' });
BookingSegment.belongsTo(Stop, { as: 'toStop', foreignKey: 'to_stop_id' });

Schedule.hasMany(ScheduleStop, { foreignKey: 'schedule_id' });
ScheduleStop.belongsTo(Schedule, { foreignKey: 'schedule_id' });
ScheduleStop.belongsTo(Stop, { foreignKey: 'stop_id' });

module.exports = {
  sequelize,
  User,
  Route,
  Vehicle,
  Schedule,
  Customer,
  Booking,
  Payment,
  Notification,
  AuditLog,
  Setting,
  Driver,
  Review,
  Promotion,
  PushToken,
  UserNotification,
  Stop,
  RouteStop,
  SegmentPrice,
  BookingSegment,
  ScheduleStop,
};
