import {
  Controller,
  Get,
  Post,
  Body,
  Param,
  UseGuards,
} from '@nestjs/common';
import { ApiTags, ApiBearerAuth, ApiOperation } from '@nestjs/swagger';
import { JwtAuthGuard } from '../auth/guards/jwt-auth.guard';
import { TranslationsService } from './translations.service';
import { TranslationType } from '../../generated/prisma/client.js';

class TranslateRequestDto {
  type: TranslationType;
  englishName: string;
  englishDescription?: string;
}

class TranslateBatchRequestDto {
  type: TranslationType;
  items: Array<{ englishName: string; englishDescription?: string }>;
}

@ApiTags('translations')
@ApiBearerAuth()
@UseGuards(JwtAuthGuard)
@Controller('translations')
export class TranslationsController {
  constructor(private translationsService: TranslationsService) {}

  @Post()
  @ApiOperation({ summary: 'Translate a single item' })
  async translate(@Body() dto: TranslateRequestDto) {
    return this.translationsService.getTranslation(
      dto.type,
      dto.englishName,
      dto.englishDescription,
    );
  }

  @Post('batch')
  @ApiOperation({ summary: 'Translate multiple items' })
  async translateBatch(@Body() dto: TranslateBatchRequestDto) {
    const result = await this.translationsService.getTranslationsBatch(
      dto.type,
      dto.items,
    );
    return Object.fromEntries(result);
  }

  @Get(':type')
  @ApiOperation({ summary: 'Get all cached translations of a type' })
  async getAllByType(@Param('type') type: TranslationType) {
    return this.translationsService.getAllByType(type);
  }
}
