<?php

declare(strict_types=1);

namespace App\Framework\Database\Example;

use App\Framework\Database\Attributes\Column;
use App\Framework\Database\Attributes\Entity;
use App\Framework\Database\IdGenerator;

/**
 * Beispiel-Entity mit auto-generierter ID
 */
#[Entity('users')]
final readonly class User
{
    #[Column(name: 'id', primary: true)]
    public string $id;

    #[Column(name: 'name')]
    public string $name;

    #[Column(name: 'email')]
    public ?string $email;

    #[Column(name: 'failed_attempts')]
    public int $failed_attempts;

    #[Column(name: 'password_hash')]
    public ?string $password_hash;

    public function __construct(
        string $name,
        ?string $email = null,
        ?string $id = null,
        int $failed_attempts = 0,
        ?string $password_hash = null
    ) {
        $this->id = $id ?? IdGenerator::generate();
        $this->name = $name;
        $this->email = $email;
        $this->failed_attempts = $failed_attempts;
        $this->password_hash = $password_hash;
    }

    /**
     * Erstellt eine neue Instanz mit geändertem Namen
     */
    public function withName(string $name): self
    {
        return new self($name, $this->email, $this->id, $this->failed_attempts, $this->password_hash);
    }

    /**
     * Erstellt eine neue Instanz mit geänderter Email
     */
    public function withEmail(?string $email): self
    {
        return new self($this->name, $email, $this->id, $this->failed_attempts, $this->password_hash);
    }
}
