import {
  Controller,
  Get,
  Post,
  Put,
  Delete,
  Body,
  Param,
  Query,
  UseGuards,
  ParseUUIDPipe,
  HttpCode,
  HttpStatus,
} from '@nestjs/common';
import {
  ApiTags,
  ApiOperation,
  ApiResponse,
  ApiBearerAuth,
  ApiQuery,
} from '@nestjs/swagger';
import { FolderService } from '../services/folder.service';
import { CreateFolderDto, UpdateFolderDto } from '../dto';
import { JwtAuthGuard } from '@/common/guards/jwt-auth.guard';
import { TenantMemberGuard } from '@/common/guards/tenant-member.guard';
import { CurrentTenant } from '@/common/decorators/current-tenant.decorator';

@ApiTags('Assets - Folders')
@Controller('assets/folders')
@UseGuards(JwtAuthGuard, TenantMemberGuard)
@ApiBearerAuth('JWT-auth')
export class FolderController {
  constructor(private readonly folderService: FolderService) {}

  @Get()
  @ApiOperation({ summary: 'Get all folders' })
  @ApiQuery({ name: 'brandId', required: false })
  @ApiResponse({ status: 200, description: 'List of folders' })
  async findAll(
    @CurrentTenant() tenantId: string,
    @Query('brandId') brandId?: string,
  ) {
    return this.folderService.findAll(tenantId, brandId);
  }

  @Get('tree')
  @ApiOperation({ summary: 'Get folder tree structure' })
  @ApiQuery({ name: 'brandId', required: false })
  @ApiResponse({ status: 200, description: 'Folder tree' })
  async getTree(
    @CurrentTenant() tenantId: string,
    @Query('brandId') brandId?: string,
  ) {
    return this.folderService.getTree(tenantId, brandId);
  }

  @Get('root')
  @ApiOperation({ summary: 'Get root folders' })
  @ApiQuery({ name: 'brandId', required: false })
  @ApiResponse({ status: 200, description: 'List of root folders' })
  async findRootFolders(
    @CurrentTenant() tenantId: string,
    @Query('brandId') brandId?: string,
  ) {
    return this.folderService.findRootFolders(tenantId, brandId);
  }

  @Get(':id')
  @ApiOperation({ summary: 'Get folder by ID' })
  @ApiResponse({ status: 200, description: 'Folder found' })
  @ApiResponse({ status: 404, description: 'Folder not found' })
  async findOne(
    @CurrentTenant() tenantId: string,
    @Param('id', ParseUUIDPipe) id: string,
  ) {
    return this.folderService.findById(tenantId, id);
  }

  @Get('slug/:slug')
  @ApiOperation({ summary: 'Get folder by slug' })
  @ApiResponse({ status: 200, description: 'Folder found' })
  @ApiResponse({ status: 404, description: 'Folder not found' })
  async findBySlug(
    @CurrentTenant() tenantId: string,
    @Param('slug') slug: string,
  ) {
    return this.folderService.findBySlug(tenantId, slug);
  }

  @Post()
  @ApiOperation({ summary: 'Create new folder' })
  @ApiResponse({ status: 201, description: 'Folder created' })
  @ApiResponse({ status: 400, description: 'Bad request' })
  async create(
    @CurrentTenant() tenantId: string,
    @Body() dto: CreateFolderDto,
  ) {
    return this.folderService.create(tenantId, dto);
  }

  @Put(':id')
  @ApiOperation({ summary: 'Update folder' })
  @ApiResponse({ status: 200, description: 'Folder updated' })
  @ApiResponse({ status: 404, description: 'Folder not found' })
  async update(
    @CurrentTenant() tenantId: string,
    @Param('id', ParseUUIDPipe) id: string,
    @Body() dto: UpdateFolderDto,
  ) {
    return this.folderService.update(tenantId, id, dto);
  }

  @Delete(':id')
  @HttpCode(HttpStatus.NO_CONTENT)
  @ApiOperation({ summary: 'Delete folder (soft delete)' })
  @ApiResponse({ status: 204, description: 'Folder deleted' })
  @ApiResponse({ status: 400, description: 'Cannot delete folder with subfolders' })
  @ApiResponse({ status: 404, description: 'Folder not found' })
  async delete(
    @CurrentTenant() tenantId: string,
    @Param('id', ParseUUIDPipe) id: string,
  ) {
    await this.folderService.softDelete(tenantId, id);
  }
}
