import {
  Controller,
  Get,
  Post,
  Patch,
  Put,
  Delete,
  Param,
  Body,
  Query,
  UseGuards,
  ParseUUIDPipe,
  HttpCode,
  HttpStatus,
} from '@nestjs/common';
import { ApiTags, ApiBearerAuth, ApiOperation, ApiResponse } from '@nestjs/swagger';
import { JwtAuthGuard } from '../../auth/guards/jwt-auth.guard';
import { RolesGuard } from '../../auth/guards/roles.guard';
import { Roles, UserRole } from '../../../common/decorators/roles.decorator';
import { CurrentUser } from '../../../common/decorators/current-user.decorator';
import { PipelinesService } from '../services/pipelines.service';
import { CreatePipelineDto } from '../dto/create-pipeline.dto';
import { UpdatePipelineDto } from '../dto/update-pipeline.dto';
import { CreateStageDto } from '../dto/create-stage.dto';
import { UpdateStageDto } from '../dto/update-stage.dto';
import { ReorderStagesDto } from '../dto/reorder-stages.dto';
import { DeleteStageQueryDto } from '../dto/delete-stage.dto';

@ApiTags('CRM Pipelines')
@ApiBearerAuth()
@Controller('crm/pipelines')
@UseGuards(JwtAuthGuard)
export class PipelinesController {
  constructor(private readonly pipelinesService: PipelinesService) {}

  @Get()
  @ApiOperation({ summary: 'List all pipelines' })
  @ApiResponse({ status: 200, description: 'List of pipelines with stages' })
  async findAll(@CurrentUser() user: any, @Query('entityType') entityType?: string) {
    const clubId = user.role === UserRole.SUPER_ADMIN ? null : user.clubId;
    return this.pipelinesService.findAll(clubId, entityType);
  }

  @Post()
  @UseGuards(RolesGuard)
  @Roles(UserRole.SUPER_ADMIN, UserRole.CLUB_ADMIN)
  @ApiOperation({ summary: 'Create a pipeline' })
  @ApiResponse({ status: 201, description: 'Pipeline created' })
  async create(@CurrentUser() user: any, @Body() dto: CreatePipelineDto) {
    const clubId = user.role === UserRole.SUPER_ADMIN ? null : user.clubId;
    return this.pipelinesService.create(clubId, dto);
  }

  @Patch(':id')
  @UseGuards(RolesGuard)
  @Roles(UserRole.SUPER_ADMIN, UserRole.CLUB_ADMIN)
  @ApiOperation({ summary: 'Update a pipeline' })
  async update(@Param('id', ParseUUIDPipe) id: string, @Body() dto: UpdatePipelineDto) {
    return this.pipelinesService.update(id, dto);
  }

  @Get(':id/stages')
  @ApiOperation({ summary: 'List stages of a pipeline' })
  async getStages(@Param('id', ParseUUIDPipe) id: string) {
    return this.pipelinesService.getStages(id);
  }

  @Post(':id/stages')
  @UseGuards(RolesGuard)
  @Roles(UserRole.SUPER_ADMIN, UserRole.CLUB_ADMIN)
  @ApiOperation({ summary: 'Create a stage in pipeline' })
  @ApiResponse({ status: 201, description: 'Stage created' })
  async createStage(@Param('id', ParseUUIDPipe) id: string, @Body() dto: CreateStageDto) {
    return this.pipelinesService.createStage(id, dto);
  }

  @Patch('stages/:id')
  @UseGuards(RolesGuard)
  @Roles(UserRole.SUPER_ADMIN, UserRole.CLUB_ADMIN)
  @ApiOperation({ summary: 'Update a stage' })
  async updateStage(@Param('id', ParseUUIDPipe) id: string, @Body() dto: UpdateStageDto) {
    return this.pipelinesService.updateStage(id, dto);
  }

  @Put('stages/reorder')
  @UseGuards(RolesGuard)
  @Roles(UserRole.SUPER_ADMIN, UserRole.CLUB_ADMIN)
  @ApiOperation({ summary: 'Reorder stages' })
  async reorderStages(@Body() dto: ReorderStagesDto) {
    return this.pipelinesService.reorderStages(dto);
  }

  @Delete('stages/:id')
  @UseGuards(RolesGuard)
  @Roles(UserRole.SUPER_ADMIN, UserRole.CLUB_ADMIN)
  @HttpCode(HttpStatus.NO_CONTENT)
  @ApiOperation({ summary: 'Delete a stage (optionally move deals to another stage)' })
  async deleteStage(@Param('id', ParseUUIDPipe) id: string, @Query() query: DeleteStageQueryDto) {
    await this.pipelinesService.deleteStage(id, query.moveToStageId);
  }

  @Delete(':id')
  @UseGuards(RolesGuard)
  @Roles(UserRole.SUPER_ADMIN, UserRole.CLUB_ADMIN)
  @HttpCode(HttpStatus.NO_CONTENT)
  @ApiOperation({ summary: 'Delete a pipeline (only if no deals)' })
  async deletePipeline(@Param('id', ParseUUIDPipe) id: string) {
    await this.pipelinesService.deletePipeline(id);
  }
}
