import {
  Controller,
  Get,
  Post,
  Patch,
  Delete,
  Param,
  Body,
  UseGuards,
  HttpStatus,
  ParseUUIDPipe,
} from '@nestjs/common';
import { ApiTags, ApiOperation, ApiBearerAuth } from '@nestjs/swagger';
import { ProfilesService } from './profiles.service';
import { CreateProfileDto } from './dto/create-profile.dto';
import { UpdateProfileDto } from './dto/update-profile.dto';
import { JwtAuthGuard } from '../common/guards/jwt-auth.guard';
import { CurrentUser } from '../common/decorators/current-user.decorator';

@ApiTags('Profiles')
@Controller('profiles')
@UseGuards(JwtAuthGuard)
@ApiBearerAuth()
export class ProfilesController {
  constructor(private readonly profilesService: ProfilesService) {}

  @Get()
  @ApiOperation({ summary: 'Get user profiles' })
  async findAll(@CurrentUser('id') userId: string) {
    const profiles = await this.profilesService.findAllByUser(userId);
    return {
      data: profiles,
      message: 'Profiles retrieved successfully',
      statusCode: HttpStatus.OK,
    };
  }

  @Get(':id')
  @ApiOperation({ summary: 'Get profile by ID' })
  async findOne(
    @CurrentUser('id') userId: string,
    @Param('id', ParseUUIDPipe) id: string,
  ) {
    const profile = await this.profilesService.findById(id, userId);
    return {
      data: profile,
      message: 'Profile retrieved successfully',
      statusCode: HttpStatus.OK,
    };
  }

  @Post()
  @ApiOperation({ summary: 'Create profile' })
  async create(
    @CurrentUser('id') userId: string,
    @Body() createProfileDto: CreateProfileDto,
  ) {
    const profile = await this.profilesService.create(userId, createProfileDto);
    return {
      data: profile,
      message: 'Profile created successfully',
      statusCode: HttpStatus.CREATED,
    };
  }

  @Patch(':id')
  @ApiOperation({ summary: 'Update profile' })
  async update(
    @CurrentUser('id') userId: string,
    @Param('id', ParseUUIDPipe) id: string,
    @Body() updateProfileDto: UpdateProfileDto,
  ) {
    const profile = await this.profilesService.update(id, userId, updateProfileDto);
    return {
      data: profile,
      message: 'Profile updated successfully',
      statusCode: HttpStatus.OK,
    };
  }

  @Delete(':id')
  @ApiOperation({ summary: 'Delete profile' })
  async remove(
    @CurrentUser('id') userId: string,
    @Param('id', ParseUUIDPipe) id: string,
  ) {
    await this.profilesService.remove(id, userId);
    return {
      data: null,
      message: 'Profile deleted successfully',
      statusCode: HttpStatus.OK,
    };
  }
}
