<?php

declare(strict_types=1);

/*
 * This file is part of the Ferienpass package.
 *
 * (c) Richard Henkenjohann <richard@ferienpass.online>
 *
 * For more information visit the project website <https://ferienpass.online>
 * or the documentation under <https://docs.ferienpass.online>.
 */

namespace Ferienpass\CoreBundle\Dto;

use Ferienpass\CoreBundle\Entity\Participant\ParticipantInterface;

class ParticipantDuplicateDto
{
    public $participant1;
    public $participant2;

    public function __construct(
        $p1Id,
        $p2Id,
        public string $statusGroup,
        ?string $p1Firstname = null,
        ?string $p1Lastname = null,
        ?string $p1Email = null,
        $p1Uuid = null,
        $p1Dob = null,
        ?string $p2Firstname = null,
        ?string $p2Lastname = null,
        ?string $p2Email = null,
        $p2Uuid = null,
        $p2Dob = null,
        ?string $p1UserFirstname = null,
        ?string $p1UserLastname = null,
        $p1UserId = null,
        $p1AttendanceCount = 0,
        public $p1UserAttendanceCount = 0,
        ?string $p2UserFirstname = null,
        ?string $p2UserLastname = null,
        $p2UserId = null,
        $p2AttendanceCount = 0,
        public $p2UserAttendanceCount = 0
    ) {
        $this->participant1 = new class($p1Id, $p1Firstname, $p1Lastname, $p1Email, $p1Uuid, $p1Dob, $p1UserFirstname, $p1UserLastname, $p1UserId, $p1AttendanceCount, $p1UserAttendanceCount) {
            public function __construct(
                public $id,
                public $firstname,
                public $lastname,
                public $email,
                public $uuid,
                public $dateOfBirth,
                public $userFirstname,
                public $userLastname,
                public $userId,
                public $attendanceCount,
                public $userAttendanceCount
            ) {
            }

            public function getName(): string
            {
                return trim($this->firstname.' '.$this->lastname).' #'.$this->id;
            }

            public function getUserName(): ?string
            {
                if (!$this->userId) {
                    return null;
                }

                return trim($this->userFirstname.' '.$this->userLastname).' #'.$this->userId;
            }

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

        $this->participant2 = new class($p2Id, $p2Firstname, $p2Lastname, $p2Email, $p2Uuid, $p2Dob, $p2UserFirstname, $p2UserLastname, $p2UserId, $p2AttendanceCount, $p2UserAttendanceCount) {
            public function __construct(
                public $id,
                public $firstname,
                public $lastname,
                public $email,
                public $uuid,
                public $dateOfBirth,
                public $userFirstname,
                public $userLastname,
                public $userId,
                public $attendanceCount,
                public $userAttendanceCount
            ) {
            }

            public function getName(): string
            {
                return trim($this->firstname.' '.$this->lastname).' #'.$this->id;
            }

            public function getUserName(): ?string
            {
                if (!$this->userId) {
                    return null;
                }

                return trim($this->userFirstname.' '.$this->userLastname).' #'.$this->userId;
            }

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

    public function getId(): string
    {
        $id1 = $this->participant1 instanceof ParticipantInterface ? $this->participant1->getId() : $this->participant1->id;
        $id2 = $this->participant2 instanceof ParticipantInterface ? $this->participant2->getId() : $this->participant2->id;

        return $id1.'-'.$id2;
    }

    public function hasDobMatch(): bool
    {
        $dob1 = $this->participant1->dateOfBirth instanceof \DateTimeInterface ? $this->participant1->dateOfBirth->format('Y-m-d') : null;
        $dob2 = $this->participant2->dateOfBirth instanceof \DateTimeInterface ? $this->participant2->dateOfBirth->format('Y-m-d') : null;

        return $dob1 === $dob2;
    }

    public function hasUserMatch(): bool
    {
        return $this->participant1->userId && $this->participant1->userId === $this->participant2->userId;
    }
}
