import {
  Controller,
  Get,
  Patch,
  Post,
  Delete,
  Body,
  UseGuards,
  UseInterceptors,
  UploadedFile,
  ParseFilePipe,
  MaxFileSizeValidator,
  FileTypeValidator,
} from '@nestjs/common';
import { FileInterceptor } from '@nestjs/platform-express';
import { SettingsService } from './settings.service';
import { UpdatePdfSettingsDto } from './dto/update-pdf-settings.dto';
import { JwtAuthGuard } from '../auth/guards/jwt-auth.guard';
import { AbilitiesGuard } from '../auth/guards/abilities.guard';
import { CanUpdate } from '../auth/decorators/check-ability.decorator';

@Controller('settings')
@UseGuards(JwtAuthGuard, AbilitiesGuard)
export class SettingsController {
  constructor(private readonly settingsService: SettingsService) {}

  @Get('pdf')
  @CanUpdate('Settings') // Admin-only (Settings subject is admin-only)
  getPdfSettings() {
    return this.settingsService.getPdfSettings();
  }

  @Patch('pdf')
  @CanUpdate('Settings')
  updatePdfSettings(@Body() dto: UpdatePdfSettingsDto) {
    return this.settingsService.updatePdfSettings(dto);
  }

  @Post('pdf/logo')
  @CanUpdate('Settings')
  @UseInterceptors(FileInterceptor('logo'))
  uploadLogo(
    @UploadedFile(
      new ParseFilePipe({
        validators: [
          new MaxFileSizeValidator({ maxSize: 2 * 1024 * 1024 }), // 2MB
          new FileTypeValidator({ fileType: /(png|jpeg|jpg|svg\+xml)/ }),
        ],
      }),
    )
    file: Express.Multer.File,
  ) {
    return this.settingsService.uploadLogo(file);
  }

  @Delete('pdf/logo')
  @CanUpdate('Settings')
  deleteLogo() {
    return this.settingsService.deleteLogo();
  }
}
