import { Controller, Get, Post, Body, UseGuards } from '@nestjs/common';
import { ApiTags, ApiOperation, ApiBearerAuth, ApiBody } from '@nestjs/swagger';
import { CloudflareService } from './cloudflare.service';

// Add Auth Guard if you have one, e.g. @UseGuards(JwtAuthGuard)
@ApiTags('Cloudflare')
@ApiBearerAuth()
@Controller('admin/cloudflare')
export class CloudflareController {
  constructor(private readonly cloudflareService: CloudflareService) {}

  @Get('zones')
  @ApiOperation({ summary: 'List Cloudflare Zones' })
  async listZones() {
    return this.cloudflareService.listZones();
  }

  @Post('cache/purge-all')
  @ApiOperation({ summary: 'Purge All Cache' })
  async purgeAll() {
    return this.cloudflareService.purgeCache({ purge_everything: true });
  }

  @Post('cache/purge-urls')
  @ApiOperation({ summary: 'Purge Cache by URLs' })
  @ApiBody({ schema: { example: { urls: ['https://example.com/image.png'] } } })
  async purgeUrls(@Body('urls') urls: string[]) {
    return this.cloudflareService.purgeCache({ files: urls });
  }

  @Post('cache/purge-tags')
  @ApiOperation({ summary: 'Purge Cache by Tags' })
  @ApiBody({ schema: { example: { tags: ['blog-posts'] } } })
  async purgeTags(@Body('tags') tags: string[]) {
    return this.cloudflareService.purgeCache({ tags });
  }

  @Post('cache/purge-hosts')
  @ApiOperation({ summary: 'Purge Cache by Hosts' })
  @ApiBody({ schema: { example: { hosts: ['api.example.com'] } } })
  async purgeHosts(@Body('hosts') hosts: string[]) {
    return this.cloudflareService.purgeCache({ hosts });
  }
}
