const Users = require("./users");
const Projects = require("./projects");
const Companies = require("./companies");
const UserProjects = require("./user-projects");
const SafetyTools = require("./safety-tools");
const Reports = require("./reports");
const SDS = require("./sds");
const AssignReport = require("./assign-report");
const EntranceLog = require("./entrance-log");
const ActivityLog = require("./activity-log");
const Notification = require("./notifications");
const RiskAssessment = require("./risk-assessment");
const JobSafetyAnalysis = require("./job-safety-analysis");
const Subscriptions = require("./subscriptions");
const MinutesOfMeeting = require("./minutes-of-meeting");
const PermitToWork = require("./permit-to-work");
const HazopStudy = require("./hazop-study");
const HazopAction = require("./hazop-action");
const TrainingVideo = require("./training-video");
const TrainingEnrollment = require("./training-enrollment");
const TrainingCourse = require("./training-course");
const TrainingModule = require("./training-module");
const TrainingLesson = require("./training-lesson");
const TrainingQuiz = require("./training-quiz");
const TrainingCourseEnrollment = require("./training-course-enrollment");
const TrainingModuleProgress = require("./training-module-progress");
const TrainingLessonProgress = require("./training-lesson-progress");
const LeaveRequest = require("./leave-request");

SafetyTools.belongsTo(Projects);
RiskAssessment.belongsTo(Projects);
JobSafetyAnalysis.belongsTo(Projects);
MinutesOfMeeting.belongsTo(Projects);
PermitToWork.belongsTo(Projects);
HazopStudy.belongsTo(Projects);
HazopStudy.belongsTo(Users, { as: "CreatedBy", foreignKey: "createdById" });
HazopAction.belongsTo(HazopStudy);
HazopAction.belongsTo(Users, { as: "Owner", foreignKey: "ownerId" });
HazopAction.belongsTo(Users, { as: "ClosedBy", foreignKey: "closedById" });
Projects.belongsTo(Companies);
Projects.belongsToMany(Users, { through: UserProjects });
Reports.belongsTo(Projects);
SDS.belongsTo(Projects);
Users.belongsTo(Companies);
Users.belongsToMany(Projects, { through: UserProjects });
EntranceLog.belongsTo(Users);
EntranceLog.belongsTo(Projects);

UserProjects.belongsTo(Projects);
Projects.hasMany(UserProjects);

UserProjects.belongsTo(Users);
Users.hasMany(UserProjects);

AssignReport.belongsTo(Reports);
Reports.hasMany(AssignReport);

AssignReport.belongsTo(Users);
Users.hasMany(AssignReport);

ActivityLog.belongsTo(Users, { foreignKey: "userId" });
Users.hasMany(ActivityLog, { foreignKey: "userId" });

ActivityLog.belongsTo(Companies, { foreignKey: "companyId" });
Companies.hasMany(ActivityLog, { foreignKey: "companyId" });

ActivityLog.belongsTo(Projects, { foreignKey: "projectId" });
Projects.hasMany(ActivityLog, { foreignKey: "projectId" });

Notification.belongsTo(Users, { as: "User", foreignKey: "userId" });
Users.hasMany(Notification, { as: "Notifications", foreignKey: "userId" });

Notification.belongsTo(Users, { as: "Assigner", foreignKey: "assignedBy" });
Users.hasMany(Notification, {
  as: "AssignedNotifications",
  foreignKey: "assignedBy",
});

Notification.belongsTo(Companies, { foreignKey: "companyId" });
Companies.hasMany(Notification, { foreignKey: "companyId" });

Notification.belongsTo(Projects, { foreignKey: "projectId" });
Projects.hasMany(Notification, { foreignKey: "projectId" });

Subscriptions.belongsTo(Companies);
Companies.hasMany(Subscriptions);

TrainingEnrollment.belongsTo(Users);
Users.hasMany(TrainingEnrollment);
TrainingEnrollment.belongsTo(TrainingVideo);
TrainingVideo.hasMany(TrainingEnrollment);

// New Training System Associations
TrainingCourse.hasMany(TrainingModule, { foreignKey: 'TrainingCourseId' });
TrainingModule.belongsTo(TrainingCourse, { foreignKey: 'TrainingCourseId' });

TrainingModule.hasMany(TrainingLesson, { foreignKey: 'TrainingModuleId' });
TrainingLesson.belongsTo(TrainingModule, { foreignKey: 'TrainingModuleId' });

TrainingModule.hasMany(TrainingQuiz, { foreignKey: 'TrainingModuleId' });
TrainingQuiz.belongsTo(TrainingModule, { foreignKey: 'TrainingModuleId' });

TrainingCourseEnrollment.belongsTo(Users, { foreignKey: 'UserId' });
Users.hasMany(TrainingCourseEnrollment, { foreignKey: 'UserId' });
TrainingCourseEnrollment.belongsTo(TrainingCourse, { foreignKey: 'TrainingCourseId' });
TrainingCourse.hasMany(TrainingCourseEnrollment, { foreignKey: 'TrainingCourseId' });

TrainingModuleProgress.belongsTo(Users, { foreignKey: 'UserId' });
Users.hasMany(TrainingModuleProgress, { foreignKey: 'UserId' });
TrainingModuleProgress.belongsTo(TrainingModule, { foreignKey: 'TrainingModuleId' });
TrainingModule.hasMany(TrainingModuleProgress, { foreignKey: 'TrainingModuleId' });
TrainingModuleProgress.belongsTo(TrainingCourseEnrollment, { foreignKey: 'TrainingCourseEnrollmentId' });
TrainingCourseEnrollment.hasMany(TrainingModuleProgress, { foreignKey: 'TrainingCourseEnrollmentId' });

TrainingLessonProgress.belongsTo(Users, { foreignKey: 'UserId' });
Users.hasMany(TrainingLessonProgress, { foreignKey: 'UserId' });
TrainingLessonProgress.belongsTo(TrainingLesson, { foreignKey: 'TrainingLessonId' });
TrainingLesson.hasMany(TrainingLessonProgress, { foreignKey: 'TrainingLessonId' });

LeaveRequest.belongsTo(Users, { foreignKey: "UserId" });
Users.hasMany(LeaveRequest, { foreignKey: "UserId" });
LeaveRequest.belongsTo(Users, { as: "Reviewer", foreignKey: "ReviewedById" });
LeaveRequest.belongsTo(Companies, { foreignKey: "CompanyId" });
Companies.hasMany(LeaveRequest, { foreignKey: "CompanyId" });

module.exports = {
  Users,
  Projects,
  Companies,
  UserProjects,
  SafetyTools,
  Reports,
  SDS,
  AssignReport,
  EntranceLog,
  ActivityLog,
  Notification,
  RiskAssessment,
  JobSafetyAnalysis,
  Subscriptions,
  MinutesOfMeeting,
  PermitToWork,
  HazopStudy,
  HazopAction,
  TrainingVideo,
  TrainingEnrollment,
  TrainingCourse,
  TrainingModule,
  TrainingLesson,
  TrainingQuiz,
  TrainingCourseEnrollment,
  TrainingModuleProgress,
  TrainingLessonProgress,
  LeaveRequest,
};
