import { Body, Controller, Get, Put } from '@nestjs/common';
import { ApiBearerAuth, ApiOperation, ApiTags } from '@nestjs/swagger';
import { IsBoolean, IsEnum, IsInt, IsOptional, Max, Min } from 'class-validator';
import { NotificationCategory } from '@prisma/client';
import { NotificationPreferencesService } from './notification-preferences.service';
import { CurrentUser, AuthenticatedUser } from '../../common/decorators/current-user.decorator';

class UpdatePreferenceDto {
  @IsEnum(NotificationCategory)
  category!: NotificationCategory;

  @IsOptional() @IsBoolean() push?: boolean;
  @IsOptional() @IsBoolean() email?: boolean;
  @IsOptional() @IsBoolean() sms?: boolean;
  @IsOptional() @IsBoolean() whatsapp?: boolean;

  @IsOptional() @IsInt() @Min(0) @Max(1439) quietHoursStart?: number | null;
  @IsOptional() @IsInt() @Min(0) @Max(1439) quietHoursEnd?: number | null;
}

@ApiTags('notification-preferences')
@ApiBearerAuth()
@Controller('notification-preferences')
export class NotificationPreferencesController {
  constructor(private readonly service: NotificationPreferencesService) {}

  @Get('me')
  @ApiOperation({ summary: 'Préférences de notifications de l’utilisateur courant (4 catégories)' })
  list(@CurrentUser() user: AuthenticatedUser) {
    return this.service.listForUser(user.userId);
  }

  @Put('me')
  @ApiOperation({ summary: 'Met à jour la préférence d’une catégorie' })
  upsert(@CurrentUser() user: AuthenticatedUser, @Body() dto: UpdatePreferenceDto) {
    return this.service.upsert(user.userId, dto);
  }
}
