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 { ProjectService } from '../services/project.service';
import { CreateProjectDto, UpdateProjectDto } from '../dto';
import { ProjectStatus } from '../entities/project.entity';
import { JwtAuthGuard } from '@/common/guards/jwt-auth.guard';
import { TenantMemberGuard } from '@/common/guards/tenant-member.guard';
import { CurrentTenant } from '@/common/decorators/current-tenant.decorator';
import { PaginationDto } from '@/shared/dto/pagination.dto';

@ApiTags('Projects')
@Controller('projects')
@UseGuards(JwtAuthGuard, TenantMemberGuard)
@ApiBearerAuth('JWT-auth')
export class ProjectController {
  constructor(private readonly projectService: ProjectService) {}

  @Get()
  @ApiOperation({ summary: 'Get all projects with pagination' })
  @ApiQuery({ name: 'brandId', required: false })
  @ApiQuery({ name: 'status', enum: ProjectStatus, required: false })
  @ApiQuery({ name: 'search', required: false })
  @ApiResponse({ status: 200, description: 'List of projects' })
  async findAll(
    @CurrentTenant() tenantId: string,
    @Query() pagination: PaginationDto,
    @Query('brandId') brandId?: string,
    @Query('status') status?: ProjectStatus,
    @Query('search') search?: string,
  ) {
    return this.projectService.findAllPaginated(tenantId, {
      ...pagination,
      brandId,
      status,
      search,
    });
  }

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

  @Get('brand/:brandId')
  @ApiOperation({ summary: 'Get projects by brand' })
  @ApiResponse({ status: 200, description: 'List of brand projects' })
  async findByBrand(
    @CurrentTenant() tenantId: string,
    @Param('brandId', ParseUUIDPipe) brandId: string,
  ) {
    return this.projectService.findByBrand(tenantId, brandId);
  }

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

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

  @Put(':id/status')
  @ApiOperation({ summary: 'Update project status' })
  @ApiResponse({ status: 200, description: 'Project status updated' })
  @ApiResponse({ status: 404, description: 'Project not found' })
  async updateStatus(
    @CurrentTenant() tenantId: string,
    @Param('id', ParseUUIDPipe) id: string,
    @Body('status') status: ProjectStatus,
  ) {
    return this.projectService.updateStatus(tenantId, id, status);
  }

  @Put(':id/progress')
  @ApiOperation({ summary: 'Update project progress' })
  @ApiResponse({ status: 200, description: 'Project progress updated' })
  @ApiResponse({ status: 404, description: 'Project not found' })
  async updateProgress(
    @CurrentTenant() tenantId: string,
    @Param('id', ParseUUIDPipe) id: string,
    @Body('progress') progress: number,
  ) {
    return this.projectService.updateProgress(tenantId, id, progress);
  }

  @Delete(':id')
  @HttpCode(HttpStatus.NO_CONTENT)
  @ApiOperation({ summary: 'Delete project (soft delete)' })
  @ApiResponse({ status: 204, description: 'Project deleted' })
  @ApiResponse({ status: 404, description: 'Project not found' })
  async delete(
    @CurrentTenant() tenantId: string,
    @Param('id', ParseUUIDPipe) id: string,
  ) {
    await this.projectService.softDelete(tenantId, id);
  }
}
