<?php

namespace App\UserProfile\Entity;

use App\Auth\Entity\User;
use Doctrine\ORM\Mapping as ORM;
use App\UserProfile\Repository\UserProfileRepository;
use Symfony\Component\Uid\Uuid;

#[ORM\Entity(repositoryClass: UserProfileRepository::class)]
#[ORM\Table(name: 'profiles')]
#[ORM\UniqueConstraint(name: 'idx_profiles_user', columns: ['user_id'])]
#[ORM\UniqueConstraint(name: 'uniq_profiles_national_code', columns: ['national_code'])]
class UserProfile
{
    #[ORM\Id]
    #[ORM\GeneratedValue]
    #[ORM\Column(type: 'integer')]
    private ?int $id = null;

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

    #[ORM\OneToOne(targetEntity: User::class)]
    #[ORM\JoinColumn(name: 'user_id', referencedColumnName: 'id', nullable: false, onDelete: 'CASCADE')]
    private User $user;

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

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

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

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

    #[ORM\Column(name: 'national_code_approved', type: 'boolean')]
    private bool $nationalCodeApproved = false;

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    #[ORM\Column(name: 'sharing_with_user', type: 'boolean')]
    private bool $sharingWithUser = false;

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

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

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

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

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

    public function getId(): ?int      { return $this->id; }
    public function getUuid(): string  { return $this->uuid; }
    public function getUser(): User    { return $this->user; }

    public function getLabel(): ?string  { return $this->label; }
    public function getFamily(): ?string { return $this->family; }
    public function getFathersName(): ?string { return $this->fathersName; }
    public function getNationalCode(): ?string { return $this->nationalCode; }
    public function isNationalCodeApproved(): bool { return $this->nationalCodeApproved; }
    public function getGender(): ?string { return $this->gender; }
    public function getDateOfBirth(): ?int { return $this->dateOfBirth; }
    public function getBloodType(): ?string { return $this->bloodType; }
    public function getMaritalStatus(): ?string { return $this->maritalStatus; }
    public function getEducation(): ?string { return $this->education; }
    public function getFieldOfStudy(): ?string { return $this->fieldOfStudy; }
    public function getJob(): ?string { return $this->job; }
    public function getAddress(): ?string { return $this->address; }
    public function getProvinceId(): ?int { return $this->provinceId; }
    public function getCityId(): ?int { return $this->cityId; }
    public function getPostalCode(): ?string { return $this->postalCode; }
    public function getReferralSource(): ?string { return $this->referralSource; }
    public function getHomePhone(): ?string { return $this->homePhone; }
    public function getWorkPhone(): ?string { return $this->workPhone; }
    public function getInsuranceId(): ?string { return $this->insuranceId; }
    public function getBasicInsuranceId(): ?int { return $this->basicInsuranceId; }
    public function getSupplementaryInsuranceId(): ?int { return $this->supplementaryInsuranceId; }
    public function getOther(): ?array { return $this->other; }
    public function isSharingWithUser(): bool { return $this->sharingWithUser; }
    public function getDescription(): ?string { return $this->description; }
    public function getAvatar(): ?string { return $this->avatar; }
    public function getCreatedAt(): int { return $this->createdAt; }
    public function getUpdatedAt(): int { return $this->updatedAt; }

    public function setLabel(?string $v): self  { $this->label = $v; return $this; }
    public function setFamily(?string $v): self { $this->family = $v; $this->touch(); return $this; }
    public function setFathersName(?string $v): self { $this->fathersName = $v; $this->touch(); return $this; }
    public function setNationalCode(?string $v): self { $this->nationalCode = $v; $this->touch(); return $this; }
    public function setNationalCodeApproved(bool $v): self { $this->nationalCodeApproved = $v; $this->touch(); return $this; }
    public function setGender(?string $v): self { $this->gender = $v; $this->touch(); return $this; }
    public function setDateOfBirth(?int $v): self { $this->dateOfBirth = $v; $this->touch(); return $this; }
    public function setBloodType(?string $v): self { $this->bloodType = $v; $this->touch(); return $this; }
    public function setMaritalStatus(?string $v): self { $this->maritalStatus = $v; $this->touch(); return $this; }
    public function setEducation(?string $v): self { $this->education = $v; $this->touch(); return $this; }
    public function setFieldOfStudy(?string $v): self { $this->fieldOfStudy = $v; $this->touch(); return $this; }
    public function setJob(?string $v): self { $this->job = $v; $this->touch(); return $this; }
    public function setAddress(?string $v): self { $this->address = $v; $this->touch(); return $this; }
    public function setProvinceId(?int $v): self { $this->provinceId = $v; $this->touch(); return $this; }
    public function setCityId(?int $v): self { $this->cityId = $v; $this->touch(); return $this; }
    public function setPostalCode(?string $v): self { $this->postalCode = $v; $this->touch(); return $this; }
    public function setReferralSource(?string $v): self { $this->referralSource = $v; $this->touch(); return $this; }
    public function setHomePhone(?string $v): self { $this->homePhone = $v; $this->touch(); return $this; }
    public function setWorkPhone(?string $v): self { $this->workPhone = $v; $this->touch(); return $this; }
    public function setInsuranceId(?string $v): self { $this->insuranceId = $v; $this->touch(); return $this; }
    public function setBasicInsuranceId(?int $v): self { $this->basicInsuranceId = $v; $this->touch(); return $this; }
    public function setSupplementaryInsuranceId(?int $v): self { $this->supplementaryInsuranceId = $v; $this->touch(); return $this; }
    public function setOther(?array $v): self { $this->other = $v; $this->touch(); return $this; }
    public function setSharingWithUser(bool $v): self { $this->sharingWithUser = $v; $this->touch(); return $this; }
    public function setDescription(?string $v): self { $this->description = $v; $this->touch(); return $this; }
    public function setAvatar(?string $v): self { $this->avatar = $v; $this->touch(); return $this; }

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

    public function toArray(): array
    {
        return [
            'id'                          => $this->id,
            'uuid'                        => $this->uuid,
            'user_uuid'                   => $this->user->getUuid(),
            'label'                       => $this->label,
            'family'                      => $this->family,
            'fathers_name'                => $this->fathersName,
            'national_code'               => $this->nationalCode,
            'national_code_approved'      => $this->nationalCodeApproved,
            'gender'                      => $this->gender,
            'birthday'                    => $this->dateOfBirth,
            'blood_type'                  => $this->bloodType,
            'marital_status'              => $this->maritalStatus,
            'education'                   => $this->education,
            'field_of_study'              => $this->fieldOfStudy,
            'job'                         => $this->job,
            'address'                     => $this->address,
            'province_id'                 => $this->provinceId,
            'city_id'                     => $this->cityId,
            'postal_code'                 => $this->postalCode,
            'referral_source'             => $this->referralSource,
            'home_phone'                  => $this->homePhone,
            'work_phone'                  => $this->workPhone,
            'insurance_id'                => $this->insuranceId,
            'basic_insurance_id'          => $this->basicInsuranceId,
            'supplementary_insurance_id'  => $this->supplementaryInsuranceId,
            'other'                       => $this->other,
            'sharing_with_user'           => $this->sharingWithUser,
            'description'                 => $this->description,
            'avatar'                      => $this->avatar,
            'created_at'                  => $this->createdAt,
            'updated_at'                  => $this->updatedAt,
        ];
    }
}
