import { Controller, Post, Get, Body } from "@nestjs/common";
import { OllamaService } from "./ollama.service";
import type {
  GenerateOptionsDto,
  GenerateResponseDto,
  ChatMessage,
  ChatOptionsDto,
  ChatResponseDto,
  EmbedResponseDto,
  ListModelsResponseDto,
  HealthCheckResponseDto,
} from "./dto";

/**
 * Request DTO for generate endpoint
 */
interface GenerateRequestDto {
  prompt: string;
  options?: GenerateOptionsDto;
  model?: string;
}

/**
 * Request DTO for chat endpoint
 */
interface ChatRequestDto {
  messages: ChatMessage[];
  options?: ChatOptionsDto;
  model?: string;
}

/**
 * Request DTO for embed endpoint
 */
interface EmbedRequestDto {
  text: string;
  model?: string;
}

/**
 * Controller for Ollama API endpoints
 * Provides text generation, chat, embeddings, and model management
 */
@Controller("ollama")
export class OllamaController {
  constructor(private readonly ollamaService: OllamaService) {}

  /**
   * Generate text from a prompt
   * POST /ollama/generate
   */
  @Post("generate")
  async generate(@Body() body: GenerateRequestDto): Promise<GenerateResponseDto> {
    return this.ollamaService.generate(body.prompt, body.options, body.model);
  }

  /**
   * Complete a chat conversation
   * POST /ollama/chat
   */
  @Post("chat")
  async chat(@Body() body: ChatRequestDto): Promise<ChatResponseDto> {
    return this.ollamaService.chat(body.messages, body.options, body.model);
  }

  /**
   * Generate embeddings for text
   * POST /ollama/embed
   */
  @Post("embed")
  async embed(@Body() body: EmbedRequestDto): Promise<EmbedResponseDto> {
    return this.ollamaService.embed(body.text, body.model);
  }

  /**
   * List available models
   * GET /ollama/models
   */
  @Get("models")
  async listModels(): Promise<ListModelsResponseDto> {
    return this.ollamaService.listModels();
  }

  /**
   * Health check endpoint
   * GET /ollama/health
   */
  @Get("health")
  async healthCheck(): Promise<HealthCheckResponseDto> {
    return this.ollamaService.healthCheck();
  }
}
