import { Request, Response } from 'express';
import { AppError } from '../errors/app-error';
import { getProfil, identifierParCarte } from '../services/utilisateur.service';
import { toUtilisateurResponse } from '../dtos/utilisateur.dto';

export async function getMe(req: Request, res: Response): Promise<void> {
  const user = req.user;
  if (!user) {
    throw new AppError(401, 'Authentification requise');
  }

  const profil = await getProfil(user.id);
  res.json({ data: toUtilisateurResponse(profil) });
}

export async function identifier(req: Request, res: Response): Promise<void> {
  const qr = req.params.qr;
  if (typeof qr !== 'string') {
    throw new AppError(400, 'QR code invalide');
  }

  const profil = await identifierParCarte(qr);
  res.json({ data: toUtilisateurResponse(profil) });
}
