import {
  Body,
  Controller,
  Delete,
  Get,
  HttpCode,
  HttpStatus,
  Inject,
  Param,
  Post,
  UseGuards,
} from '@nestjs/common';
import { PreferencesService } from './preferences.service.js';
import { AuthGuard } from '../auth/auth.guard.js';
import { CurrentUser } from '../auth/current-user.decorator.js';

@Controller('api/preferences')
@UseGuards(AuthGuard)
export class PreferencesController {
  constructor(@Inject(PreferencesService) private readonly preferences: PreferencesService) {}

  @Get()
  async show(@CurrentUser() user: { id: string }): Promise<Record<string, unknown>> {
    return this.preferences.getEffective(user.id);
  }

  @Post()
  @HttpCode(HttpStatus.OK)
  async set(
    @CurrentUser() user: { id: string },
    @Body() body: { key: string; value: unknown },
  ): Promise<{ success: boolean; message: string }> {
    return this.preferences.set(user.id, body.key, body.value);
  }

  @Delete(':key')
  @HttpCode(HttpStatus.OK)
  async reset(
    @CurrentUser() user: { id: string },
    @Param('key') key: string,
  ): Promise<{ success: boolean; message: string }> {
    return this.preferences.reset(user.id, key);
  }
}
