<?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\Tests\Entity;

use Ferienpass\CoreBundle\Entity\Attendance;
use Ferienpass\CoreBundle\Entity\Offer\Base;
use Ferienpass\CoreBundle\Entity\Participant\BaseParticipant;
use Ferienpass\CoreBundle\Entity\ParticipantLog;
use Ferienpass\CoreBundle\Entity\User;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Workflow\Transition;

final class ParticipantLogTest extends TestCase
{
    public function testGetIdIsNullByDefault(): void
    {
        $participant = new BaseParticipant();
        $log = new ParticipantLog($participant);

        self::assertNull($log->getId());
    }

    public function testToStringReturnsEmptyStringWhenNoId(): void
    {
        $participant = new BaseParticipant();
        $log = new ParticipantLog($participant);

        self::assertSame('', (string) $log);
    }

    public function testGetParticipantReturnsParticipant(): void
    {
        $participant = new BaseParticipant();
        $log = new ParticipantLog($participant);

        self::assertSame($participant, $log->getParticipant());
    }

    public function testGetAttendanceIsNullByDefault(): void
    {
        $participant = new BaseParticipant();
        $log = new ParticipantLog($participant);

        self::assertNull($log->getAttendance());
    }

    public function testGetCreatedByIsNullByDefault(): void
    {
        $participant = new BaseParticipant();
        $log = new ParticipantLog($participant);

        self::assertNull($log->getCreatedBy());
    }

    public function testGetCreatedByReturnsUser(): void
    {
        $participant = new BaseParticipant();
        $user = new User();
        $log = new ParticipantLog($participant, $user);

        self::assertSame($user, $log->getCreatedBy());
    }

    public function testIsCommentReturnsFalseByDefault(): void
    {
        $participant = new BaseParticipant();
        $log = new ParticipantLog($participant);

        self::assertFalse($log->isComment());
    }

    public function testIsCommentReturnsTrueWhenCommentSet(): void
    {
        $participant = new BaseParticipant();
        $log = new ParticipantLog($participant, comment: 'Test comment');

        self::assertTrue($log->isComment());
        self::assertSame('Test comment', $log->getComment());
    }

    public function testIsTransitionReturnsFalseByDefault(): void
    {
        $participant = new BaseParticipant();
        $log = new ParticipantLog($participant);

        self::assertFalse($log->isTransition());
        self::assertNull($log->getTransition());
    }

    public function testIsTransitionReturnsTrueWhenTransitionSet(): void
    {
        $participant = new BaseParticipant();
        $transition = new Transition('confirm', ['waiting'], ['confirmed']);
        $log = new ParticipantLog($participant, transition: $transition);

        self::assertTrue($log->isTransition());
        self::assertSame('confirm', $log->getTransitionName());
    }

    public function testGetTransitionReturnsTransition(): void
    {
        $participant = new BaseParticipant();
        $transition = new Transition('approve', ['pending'], ['approved']);
        $log = new ParticipantLog($participant, transition: $transition);

        $result = $log->getTransition();
        self::assertInstanceOf(Transition::class, $result);
        self::assertSame('approve', $result->getName());
    }

    public function testGetCreatedAtIsSetOnConstruct(): void
    {
        $participant = new BaseParticipant();
        $log = new ParticipantLog($participant);

        self::assertInstanceOf(\DateTimeInterface::class, $log->getCreatedAt());
    }

    public function testWithAttendanceForSameParticipant(): void
    {
        $participant = new BaseParticipant();
        $offer = new Base();
        $attendance = new Attendance($offer, $participant);

        $log = new ParticipantLog($participant, attendance: $attendance);

        self::assertSame($attendance, $log->getAttendance());
    }

    public function testConstructorThrowsWhenAttendanceBelongsToDifferentParticipant(): void
    {
        $participant = new BaseParticipant();
        $otherParticipant = new BaseParticipant();
        $attendance = new Attendance(new Base(), $otherParticipant);

        $this->expectException(\InvalidArgumentException::class);
        new ParticipantLog($participant, attendance: $attendance);
    }
}
