<?php

namespace App\Auth\Entity;

use App\Auth\Repository\PreRegistrationRepository;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Uid\Uuid;

#[ORM\Entity(repositoryClass: PreRegistrationRepository::class)]
#[ORM\Table(name: 'pre_registrations')]
#[ORM\Index(columns: ['mobile'], name: 'idx_prereg_mobile')]
#[ORM\Index(columns: ['status'], name: 'idx_prereg_status')]
class PreRegistration
{
    public const TYPE_INDEPENDENT_DOCTOR = 'independent_doctor';
    public const TYPE_DOCTOR_WITH_CLINIC = 'doctor_with_clinic';
    public const TYPE_CLINIC_MANAGER     = 'clinic_manager';

    public const STATUS_PENDING  = 'pending';
    public const STATUS_APPROVED = 'approved';
    public const STATUS_REJECTED = 'rejected';

    #[ORM\Id]
    #[ORM\GeneratedValue]
    #[ORM\Column(type: 'integer')]
    private ?int $id = null;

    #[ORM\Column(type: 'string', length: 36, unique: true)]
    private string $uuid;

    #[ORM\Column(type: 'string', length: 30)]
    private string $type;

    #[ORM\Column(type: 'string', length: 255)]
    private string $name;

    #[ORM\Column(type: 'string', length: 20)]
    private string $mobile;

    #[ORM\Column(type: 'text', nullable: true)]
    private ?string $info = null;

    #[ORM\Column(type: 'string', length: 20)]
    private string $status = self::STATUS_PENDING;

    #[ORM\Column(name: 'admin_note', type: 'text', nullable: true)]
    private ?string $adminNote = null;

    #[ORM\Column(name: 'created_at', type: 'integer')]
    private int $createdAt;

    #[ORM\Column(name: 'updated_at', type: 'integer')]
    private int $updatedAt;

    public function __construct(string $type, string $name, string $mobile, ?string $info = null)
    {
        $this->uuid      = Uuid::v4()->toRfc4122();
        $this->type      = $type;
        $this->name      = $name;
        $this->mobile    = $mobile;
        $this->info      = $info;
        $this->createdAt = time();
        $this->updatedAt = time();
    }

    public function getId(): ?int        { return $this->id; }
    public function getUuid(): string    { return $this->uuid; }
    public function getType(): string    { return $this->type; }
    public function getName(): string    { return $this->name; }
    public function getMobile(): string  { return $this->mobile; }
    public function getInfo(): ?string   { return $this->info; }
    public function getStatus(): string  { return $this->status; }
    public function getAdminNote(): ?string { return $this->adminNote; }
    public function getCreatedAt(): int  { return $this->createdAt; }
    public function getUpdatedAt(): int  { return $this->updatedAt; }

    public function approve(): void
    {
        $this->status    = self::STATUS_APPROVED;
        $this->updatedAt = time();
    }

    public function reject(?string $note = null): void
    {
        $this->status    = self::STATUS_REJECTED;
        $this->adminNote = $note;
        $this->updatedAt = time();
    }

    public function isPending(): bool { return $this->status === self::STATUS_PENDING; }
}
