import { UseGuards } from '@nestjs/common';
import { JwtAuthGuard } from '@/modules/auth/jwt-auth.guard';
import { LogActivity } from '@/common/decorators/activity-log.decorator';
import {
  Body,
  Controller,
  Delete,
  Get,
  Param,
  ParseArrayPipe,
  ParseIntPipe,
  Patch,
  Post,
  Query,
} from '@nestjs/common';

import { CreateTranslationDto } from './dto/create-translation.dto';
import {
  TranslationDictionaryQueryDto,
  TranslationKeyQueryDto,
  TranslationQueryDto,
} from './dto/translation-query.dto';
import { UpdateTranslationDto } from './dto/update-translation.dto';
import { TranslationsService } from './translations.service';

@Controller('translations')
export class TranslationsController {
  constructor(private readonly service: TranslationsService) {}

  // ============================================================
  // BULK CREATE
  // POST /api/v1/translations/bulk
  // Body: CreateTranslationDto[]
  // ============================================================

  @Post('bulk')
  @LogActivity({
    summaryOnly: true,
    module: 'translations',
    action: 'CREATE',
    reference: 'translations/bulk',
    countField: 'createdCount',
  })
  @UseGuards(JwtAuthGuard)
  createBulk(
    @Body(
      new ParseArrayPipe({
        items: CreateTranslationDto,
      }),
    )
    dtos: CreateTranslationDto[],
  ) {
    return this.service.createBulk(dtos);
  }

  // ============================================================
  // CREATE ONE
  // POST /api/v1/translations
  // ============================================================

  @Post()
  @LogActivity({
    snapshot: { source: 'translation' },
    module: 'translations',
    action: 'CREATE',
    reference: 'translations',
  })
  @UseGuards(JwtAuthGuard)
  create(
    @Body()
    dto: CreateTranslationDto,
  ) {
    return this.service.create(dto);
  }

  // ============================================================
  // GET ALL
  // ============================================================

  @Get()
  findAll(
    @Query()
    query: TranslationQueryDto,
  ) {
    return this.service.findAll(query);
  }

  // IMPORTANT:
  // Static GET routes must be declared before @Get(':id').

  // ============================================================
  // GET PAGES BY MODULE
  // GET /api/v1/translations/pages?module=pswg
  // ============================================================

  @Get('pages')
  getPages(
    @Query('module')
    module: string,
  ) {
    return this.service.getPages(module);
  }

  // ============================================================
  // GET MODULES
  // ============================================================

  @Get('modules')
  getModules() {
    return this.service.getModules();
  }

  // ============================================================
  // GET DICTIONARY
  // GET /api/v1/translations/dictionary?language=en&module=pswg&page=meeting_requests
  // ============================================================

  @Get('dictionary')
  getDictionary(
    @Query()
    query: TranslationDictionaryQueryDto,
  ) {
    return this.service.getDictionary(query);
  }

  // ============================================================
  // ENGLISH DICTIONARY
  // ============================================================

  @Get('dictionary/english')
  getEnglishDictionary(
    @Query('module')
    module?: string,

    @Query('page')
    page?: string,
  ) {
    return this.service.getEnglishDictionary(module, page);
  }

  // ============================================================
  // KHMER DICTIONARY
  // ============================================================

  @Get('dictionary/khmer')
  getKhmerDictionary(
    @Query('module')
    module?: string,

    @Query('page')
    page?: string,
  ) {
    return this.service.getKhmerDictionary(module, page);
  }

  // ============================================================
  // GET BY KEY
  // ============================================================

  @Get('key/:translationKey')
  findByKey(
    @Param('translationKey')
    translationKey: string,

    @Query()
    query: TranslationKeyQueryDto,
  ) {
    return this.service.findByKey(translationKey, query);
  }

  // ============================================================
  // ENABLE
  // ============================================================

  @Patch(':id/enable')
  @LogActivity({
    snapshot: { source: 'translation', idParam: 'id' },
    module: 'translations',
    action: 'UPDATE',
    reference: 'translations/:id/enable',
  })
  @UseGuards(JwtAuthGuard)
  enable(
    @Param('id', ParseIntPipe)
    id: number,
  ) {
    return this.service.enable(id);
  }

  // ============================================================
  // GET ONE
  // ============================================================

  @Get(':id')
  findOne(
    @Param('id', ParseIntPipe)
    id: number,
  ) {
    return this.service.findOne(id);
  }

  // ============================================================
  // UPDATE
  // ============================================================

  @Patch(':id')
  @LogActivity({
    snapshot: { source: 'translation', idParam: 'id' },
    module: 'translations',
    action: 'UPDATE',
    reference: 'translations/:id',
  })
  @UseGuards(JwtAuthGuard)
  update(
    @Param('id', ParseIntPipe)
    id: number,

    @Body()
    dto: UpdateTranslationDto,
  ) {
    return this.service.update(id, dto);
  }

  // ============================================================
  // DELETE
  // ============================================================

  @Delete(':id')
  @LogActivity({
    snapshot: { source: 'translation', idParam: 'id' },
    module: 'translations',
    action: 'DELETE',
    reference: 'translations/:id',
  })
  @UseGuards(JwtAuthGuard)
  remove(
    @Param('id', ParseIntPipe)
    id: number,
  ) {
    return this.service.remove(id);
  }
}
