import {
  Controller,
  Get,
  Post,
  Patch,
  Delete,
  Body,
  Param,
  ParseUUIDPipe,
  HttpCode,
  HttpStatus,
  UseGuards,
} from '@nestjs/common';
import {
  ApiTags,
  ApiOperation,
  ApiBearerAuth,
  ApiParam,
} from '@nestjs/swagger';
import { PipelinesService } from './pipelines.service';
import { CreatePipelineDto, CreatePipelineStageDto } from './dto/create-pipeline.dto';
import { UpdatePipelineDto } from './dto/update-pipeline.dto';
import { UpdateStageDto } from './dto/update-stage.dto';
import { CurrentUser, JwtPayload } from '../common/decorators';
import { TenantGuard } from '../auth/guards/tenant.guard';
import { singleResponse } from '../common/dto/pagination.dto';

@ApiTags('Pipelines')
@ApiBearerAuth('access-token')
@UseGuards(TenantGuard)
@Controller('pipelines')
export class PipelinesController {
  constructor(private readonly pipelinesService: PipelinesService) {}

  @Post()
  @HttpCode(HttpStatus.CREATED)
  @ApiOperation({ summary: 'Pipeline erstellen' })
  async create(
    @CurrentUser() user: JwtPayload,
    @Body() dto: CreatePipelineDto,
  ) {
    const pipeline = await this.pipelinesService.create(
      user.tenantId!,
      user.sub,
      dto,
    );
    return singleResponse(pipeline);
  }

  @Get()
  @ApiOperation({ summary: 'Alle Pipelines auflisten' })
  async findAll(@CurrentUser() user: JwtPayload) {
    const pipelines = await this.pipelinesService.findAll(user.tenantId!);
    return {
      success: true,
      data: pipelines,
      meta: { timestamp: new Date().toISOString() },
    };
  }

  @Get(':id')
  @ApiOperation({ summary: 'Pipeline-Details abrufen' })
  @ApiParam({ name: 'id', type: 'string', format: 'uuid' })
  async findOne(
    @CurrentUser() user: JwtPayload,
    @Param('id', ParseUUIDPipe) id: string,
  ) {
    const pipeline = await this.pipelinesService.findOne(user.tenantId!, id);
    return singleResponse(pipeline);
  }

  @Patch(':id')
  @ApiOperation({ summary: 'Pipeline aktualisieren' })
  @ApiParam({ name: 'id', type: 'string', format: 'uuid' })
  async update(
    @CurrentUser() user: JwtPayload,
    @Param('id', ParseUUIDPipe) id: string,
    @Body() dto: UpdatePipelineDto,
  ) {
    const pipeline = await this.pipelinesService.update(
      user.tenantId!,
      id,
      user.sub,
      dto,
    );
    return singleResponse(pipeline);
  }

  @Delete(':id')
  @ApiOperation({ summary: 'Pipeline loeschen' })
  @ApiParam({ name: 'id', type: 'string', format: 'uuid' })
  async remove(
    @CurrentUser() user: JwtPayload,
    @Param('id', ParseUUIDPipe) id: string,
  ) {
    const pipeline = await this.pipelinesService.remove(user.tenantId!, id);
    return singleResponse(pipeline);
  }

  // Stage Management
  @Post(':id/stages')
  @HttpCode(HttpStatus.CREATED)
  @ApiOperation({ summary: 'Stufe zur Pipeline hinzufuegen' })
  @ApiParam({ name: 'id', type: 'string', format: 'uuid' })
  async addStage(
    @CurrentUser() user: JwtPayload,
    @Param('id', ParseUUIDPipe) pipelineId: string,
    @Body() dto: CreatePipelineStageDto,
  ) {
    const stage = await this.pipelinesService.addStage(
      user.tenantId!,
      pipelineId,
      dto.name,
      dto.sortOrder ?? 0,
      dto.color,
      dto.probability,
    );
    return singleResponse(stage);
  }

  @Patch(':id/stages/:stageId')
  @ApiOperation({ summary: 'Stufe aktualisieren (Name, Farbe, Reihenfolge)' })
  @ApiParam({ name: 'id', type: 'string', format: 'uuid' })
  @ApiParam({ name: 'stageId', type: 'string', format: 'uuid' })
  async updateStage(
    @CurrentUser() user: JwtPayload,
    @Param('id', ParseUUIDPipe) pipelineId: string,
    @Param('stageId', ParseUUIDPipe) stageId: string,
    @Body() dto: UpdateStageDto,
  ) {
    const stage = await this.pipelinesService.updateStage(
      user.tenantId!,
      pipelineId,
      stageId,
      dto,
    );
    return singleResponse(stage);
  }

  @Delete(':id/stages/:stageId')
  @ApiOperation({ summary: 'Stufe aus Pipeline entfernen' })
  @ApiParam({ name: 'id', type: 'string', format: 'uuid' })
  @ApiParam({ name: 'stageId', type: 'string', format: 'uuid' })
  async removeStage(
    @CurrentUser() user: JwtPayload,
    @Param('id', ParseUUIDPipe) pipelineId: string,
    @Param('stageId', ParseUUIDPipe) stageId: string,
  ) {
    const stage = await this.pipelinesService.removeStage(
      user.tenantId!,
      pipelineId,
      stageId,
    );
    return singleResponse(stage);
  }
}
