<?php

declare(strict_types=1);

namespace App\Entity;

use App\Repository\UserRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use App\Entity\Image;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
use Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface;
use Symfony\Component\Security\Core\User\UserInterface;

#[ORM\Entity(repositoryClass: UserRepository::class)]
#[ORM\Table(name: '`user`')]
#[UniqueEntity(fields: ['email'], message: 'An account with this email already exists')]
class User implements UserInterface, PasswordAuthenticatedUserInterface
{
    #[ORM\Id]
    #[ORM\GeneratedValue]
    #[ORM\Column]
    private ?int $id = null;

    #[ORM\Column(length: 180, unique: true)]
    private string $email = '';

    /** @var list<string> */
    #[ORM\Column]
    private array $roles = [];

    #[ORM\Column]
    private string $password = '';

    #[ORM\Column(length: 50, nullable: true)]
    private ?string $theme = null;

    /**
     * Design version preference: 'v1' (cream, the original) or 'v2' (dusks,
     * the atmospheric redesign). Null means the user has never opted in,
     * which the app treats as v1. Cookie-mirrored on login.
     */
    #[ORM\Column(length: 10, nullable: true)]
    private ?string $designVersion = null;

    #[ORM\Column(length: 60, nullable: true)]
    private ?string $timezone = null;

    /** @var Collection<int, Device> */
    #[ORM\OneToMany(targetEntity: Device::class, mappedBy: 'user', orphanRemoval: true)]
    private Collection $devices;

    /** @var Collection<int, Image> */
    #[ORM\OneToMany(targetEntity: Image::class, mappedBy: 'user', orphanRemoval: true)]
    private Collection $images;

    public function getId(): ?int
    {
        return $this->id;
    }

    public function getEmail(): string
    {
        return $this->email;
    }

    public function setEmail(string $email): static
    {
        $this->email = $email;
        return $this;
    }

    public function getUserIdentifier(): string
    {
        return $this->email;
    }

    /** @return list<string> */
    public function getRoles(): array
    {
        $roles = $this->roles;
        $roles[] = 'ROLE_USER';
        return array_unique($roles);
    }

    /** @param list<string> $roles */
    public function setRoles(array $roles): static
    {
        $this->roles = $roles;
        return $this;
    }

    public function getPassword(): string
    {
        return $this->password;
    }

    public function setPassword(string $password): static
    {
        $this->password = $password;
        return $this;
    }

    public function getTheme(): ?string
    {
        return $this->theme;
    }

    public function setTheme(?string $theme): static
    {
        $this->theme = $theme;
        return $this;
    }

    /** Returns 'v1' or 'v2'; defaults to 'v1' when unset. */
    public function getDesignVersion(): string
    {
        return $this->designVersion ?? 'v1';
    }

    public function setDesignVersion(?string $version): static
    {
        $this->designVersion = $version;
        return $this;
    }

    public function getTimezone(): string
    {
        return $this->timezone ?? 'UTC';
    }

    public function setTimezone(?string $timezone): static
    {
        $this->timezone = $timezone;
        return $this;
    }

    public function eraseCredentials(): void {}

    /** @return Collection<int, Device> */
    public function getDevices(): Collection { return $this->devices; }

    /** @return Collection<int, Image> */
    public function getImages(): Collection { return $this->images; }

    public function __construct()
    {
        $this->devices = new ArrayCollection();
        $this->images  = new ArrayCollection();
    }
}
