import {
  Controller,
  Get,
  Post,
  Patch,
  Delete,
  Body,
  Param,
  Query,
  UseGuards,
  ParseUUIDPipe,
  HttpCode,
  HttpStatus,
} from '@nestjs/common';
import { ApiTags, ApiOperation, ApiBearerAuth } from '@nestjs/swagger';
import { UsersService } from './users.service';
import { CreateUserDto } from './dto/create-user.dto';
import { UpdateUserDto } from './dto/update-user.dto';
import { ChangePasswordDto } from './dto/change-password.dto';
import { Roles } from '../../common/decorators/roles.decorator';
import { CurrentUser, JwtPayload } from '../../common/decorators/current-user.decorator';
import { RolesGuard } from '../../common/guards/roles.guard';

@ApiTags('Benutzer')
@ApiBearerAuth('access-token')
@Controller('users')
export class UsersController {
  constructor(private readonly usersService: UsersService) {}

  /**
   * GET /api/v1/users/me
   * Eigenes Profil abrufen.
   */
  @Get('me')
  @ApiOperation({ summary: 'Eigenes Profil abrufen' })
  async getProfile(@CurrentUser('sub') userId: string) {
    return this.usersService.findById(userId);
  }

  /**
   * PATCH /api/v1/users/me
   * Eigenes Profil aktualisieren (firstName, lastName).
   */
  @Patch('me')
  @ApiOperation({ summary: 'Eigenes Profil aktualisieren' })
  async updateProfile(
    @CurrentUser('sub') userId: string,
    @Body() dto: UpdateUserDto,
  ) {
    return this.usersService.updateProfile(userId, dto);
  }

  /**
   * POST /api/v1/users/me/change-password
   * Eigenes Passwort ändern.
   */
  @Post('me/change-password')
  @HttpCode(HttpStatus.OK)
  @ApiOperation({ summary: 'Eigenes Passwort ändern' })
  async changePassword(
    @CurrentUser('sub') userId: string,
    @Body() dto: ChangePasswordDto,
  ) {
    await this.usersService.changePassword(
      userId,
      dto.currentPassword,
      dto.newPassword,
    );
    return { message: 'Passwort erfolgreich geändert' };
  }

  /**
   * GET /api/v1/users
   * Alle User auflisten (nur PLATFORM_ADMIN).
   */
  @Get()
  @Roles('PLATFORM_ADMIN')
  @UseGuards(RolesGuard)
  @ApiOperation({ summary: 'Alle Benutzer auflisten (Admin)' })
  async findAll(
    @Query('page') page?: string,
    @Query('limit') limit?: string,
  ) {
    return this.usersService.findAll(
      Number(page) || 1,
      Number(limit) || 20,
    );
  }

  /**
   * GET /api/v1/users/:id
   * User nach ID (nur PLATFORM_ADMIN).
   */
  @Get(':id')
  @Roles('PLATFORM_ADMIN')
  @UseGuards(RolesGuard)
  @ApiOperation({ summary: 'Benutzer nach ID abrufen (Admin)' })
  async findById(@Param('id', ParseUUIDPipe) id: string) {
    return this.usersService.findById(id);
  }

  /**
   * POST /api/v1/users
   * Neuen User anlegen (nur PLATFORM_ADMIN).
   */
  @Post()
  @Roles('PLATFORM_ADMIN')
  @UseGuards(RolesGuard)
  @ApiOperation({ summary: 'Neuen Benutzer anlegen (Admin)' })
  async create(@Body() dto: CreateUserDto) {
    return this.usersService.create(dto);
  }

  /**
   * PATCH /api/v1/users/:id
   * User aktualisieren (nur PLATFORM_ADMIN).
   */
  @Patch(':id')
  @Roles('PLATFORM_ADMIN')
  @UseGuards(RolesGuard)
  @ApiOperation({ summary: 'Benutzer aktualisieren (Admin)' })
  async update(
    @Param('id', ParseUUIDPipe) id: string,
    @Body() dto: UpdateUserDto,
  ) {
    return this.usersService.update(id, dto);
  }

  /**
   * DELETE /api/v1/users/:id
   * User löschen (nur PLATFORM_ADMIN).
   */
  @Delete(':id')
  @Roles('PLATFORM_ADMIN')
  @UseGuards(RolesGuard)
  @ApiOperation({ summary: 'Benutzer löschen (Admin)' })
  async delete(@Param('id', ParseUUIDPipe) id: string) {
    return this.usersService.delete(id);
  }
}
