import { Controller, Post, Get, Body, Res, HttpCode, HttpStatus } from "@nestjs/common";
import { Response } from "express";
import { LlmService } from "./llm.service";
import { ChatRequestDto, ChatResponseDto, EmbedRequestDto, EmbedResponseDto } from "./dto";
import type { LlmProviderHealthStatus } from "./providers/llm-provider.interface";

@Controller("llm")
export class LlmController {
  constructor(private readonly llmService: LlmService) {}

  @Get("health")
  async health(): Promise<LlmProviderHealthStatus> {
    return this.llmService.checkHealth();
  }
  @Get("models") async listModels(): Promise<{ models: string[] }> {
    return { models: await this.llmService.listModels() };
  }
  @Post("chat") @HttpCode(HttpStatus.OK) async chat(
    @Body() req: ChatRequestDto,
    @Res({ passthrough: true }) res: Response
  ): Promise<ChatResponseDto | undefined> {
    if (req.stream === true) {
      res.setHeader("Content-Type", "text/event-stream");
      res.setHeader("Cache-Control", "no-cache");
      res.setHeader("Connection", "keep-alive");
      res.setHeader("X-Accel-Buffering", "no");
      try {
        for await (const c of this.llmService.chatStream(req))
          res.write("data: " + JSON.stringify(c) + "\n\n");
        res.write("data: [DONE]\n\n");
        res.end();
      } catch (e: unknown) {
        res.write(
          "data: " + JSON.stringify({ error: e instanceof Error ? e.message : String(e) }) + "\n\n"
        );
        res.end();
      }
      return;
    }
    return this.llmService.chat(req);
  }
  @Post("embed") @HttpCode(HttpStatus.OK) async embed(
    @Body() req: EmbedRequestDto
  ): Promise<EmbedResponseDto> {
    return this.llmService.embed(req);
  }
}
