import {
  Controller,
  Get,
  Post,
  Patch,
  Param,
  Body,
  UseGuards,
  ParseUUIDPipe,
} 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 { LostReasonsService } from '../services/lost-reasons.service';
import { CreateLostReasonDto } from '../dto/create-lost-reason.dto';
import { UpdateLostReasonDto } from '../dto/update-lost-reason.dto';

@ApiTags('CRM Lost Reasons')
@ApiBearerAuth()
@Controller('crm/lost-reasons')
@UseGuards(JwtAuthGuard)
export class LostReasonsController {
  constructor(private readonly lostReasonsService: LostReasonsService) {}

  @Get()
  @ApiOperation({ summary: 'List all lost reasons' })
  @ApiResponse({ status: 200 })
  async findAll(@CurrentUser() user: any) {
    const clubId = user.role === UserRole.SUPER_ADMIN ? null : user.clubId;
    return this.lostReasonsService.findAll(clubId);
  }

  @Post()
  @UseGuards(RolesGuard)
  @Roles(UserRole.SUPER_ADMIN, UserRole.CLUB_ADMIN)
  @ApiOperation({ summary: 'Create a lost reason' })
  @ApiResponse({ status: 201 })
  async create(@CurrentUser() user: any, @Body() dto: CreateLostReasonDto) {
    const clubId = user.role === UserRole.SUPER_ADMIN ? null : user.clubId;
    return this.lostReasonsService.create(clubId, dto);
  }

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