import sequelize from '../db.js';
import defineUser from './User.js';
import defineVehicle from './Vehicle.js';
import defineService from './Service.js';
import defineBooking from './Booking.js';
import defineJobCard from './JobCard.js';
import defineJobTask from './JobTask.js';
import defineTaskImage from './TaskImage.js';
import defineOtpCode from './OtpCode.js';
import defineAttendance from './Attendance.js';

const User = defineUser(sequelize);
const Vehicle = defineVehicle(sequelize);
const Service = defineService(sequelize);
const Booking = defineBooking(sequelize);
const JobCard = defineJobCard(sequelize);
const JobTask = defineJobTask(sequelize);
const TaskImage = defineTaskImage(sequelize);
const OtpCode = defineOtpCode(sequelize);
const Attendance = defineAttendance(sequelize);

User.hasMany(Vehicle, { foreignKey: 'user_id', as: 'vehicles' });
Vehicle.belongsTo(User, { foreignKey: 'user_id', as: 'owner' });

User.hasMany(Booking, { foreignKey: 'user_id', as: 'bookings' });
Booking.belongsTo(User, { foreignKey: 'user_id', as: 'customer' });
Booking.belongsTo(Vehicle, { foreignKey: 'vehicle_id', as: 'vehicle' });
Booking.belongsTo(Service, { foreignKey: 'service_id', as: 'service' });

Booking.hasOne(JobCard, { foreignKey: 'booking_id', as: 'job_card' });
JobCard.belongsTo(Booking, { foreignKey: 'booking_id', as: 'booking' });

JobCard.hasMany(JobTask, { foreignKey: 'job_card_id', as: 'tasks' });
JobTask.belongsTo(JobCard, { foreignKey: 'job_card_id', as: 'job_card' });
JobTask.belongsTo(Service, { foreignKey: 'service_id', as: 'service' });
JobTask.belongsTo(User, { foreignKey: 'employee_id', as: 'employee' });

JobTask.hasMany(TaskImage, { foreignKey: 'job_task_id', as: 'images' });
TaskImage.belongsTo(JobTask, { foreignKey: 'job_task_id', as: 'task' });

User.hasMany(Attendance, { foreignKey: 'user_id', as: 'attendance' });
Attendance.belongsTo(User, { foreignKey: 'user_id', as: 'user' });

export {
  sequelize,
  User,
  Vehicle,
  Service,
  Booking,
  JobCard,
  JobTask,
  TaskImage,
  OtpCode,
  Attendance,
};
