<?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\Debtor;
use Ferienpass\CoreBundle\Entity\Edition;
use Ferienpass\CoreBundle\Entity\Offer\Base;
use Ferienpass\CoreBundle\Entity\Participant\BaseParticipant;
use Ferienpass\CoreBundle\Entity\User;
use PHPUnit\Framework\TestCase;

final class DebtorTest extends TestCase
{
    public function testConstructWithUser(): void
    {
        $user = new User();
        $debtor = new Debtor($user);

        self::assertSame($user, $debtor->getUser());
    }

    public function testConstructWithoutUser(): void
    {
        $debtor = new Debtor();

        self::assertNull($debtor->getUser());
    }

    public function testUuidIsGeneratedOnConstruct(): void
    {
        $d1 = new Debtor();
        $d2 = new Debtor();

        self::assertNotSame((string) $d1->getUuid(), (string) $d2->getUuid());
    }

    public function testToStringReturnsUuid(): void
    {
        $debtor = new Debtor();

        self::assertSame((string) $debtor->getUuid(), (string) $debtor);
    }

    public function testIdIsNullByDefault(): void
    {
        $debtor = new Debtor();

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

    public function testPaymentsInitiallyEmpty(): void
    {
        $debtor = new Debtor();

        self::assertCount(0, $debtor->getPayments());
    }

    public function testBookEntriesInitiallyEmpty(): void
    {
        $debtor = new Debtor();

        self::assertCount(0, $debtor->getBookEntries());
    }

    public function testGetBalanceIsZeroWhenNoBookEntries(): void
    {
        $debtor = new Debtor();

        self::assertSame(0, $debtor->getBalance());
    }

    public function testGetSummedPaymentsIsZeroWhenNoBookEntries(): void
    {
        $debtor = new Debtor();

        self::assertSame(0, $debtor->getSummedPayments());
    }

    public function testGetAttendancesDueReturnsEmptyWhenNoUser(): void
    {
        $debtor = new Debtor();

        self::assertSame([], $debtor->getAttendancesDue());
    }

    public function testGetDueSumIsZeroWhenNoAttendances(): void
    {
        $debtor = new Debtor();

        self::assertSame(0, $debtor->getDueSum());
    }

    public function testGetAttendancesDueReturnsEmptyWhenUserHasNoParticipants(): void
    {
        $user = new User();
        $debtor = new Debtor($user);

        self::assertSame([], $debtor->getAttendancesDue());
    }

    public function testGetAttendancesDueIncludesAPayableAttendance(): void
    {
        $debtor = new Debtor($this->userOwing(new Edition()));

        self::assertCount(1, $debtor->getAttendancesDue());
    }

    public function testGetAttendancesDueSkipsSeasonsThatDoNotCollectFees(): void
    {
        $edition = new Edition();
        $edition->setUsePayments(false);

        $debtor = new Debtor($this->userOwing($edition));

        self::assertSame([], $debtor->getAttendancesDue());
    }

    /**
     * A user with one participant holding one unpaid, payable attendance in the
     * given season.
     */
    private function userOwing(Edition $edition): User
    {
        $offer = new Base();
        $offer->setEdition($edition);

        $participant = new BaseParticipant();

        $attendance = new Attendance($offer, $participant);
        $attendance->setPayable(true);
        $participant->addAttendance($attendance);

        $user = new User();
        $user->addParticipant($participant);

        return $user;
    }
}
