import { Injectable, NotFoundException } from '@nestjs/common';
import { PrismaService } from '../prisma/prisma.service';
import { UpdateSettingsDto } from './dto/update-settings.dto';

@Injectable()
export class UsersService {
  constructor(private readonly prisma: PrismaService) {}

  async me(userId: string) {
    const user = await this.prisma.user.findUnique({ where: { id: userId } });
    if (!user) throw new NotFoundException();
    return {
      id: user.id,
      displayName: user.displayName,
      parentEmail: user.parentEmail,
      age: user.age,
      language: user.language,
      theme: user.theme,
      notifyCertificateEmail: user.notifyCertificateEmail,
      createdAt: user.createdAt,
    };
  }

  async updateSettings(userId: string, dto: UpdateSettingsDto) {
    const user = await this.prisma.user.update({
      where: { id: userId },
      data: {
        notifyCertificateEmail: dto.notifyCertificate,
        language: dto.language,
        theme: dto.theme,
      },
    });
    return this.me(user.id);
  }
}
