import { Controller, Get, Post, Put, Delete, Body, Param, Req, UseGuards, HttpException, HttpStatus } from '@nestjs/common';
import { SeekerExperienceService } from './seeker_experience.service';
import { Public } from '../auth/public.decorator';
import { ApiBearerAuth, ApiTags, ApiOperation } from '@nestjs/swagger';
import { Request } from 'express';
import { JwtAuthGuard } from '../auth/jwt-auth.guard';

@ApiTags('seekerExperience')
@ApiBearerAuth('access-token')
@UseGuards(JwtAuthGuard)
@Controller('seekerExperience')
export class SeekerExperienceController {
  constructor(
    private readonly seekerExperienceService: SeekerExperienceService,
  ) {}

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

  @Public()
  @Get(':id')
  @ApiOperation({ summary: 'Get a specific seeker experience by ID' })
  async findOne(@Param('id') id: string) {
    return this.seekerExperienceService.findOne(Number(id));
  }

  @Post()
  @ApiOperation({ summary: 'Add a new experience' })
  async create(@Body() data: any, @Req() req: Request) {
    const user = (req as any).user;
    if (!user) throw new HttpException('Unauthorized: No user found', HttpStatus.UNAUTHORIZED);
    
    const userId = user.userId || user.id || user.sub;
    if (!userId) throw new HttpException('Unauthorized: No user ID found', HttpStatus.UNAUTHORIZED);
    
    return await this.seekerExperienceService.create(userId, data);
  }

  @Put(':id')
  @ApiOperation({ summary: 'Update an existing experience' })
  async update(@Param('id') id: string, @Body() data: any, @Req() req: Request) {
    const user = (req as any).user;
    if (!user) throw new HttpException('Unauthorized: No user found', HttpStatus.UNAUTHORIZED);
    
    const userId = user.userId || user.id || user.sub;
    if (!userId) throw new HttpException('Unauthorized: No user ID found', HttpStatus.UNAUTHORIZED);

    return await this.seekerExperienceService.update(userId, Number(id), data);
  }

  @Delete(':id')
  @ApiOperation({ summary: 'Delete an experience' })
  async remove(@Param('id') id: string, @Req() req: Request) {
    const user = (req as any).user;
    if (!user) throw new HttpException('Unauthorized: No user found', HttpStatus.UNAUTHORIZED);
    
    const userId = user.userId || user.id || user.sub;
    if (!userId) throw new HttpException('Unauthorized: No user ID found', HttpStatus.UNAUTHORIZED);
    
    return await this.seekerExperienceService.remove(userId, Number(id));
  }
}
