<?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\Offer\OfferInterface;
use Ferienpass\CoreBundle\Entity\OfferLog;
use Ferienpass\CoreBundle\Entity\User;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Workflow\Transition;

final class OfferLogTest extends TestCase
{
    public function testGetIdIsNullByDefault(): void
    {
        $offer = $this->createStub(OfferInterface::class);
        $user = new User();
        $log = new OfferLog($offer, $user);

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

    public function testToStringReturnsEmptyStringWhenNoId(): void
    {
        $offer = $this->createStub(OfferInterface::class);
        $user = new User();
        $log = new OfferLog($offer, $user);

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

    public function testGetOfferReturnsOffer(): void
    {
        $offer = $this->createStub(OfferInterface::class);
        $user = new User();
        $log = new OfferLog($offer, $user);

        self::assertSame($offer, $log->getOffer());
    }

    public function testGetCreatedByReturnsUser(): void
    {
        $offer = $this->createStub(OfferInterface::class);
        $user = new User();
        $log = new OfferLog($offer, $user);

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

    public function testIsCommentReturnsFalseByDefault(): void
    {
        $offer = $this->createStub(OfferInterface::class);
        $log = new OfferLog($offer, new User());

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

    public function testIsCommentReturnsTrueWhenCommentSet(): void
    {
        $offer = $this->createStub(OfferInterface::class);
        $log = new OfferLog($offer, new User(), comment: 'Test comment');

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

    public function testIsTransitionReturnsFalseByDefault(): void
    {
        $offer = $this->createStub(OfferInterface::class);
        $log = new OfferLog($offer, new User());

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

    public function testIsTransitionReturnsTrueWhenTransitionSet(): void
    {
        $offer = $this->createStub(OfferInterface::class);
        $transition = new Transition('confirm', ['waiting'], ['confirmed']);
        $log = new OfferLog($offer, new User(), transition: $transition);

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

    public function testGetTransitionReturnsTransition(): void
    {
        $offer = $this->createStub(OfferInterface::class);
        $transition = new Transition('approve', ['pending'], ['approved']);
        $log = new OfferLog($offer, new User(), transition: $transition);

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

    public function testGetCreatedAtIsSetOnConstruct(): void
    {
        $offer = $this->createStub(OfferInterface::class);
        $log = new OfferLog($offer, new User());

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