import {
    Body,
    Controller,
    Get,
    Param,
    Post,
    Put,
    Query,
    UseGuards,
    Req
} from '@nestjs/common';

import { EmailTemplateService } from './email-template.service';

import { ApiBearerAuth, ApiOperation, ApiTags } from '@nestjs/swagger';
import { AuthGuard } from 'src/auth/auth.guard';
import { CreateEmailTemplateDto, TestEmailDto, UpdateEmailTemplateDto } from './dto/emailtemplate.dto';

@ApiTags('Email Template')
@Controller('email-template')

export class EmailTemplateController {
    constructor(private readonly service: EmailTemplateService) { }

    @UseGuards(AuthGuard)
    @ApiBearerAuth('access_token')
    @ApiOperation({ summary: 'Create new email template' })
    @Post()
    create(@Body() dto: CreateEmailTemplateDto) {
        return this.service.create(dto);
    }



    @UseGuards(AuthGuard)
    @ApiBearerAuth('access_token')
    @ApiOperation({ summary: 'List all email templates (paginated)' })
    @Get()
    findAll(
        @Query('page') page: number,
        @Query('limit') limit: number,
    ) {
        return this.service.findAll(page, limit);
    }

    // ---------------- GET SINGLE TEMPLATE ----------------
    @UseGuards(AuthGuard)
    @ApiBearerAuth('access_token')
    @ApiOperation({ summary: 'Get an email template by name' })
    @Get(':name')
    findOne(@Param('name') name: string) {
        return this.service.findOne(name);
    }

    // ---------------- UPDATE TEMPLATE ----------------
    @UseGuards(AuthGuard)
    @ApiBearerAuth('access_token')
    @ApiOperation({ summary: 'Update email template by name' })
    @Put(':name')
    update(
        @Param('name') name: string,
        @Body() dto: UpdateEmailTemplateDto,
    ) {
        return this.service.update(name, dto);
    }

    @Post('test-email')
    testEmail(@Body() dto: TestEmailDto, @Req() req) {
        return this.service.testEmail(dto, req);
    }

    @ApiOperation({ summary: 'Sync email templates with JSON file' })
    @Post('sync-json')
    manualSyncEmailTemplates() {
        return this.service.manualSyncEmailTemplates();
    }
}
