<?php

declare(strict_types=1);

namespace App\Controller;

use App\Entity\User;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Attribute\Route;
use Symfony\Component\Security\Http\Attribute\IsGranted;

#[Route('/api/user')]
#[IsGranted('ROLE_USER')]
class UserApiController extends AbstractController
{
    private const VALID_THEMES = [
        'warm-craft',
        'playful-pop',
        'sage-cream',
        'dusty-mauve',
        'ocean-dusk',
        'honey-slate',
    ];

    #[Route('/search', name: 'api_users_search', methods: ['GET'])]
    public function search(Request $request, EntityManagerInterface $em): JsonResponse
    {
        $q = trim((string) $request->query->get('q', ''));
        if (strlen($q) < 2) {
            return $this->json([]);
        }

        /** @var User $me */
        $me = $this->getUser();

        $results = $em->createQueryBuilder()
            ->select('u')
            ->from(User::class, 'u')
            ->where('u.email LIKE :q')
            ->andWhere('u.id != :me')
            ->setParameter('q', '%' . $q . '%')
            ->setParameter('me', $me->getId())
            ->setMaxResults(10)
            ->getQuery()
            ->getResult();

        return $this->json(array_map(
            static fn(User $u) => ['id' => $u->getId(), 'email' => $u->getEmail()],
            $results,
        ));
    }

    #[Route('/theme', name: 'api_user_theme', methods: ['PATCH'])]
    public function updateTheme(Request $request, EntityManagerInterface $em): JsonResponse
    {
        $body = json_decode($request->getContent(), true);
        $theme = $body['theme'] ?? null;

        if (!is_string($theme) || !in_array($theme, self::VALID_THEMES, true)) {
            return $this->json(['error' => 'Invalid theme'], Response::HTTP_UNPROCESSABLE_ENTITY);
        }

        /** @var User $user */
        $user = $this->getUser();
        $user->setTheme($theme);
        $em->flush();

        return $this->json(['theme' => $theme]);
    }

    #[Route('/timezone', name: 'api_user_timezone', methods: ['PATCH'])]
    public function updateTimezone(Request $request, EntityManagerInterface $em): JsonResponse
    {
        $body = json_decode($request->getContent(), true);
        $tz   = $body['timezone'] ?? null;

        if (!is_string($tz)) {
            return $this->json(['error' => 'Missing timezone'], Response::HTTP_UNPROCESSABLE_ENTITY);
        }

        try {
            new \DateTimeZone($tz);
        } catch (\Exception) {
            return $this->json(['error' => 'Invalid timezone identifier'], Response::HTTP_UNPROCESSABLE_ENTITY);
        }

        /** @var User $user */
        $user = $this->getUser();
        $user->setTimezone($tz);
        $em->flush();

        return $this->json(['timezone' => $tz]);
    }
}
