import { Controller, Get, Post, Put, Delete, Body, Param, UseGuards, Req, HttpException, HttpStatus } from '@nestjs/common';
import { SeekerEducationService } from './seeker_education.service';
import { Public } from '../auth/public.decorator';
import { JwtAuthGuard } from '../auth/jwt-auth.guard';
import { ApiBearerAuth, ApiTags, ApiOperation } from '@nestjs/swagger';
import { CreateSeekerEducationDto } from './dto/create-seeker_education.dto';
import { UpdateSeekerEducationDto } from './dto/update-seeker_education.dto';

@ApiTags('seekerEducation')
@ApiBearerAuth('access-token')
@UseGuards(JwtAuthGuard)
@Controller('seekerEducation')
export class SeekerEducationController {
  constructor(
    private readonly seekerEducationService: SeekerEducationService,
  ) {}

  @Public()
  @Get()
  @ApiOperation({ summary: 'Get all seeker educations' })
  async findAll() {
    return this.seekerEducationService.findAll();
  }

  @Post()
  @ApiOperation({ summary: 'Add a new education for the authenticated seeker' })
  async create(@Req() req, @Body() createDto: CreateSeekerEducationDto) {
    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.seekerEducationService.create(userId, createDto);
  }

  @Put(':id')
  @ApiOperation({ summary: 'Update a seeker education by ID' })
  async update(@Req() req, @Param('id') id: string, @Body() updateDto: UpdateSeekerEducationDto) {
    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.seekerEducationService.update(userId, +id, updateDto);
  }

  @Delete(':id')
  @ApiOperation({ summary: 'Delete a seeker education 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.seekerEducationService.remove(userId, +id);
  }
}
