import { Controller, Get, Post, Put, Delete, Body, Param, UseGuards, Req, HttpException, HttpStatus } from '@nestjs/common';
import { SeekerCertificationsService } from './seeker_certifications.service';
import { JwtAuthGuard } from '../auth/jwt-auth.guard';
import { ApiBearerAuth, ApiTags, ApiOperation } from '@nestjs/swagger';
import { CreateSeekerCertificationDto } from './dto/create-seeker_certification.dto';
import { UpdateSeekerCertificationDto } from './dto/update-seeker_certification.dto';

@ApiTags('seekerCertifications')
@ApiBearerAuth('access-token')
@UseGuards(JwtAuthGuard)
@Controller('seekerCertifications')
export class SeekerCertificationsController {
  constructor(private readonly seekerCertificationsService: SeekerCertificationsService) {}

  @Get()
  @ApiOperation({ summary: 'Get all certifications for the authenticated seeker' })
  async findAll(@Req() req) {
    const userId = req.user?.id || req.user?.userId || req.user?.sub;
    if (!userId) throw new HttpException('Unauthorized: No user ID found', HttpStatus.UNAUTHORIZED);
    return this.seekerCertificationsService.findAllBySeekerId(userId);
  }

  @Post()
  @ApiOperation({ summary: 'Add a new certification for the authenticated seeker' })
  async create(@Req() req, @Body() createDto: CreateSeekerCertificationDto) {
    const userId = req.user?.id || req.user?.userId || req.user?.sub;
    if (!userId) throw new HttpException('Unauthorized: No user ID found', HttpStatus.UNAUTHORIZED);
    return this.seekerCertificationsService.create(userId, createDto);
  }

  @Put(':id')
  @ApiOperation({ summary: 'Update a seeker certification by ID' })
  async update(@Req() req, @Param('id') id: string, @Body() updateDto: UpdateSeekerCertificationDto) {
    const userId = req.user?.id || req.user?.userId || req.user?.sub;
    if (!userId) throw new HttpException('Unauthorized: No user ID found', HttpStatus.UNAUTHORIZED);
    return this.seekerCertificationsService.update(userId, +id, updateDto);
  }

  @Delete(':id')
  @ApiOperation({ summary: 'Delete a seeker certification by ID' })
  async remove(@Req() req, @Param('id') id: string) {
    const userId = req.user?.id || req.user?.userId || req.user?.sub;
    if (!userId) throw new HttpException('Unauthorized: No user ID found', HttpStatus.UNAUTHORIZED);
    return this.seekerCertificationsService.remove(userId, +id);
  }
}
