import { sequelize } from '@config/database';

// Import all models
import { User } from './User';
import { WorkflowRequest } from './WorkflowRequest';
import { ApprovalLevel } from './ApprovalLevel';
import { Participant } from './Participant';
import { Document } from './Document';
import { Subscription } from './Subscription';
import { Activity } from './Activity';
import { WorkNote } from './WorkNote';
import { WorkNoteAttachment } from './WorkNoteAttachment';
import { TatAlert } from './TatAlert';
import { Holiday } from './Holiday';
import { Notification } from './Notification';
import ConclusionRemark from './ConclusionRemark';
import RequestSummary from './RequestSummary';
import SharedSummary from './SharedSummary';

// Define associations
const defineAssociations = () => {
  // User associations
  User.hasMany(WorkflowRequest, {
    as: 'initiatedRequests',
    foreignKey: 'initiatorId',
    sourceKey: 'userId'
  });

  User.hasMany(ApprovalLevel, {
    as: 'approvalLevels',
    foreignKey: 'approverId',
    sourceKey: 'userId'
  });

  User.hasMany(Participant, {
    as: 'participations',
    foreignKey: 'userId',
    sourceKey: 'userId'
  });

  User.hasMany(Document, {
    as: 'uploadedDocuments',
    foreignKey: 'uploadedBy',
    sourceKey: 'userId'
  });

  // WorkflowRequest associations
  WorkflowRequest.hasMany(ApprovalLevel, {
    as: 'approvalLevels',
    foreignKey: 'requestId',
    sourceKey: 'requestId'
  });

  WorkflowRequest.hasMany(Participant, {
    as: 'participants',
    foreignKey: 'requestId',
    sourceKey: 'requestId'
  });

  WorkflowRequest.hasMany(Document, {
    as: 'documents',
    foreignKey: 'requestId',
    sourceKey: 'requestId'
  });

  WorkflowRequest.hasOne(ConclusionRemark, {
    as: 'conclusion',
    foreignKey: 'requestId',
    sourceKey: 'requestId'
  });

  ConclusionRemark.belongsTo(WorkflowRequest, {
    foreignKey: 'requestId',
    targetKey: 'requestId'
  });

  ConclusionRemark.belongsTo(User, {
    as: 'editor',
    foreignKey: 'editedBy',
    targetKey: 'userId'
  });

  // RequestSummary associations
  // Note: belongsTo associations are defined in the model files to avoid duplicate alias conflicts
  // Only hasOne/hasMany associations are defined here
  WorkflowRequest.hasOne(RequestSummary, {
    as: 'summary',
    foreignKey: 'requestId',
    sourceKey: 'requestId'
  });

  RequestSummary.hasMany(SharedSummary, {
    as: 'sharedSummaries',
    foreignKey: 'summaryId',
    sourceKey: 'summaryId'
  });

  // User associations for summaries
  User.hasMany(RequestSummary, {
    as: 'createdSummaries',
    foreignKey: 'initiatorId',
    sourceKey: 'userId'
  });

  User.hasMany(SharedSummary, {
    as: 'sharedByMe',
    foreignKey: 'sharedBy',
    sourceKey: 'userId'
  });

  User.hasMany(SharedSummary, {
    as: 'sharedWithMe',
    foreignKey: 'sharedWith',
    sourceKey: 'userId'
  });

  // Note: belongsTo associations are defined in individual model files to avoid duplicate alias conflicts
  // Only hasMany associations from WorkflowRequest are defined here since they're one-way
};

// Initialize associations
defineAssociations();

// Export models and sequelize instance
export {
  sequelize,
  User,
  WorkflowRequest,
  ApprovalLevel,
  Participant,
  Document,
  Subscription,
  Activity,
  WorkNote,
  WorkNoteAttachment,
  TatAlert,
  Holiday,
  Notification,
  ConclusionRemark,
  RequestSummary,
  SharedSummary
};

// Export default sequelize instance
export default sequelize;
