import { Controller, Get, Post, Body, Patch, Param, Delete, UploadedFiles, UseInterceptors } from '@nestjs/common';
import { IconService } from '../providers/icon.service';
import { GroupService } from '../providers/group.service';
import { CreateIconDto } from '../dto/create-icon.dto';
import { UpdateIconDto } from '../dto/update-icon.dto';
import { CreateGroupDto } from '../dto/create-group.dto';
import { UpdateGroupDto } from '../dto/update-group.dto';
import { ApiTags, ApiOperation, ApiParam, ApiBody, ApiBearerAuth, ApiConsumes } from '@nestjs/swagger';
import { SuperAdminAuthGuard } from 'src/modules/auth/guards/superAdminAuth.guard';
import { UseGuards } from '@nestjs/common';
import { AdminAuthGuard } from 'src/modules/auth/guards/adminAuth.guard';
import { FilesInterceptor, type File } from '@nest-lab/fastify-multer';
import { CreateBatchIconsDto } from '../dto/create-batch-icons.dto';

@ApiTags('icons')
@Controller()
export class IconsController {
  constructor(
    private readonly iconService: IconService,
    private readonly groupService: GroupService,
  ) {}

  @Get('admin/groups/icons')
  @UseGuards(AdminAuthGuard)
  @ApiBearerAuth()
  @ApiOperation({ summary: 'Get all icons' })
  findAllPublicIcons() {
    return this.groupService.findAllGroups();
  }

  /**
   * Super Admin Endpoints
   */
  @Post('super-admin/icons')
  @UseGuards(SuperAdminAuthGuard)
  @ApiOperation({ summary: 'Create icon' })
  @ApiBody({ type: CreateIconDto })
  createIcon(@Body() dto: CreateIconDto) {
    return this.iconService.create(dto);
  }


  @Post('super-admin/icons/batch')
  @UseGuards(SuperAdminAuthGuard)
  @ApiOperation({ summary: 'Upload and create icons in batch' })
  @ApiConsumes('multipart/form-data')
  @UseInterceptors(FilesInterceptor('files'))
  @ApiBody({ type: CreateBatchIconsDto })
  createBatchIcons(
    @Body('groupId') groupId: string,
    @UploadedFiles() files: File[],
  ) {
    return this.iconService.createBatch(groupId, files);
  }


  @Get('super-admin/icons')
  @UseGuards(SuperAdminAuthGuard)
  @ApiOperation({ summary: 'Get all icons' })
  findAllIcons() {
    return this.iconService.findAll();
  }

  @Get('super-admin/icons/:id')
  @UseGuards(SuperAdminAuthGuard)
  @ApiOperation({ summary: 'Get icon by id' })
  @ApiParam({ name: 'id', required: true, type: String })
  findOneIcon(@Param('id') id: string) {
    return this.iconService.findOne(id);
  }

  @Patch('super-admin/icons/:id')
  @UseGuards(SuperAdminAuthGuard)
  @ApiOperation({ summary: 'Update icon' })
  @ApiParam({ name: 'id' })
  @ApiBody({ type: UpdateIconDto })
  updateIcon(@Param('id') id: string, @Body() dto: UpdateIconDto) {
    return this.iconService.update(id, dto);
  }

  @Delete('super-admin/icons/:id')
  @UseGuards(SuperAdminAuthGuard)
  @ApiOperation({ summary: 'Delete icon' })
  @ApiParam({ name: 'id' })
  removeIcon(@Param('id') id: string) {
    return this.iconService.remove(id);
  }

  // Group endpoints (must come before :id routes to avoid route conflicts)
  @Post('super-admin/icons/groups')
  @UseGuards(SuperAdminAuthGuard)
  @ApiOperation({ summary: 'Create icon group' })
  @ApiBody({ type: CreateGroupDto })
  createGroup(@Body() dto: CreateGroupDto) {
    return this.groupService.create(dto);
  }

  @Get('super-admin/icons/groups')
  @UseGuards(SuperAdminAuthGuard)
  @ApiOperation({ summary: 'Get all icon groups' })
  async findAllGroups() {
    console.log('find icona');
    const a = await this.groupService.findAllGroups();
    console.log(a);
    return a;
  }

  @Get('super-admin/icons/groups/:id')
  @UseGuards(SuperAdminAuthGuard)
  @ApiOperation({ summary: 'Get icon group by id' })
  @ApiParam({ name: 'id', required: true, type: String })
  findOneGroup(@Param('id') id: string) {
    return this.groupService.findOne(id);
  }

  @Patch('super-admin/icons/groups/:id')
  @UseGuards(SuperAdminAuthGuard)
  @ApiOperation({ summary: 'Update icon group' })
  @ApiParam({ name: 'id' })
  updateGroup(@Param('id') id: string, @Body() dto: UpdateGroupDto) {
    return this.groupService.update(id, dto);
  }

  @Delete('super-admin/icons/groups/:id')
  @UseGuards(SuperAdminAuthGuard)
  @ApiOperation({ summary: 'Delete icon group' })
  @ApiParam({ name: 'id' })
  removeGroup(@Param('id') id: string) {
    return this.groupService.remove(id);
  }
}
