import { Controller, Get, Post, Body, HttpCode } from '@nestjs/common'
import { RagAgentService, LawRetrievalContext } from './rag-agent.service'

@Controller('rag-agent')
export class RagAgentController {
  constructor(private readonly svc: RagAgentService) {}

  // ── Job 1: Form Retrieval ─────────────────────────────────────────────────

  // Start stock form retrieval run (fire-and-forget — returns 202 immediately)
  @Post('retrieve-forms')
  @HttpCode(202)
  retrieveForms() {
    this.svc.runFormRetrieval().catch(err =>
      console.error('RAG form retrieval failed:', err),
    )
    return { message: 'Form retrieval started — poll /rag-agent/status for progress' }
  }

  // Current form retrieval run status + log
  @Get('status')
  status() {
    return this.svc.getCurrentRun() ?? { message: 'No form retrieval run yet' }
  }

  // ── Job 2: Law Retrieval ──────────────────────────────────────────────────

  // Start a law retrieval run for a given practice area / matter context
  // Body: { practiceArea, keywords?, matterId?, jurisdiction? }
  @Post('fetch-laws')
  @HttpCode(202)
  fetchLaws(@Body() context: LawRetrievalContext) {
    this.svc.runLawRetrieval(context).catch(err =>
      console.error('RAG law retrieval failed:', err),
    )
    return { message: `Law retrieval started for "${context.practiceArea}" — poll /rag-agent/law-status for progress` }
  }

  // Current law retrieval run status + log
  @Get('law-status')
  lawStatus() {
    return this.svc.getCurrentLawRun() ?? { message: 'No law retrieval run yet' }
  }
}
