<?php

namespace App\Doctor\Entity;

use App\Location\Entity\City;
use App\Location\Entity\Province;
use Doctrine\ORM\Mapping as ORM;
use App\Doctor\Repository\DoctorAddressRepository;
use Symfony\Component\Uid\Uuid;

#[ORM\Entity(repositoryClass: DoctorAddressRepository::class)]
#[ORM\Table(name: 'doctor_addresses')]
#[ORM\Index(columns: ['doctor_id'], name: 'idx_doctor_addresses_doctor')]
#[ORM\Index(columns: ['clinic_id'], name: 'idx_doctor_addr_clinic')]
class DoctorAddress
{
    public const TYPE_PERSONAL = 'personal';
    public const TYPE_CLINIC   = 'clinic';

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

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

    #[ORM\ManyToOne(targetEntity: Doctor::class, inversedBy: 'addresses')]
    #[ORM\JoinColumn(name: 'doctor_id', referencedColumnName: 'id', nullable: true, onDelete: 'CASCADE')]
    private ?Doctor $doctor = null;

    #[ORM\Column(name: 'clinic_id', type: 'integer', nullable: true)]
    private ?int $clinicId = null;

    #[ORM\Column(type: 'string', length: 10)]
    private string $type = self::TYPE_PERSONAL;

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

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

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

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

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

    #[ORM\ManyToOne(targetEntity: City::class)]
    #[ORM\JoinColumn(name: 'city_id', referencedColumnName: 'id', nullable: true, onDelete: 'SET NULL')]
    private ?City $city = null;

    #[ORM\ManyToOne(targetEntity: Province::class)]
    #[ORM\JoinColumn(name: 'province_id', referencedColumnName: 'id', nullable: true, onDelete: 'SET NULL')]
    private ?Province $province = null;

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

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

    private function __construct()
    {
        $this->uuid      = Uuid::v4()->toRfc4122();
        $this->createdAt = time();
        $this->updatedAt = time();
    }

    public static function forDoctor(Doctor $doctor): self
    {
        $a         = new self();
        $a->type   = self::TYPE_PERSONAL;
        $a->doctor = $doctor;
        return $a;
    }

    public static function forClinic(int $clinicId): self
    {
        $a           = new self();
        $a->type     = self::TYPE_CLINIC;
        $a->clinicId = $clinicId;
        return $a;
    }

    public function getId(): ?int           { return $this->id; }
    public function getUuid(): string       { return $this->uuid; }
    public function getDoctor(): ?Doctor    { return $this->doctor; }
    public function getClinicId(): ?int     { return $this->clinicId; }
    public function getType(): string       { return $this->type; }
    public function getName(): ?string      { return $this->name; }
    public function getAddress(): ?string   { return $this->address; }
    public function getTelephone(): ?string { return $this->telephone; }
    public function getLatitude(): ?float   { return $this->latitude; }
    public function getLongitude(): ?float  { return $this->longitude; }
    public function getCity(): ?City        { return $this->city; }
    public function getProvince(): ?Province { return $this->province; }

    public function setName(?string $v): self      { $this->name = $v; return $this; }
    public function setAddress(?string $v): self   { $this->address = $v; $this->touch(); return $this; }
    public function setTelephone(?string $v): self { $this->telephone = $v; $this->touch(); return $this; }
    public function setLatitude(?float $v): self   { $this->latitude = $v; $this->touch(); return $this; }
    public function setLongitude(?float $v): self  { $this->longitude = $v; $this->touch(); return $this; }
    public function setCity(?City $v): self        { $this->city = $v; $this->touch(); return $this; }
    public function setProvince(?Province $v): self { $this->province = $v; $this->touch(); return $this; }

    private function touch(): void { $this->updatedAt = time(); }

    public function toArray(?string $clinicName = null): array
    {
        return [
            'id'          => (string) $this->id,
            'uuid'        => $this->uuid,
            'type'        => $this->type,
            'clinic_id'   => $this->clinicId,
            'clinic_name' => $clinicName,
            'name'        => $this->name,
            'map'        => [
                'latitude'  => $this->latitude !== null ? (string) $this->latitude : null,
                'longitude' => $this->longitude !== null ? (string) $this->longitude : null,
            ],
            'address'    => $this->address,
            'telephone'  => $this->telephone,
            'city'       => $this->city !== null ? [
                'id'   => (string) $this->city->getId(),
                'name' => $this->city->getName(),
            ] : null,
            'province'   => $this->province !== null ? [
                'id'   => (string) $this->province->getId(),
                'name' => $this->province->getName(),
            ] : null,
        ];
    }
}
