import { Module } from '@nestjs/common'
import { ConfigModule, ConfigService } from '@nestjs/config'
import { MongooseModule } from '@nestjs/mongoose'
import {
  ClientFolder, ClientFolderSchema,
  Matter, MatterSchema,
  Party, PartySchema,
  Hearing, HearingSchema,
  LegalDocument, LegalDocumentSchema,
  TimeEntry, TimeEntrySchema,
  ConflictCheck, ConflictCheckSchema,
  Invoice, InvoiceSchema,
  Expense, ExpenseSchema,
  TrustAccount, TrustAccountSchema,
  TrustTransaction, TrustTransactionSchema,
  Email, EmailSchema,
  UserEmailAccount, UserEmailAccountSchema,
  FormTemplate, FormTemplateSchema,
  FormGenerationLog, FormGenerationLogSchema,
  WorkflowTemplate, WorkflowTemplateSchema,
  MatterWorkflow, MatterWorkflowSchema,
  LawCorpusEntry, LawCorpusEntrySchema,
  ObligationRule, ObligationRuleSchema,
  MatterDeadline, MatterDeadlineSchema,
} from './common/schemas'
import { ClientFoldersModule } from './client-folders/client-folders.module'
import { MattersModule } from './matters/matters.module'
import { HearingsModule } from './hearings/hearings.module'
import { DocumentsModule } from './documents/documents.module'
import { RetainerModule } from './retainer/retainer.module'
import { TimeEntriesModule } from './time-entries/time-entries.module'
import { ConflictChecksModule } from './conflict-checks/conflict-checks.module'
import { InvoicesModule } from './invoices/invoices.module'
import { ExpensesModule } from './expenses/expenses.module'
import { TrustModule } from './trust/trust.module'
import { EmailModule } from './email/email.module'
import { AgentModule } from './agent/agent.module'
import { EncryptionModule } from './encryption/encryption.module'
import { FormTemplatesModule } from './form-templates/form-templates.module'
import { WorkflowsModule } from './workflows/workflows.module'
import { RagAgentModule } from './rag-agent/rag-agent.module'
import { MatterDeadlinesModule } from './matter-deadlines/matter-deadlines.module'
import { AnnualComplianceModule } from './annual-compliance/annual-compliance.module'

@Module({
  imports: [
    ConfigModule.forRoot({ isGlobal: true }),
    MongooseModule.forRootAsync({
      useFactory: (config: ConfigService) => ({
        uri: config.get<string>('MONGODB_URI', 'mongodb://localhost:27017/gps_legal'),
      }),
      inject: [ConfigService],
    }),
    MongooseModule.forFeature([
      { name: ClientFolder.name, schema: ClientFolderSchema },
      { name: Matter.name, schema: MatterSchema },
      { name: Party.name, schema: PartySchema },
      { name: Hearing.name, schema: HearingSchema },
      { name: LegalDocument.name, schema: LegalDocumentSchema },
      { name: TimeEntry.name, schema: TimeEntrySchema },
      { name: ConflictCheck.name, schema: ConflictCheckSchema },
      { name: Invoice.name, schema: InvoiceSchema },
      { name: Expense.name,           schema: ExpenseSchema },
      { name: TrustAccount.name,      schema: TrustAccountSchema },
      { name: TrustTransaction.name,  schema: TrustTransactionSchema },
      { name: Email.name,              schema: EmailSchema },
      { name: UserEmailAccount.name,   schema: UserEmailAccountSchema },
      { name: FormTemplate.name,       schema: FormTemplateSchema },
      { name: FormGenerationLog.name,  schema: FormGenerationLogSchema },
      { name: WorkflowTemplate.name,   schema: WorkflowTemplateSchema },
      { name: MatterWorkflow.name,     schema: MatterWorkflowSchema },
      { name: LawCorpusEntry.name,     schema: LawCorpusEntrySchema },
      { name: ObligationRule.name,     schema: ObligationRuleSchema },
      { name: MatterDeadline.name,     schema: MatterDeadlineSchema },
    ]),
    ClientFoldersModule,
    MattersModule,
    HearingsModule,
    DocumentsModule,
    RetainerModule,
    TimeEntriesModule,
    ConflictChecksModule,
    InvoicesModule,
    ExpensesModule,
    TrustModule,
    EncryptionModule,
    EmailModule,
    AgentModule,
    FormTemplatesModule,
    WorkflowsModule,
    RagAgentModule,
    MatterDeadlinesModule,
    AnnualComplianceModule,
  ],
})
export class AppModule {}
