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';
import { DealerClaimDetails } from './DealerClaimDetails';
import { DealerProposalDetails } from './DealerProposalDetails';
import { DealerCompletionDetails } from './DealerCompletionDetails';
import { DealerProposalCostItem } from './DealerProposalCostItem';
import { InternalOrder } from './InternalOrder';
import { ClaimBudgetTracking } from './ClaimBudgetTracking';
import { Dealer } from './Dealer';
import { ActivityType } from './ActivityType';
import { DealerClaimHistory } from './DealerClaimHistory';
import { WorkflowTemplate } from './WorkflowTemplate';
import { ClaimInvoice } from './ClaimInvoice';
import { ClaimInvoiceItem } from './ClaimInvoiceItem';
import { ClaimCreditNote } from './ClaimCreditNote';
import { ClaimCreditNoteItem } from './ClaimCreditNoteItem';
import { Form16aSubmission } from './Form16aSubmission';
import { Form16CreditNote } from './Form16CreditNote';
import { Form16DebitNote } from './Form16DebitNote';
import { Tds26asEntry } from './Tds26asEntry';
import { Form1626asUploadLog } from './Form1626asUploadLog';
import { Form16NonSubmittedNotification } from './Form16NonSubmittedNotification';
import { Form1626asQuarterSnapshot } from './Form1626asQuarterSnapshot';
import { Form16QuarterStatus } from './Form16QuarterStatus';
import { Form16LedgerEntry } from './Form16LedgerEntry';
import { Form16SapResponse } from './Form16SapResponse';
import { Form16DebitNoteSapResponse } from './Form16DebitNoteSapResponse';
import { From16SapReadFile } from './From16SapReadFile';
import { CpcDocument } from './CpcDocument';
import { CpcAuditLog } from './CpcAuditLog';


// 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'
  });

  // InternalOrder associations
  WorkflowRequest.hasOne(InternalOrder, {
    as: 'internalOrder',
    foreignKey: 'requestId',
    sourceKey: 'requestId'
  });

  // ClaimBudgetTracking associations
  WorkflowRequest.hasOne(ClaimBudgetTracking, {
    as: 'budgetTracking',
    foreignKey: 'requestId',
    sourceKey: 'requestId'
  });

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

  // Form 16 associations
  WorkflowRequest.hasOne(Form16aSubmission, {
    as: 'form16Submission',
    foreignKey: 'requestId',
    sourceKey: 'requestId'
  });

  Form16aSubmission.hasMany(Form16CreditNote, {
    as: 'creditNotes',
    foreignKey: 'submissionId',
    sourceKey: 'id'
  });

  Form16CreditNote.hasOne(Form16DebitNote, {
    as: 'debitNote',
    foreignKey: 'creditNoteId',
    sourceKey: 'id'
  });

  // ClaimCreditNote associations
  ClaimCreditNote.hasMany(ClaimCreditNoteItem, {
    as: 'items',
    foreignKey: 'creditNoteId',
    sourceKey: 'creditNoteId'
  });

  // 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

  // CPC-CSD associations
  CpcDocument.hasMany(CpcAuditLog, {
    as: 'auditLogs',
    foreignKey: 'documentId',
    sourceKey: 'id'
  });
};

// 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,
  WorkflowTemplate,
  DealerClaimDetails,
  DealerProposalDetails,
  DealerCompletionDetails,
  DealerProposalCostItem,
  InternalOrder,
  ClaimBudgetTracking,
  Dealer,
  ActivityType,
  DealerClaimHistory,
  ClaimInvoice,
  ClaimInvoiceItem,
  ClaimCreditNote,
  ClaimCreditNoteItem,
  Form16aSubmission,
  Form16CreditNote,
  Form16DebitNote,
  Tds26asEntry,
  Form1626asUploadLog,
  Form16NonSubmittedNotification,
  Form1626asQuarterSnapshot,
  Form16QuarterStatus,
  Form16LedgerEntry,
  Form16SapResponse,
  Form16DebitNoteSapResponse,
  From16SapReadFile,
  CpcDocument,
  CpcAuditLog
};

// Export default sequelize instance
export default sequelize;
