<?php

declare(strict_types=1);

namespace Tests\Framework\DateTime;

use App\Framework\DateTime\FrozenClock;

class FrozenClockTest extends \PHPUnit\Framework\TestCase
{
    public function testNowReturnsFrozenTime(): void
    {
        $frozenTime = new \DateTimeImmutable('2021-01-01 00:00:00');
        $clock = new FrozenClock($frozenTime);

        $this->assertEquals($frozenTime, $clock->now());

        // Überprüfen, dass die Zeit eingefroren ist
        sleep(1);
        $this->assertEquals($frozenTime, $clock->now());
    }

    public function testFromTimestampReturnsCorrectDateTime(): void
    {
        $clock = new FrozenClock('2021-01-01 00:00:00');
        $timestamp = 1609459200; // 2021-01-01 00:00:00 UTC
        $date = $clock->fromTimestamp($timestamp);

        $this->assertInstanceOf(\DateTimeImmutable::class, $date);
        $this->assertEquals($timestamp, $date->getTimestamp());
    }

    public function testFromStringReturnsCorrectDateTime(): void
    {
        $clock = new FrozenClock('2021-01-01 00:00:00');
        $date = $clock->fromString('2021-02-01 00:00:00');

        $this->assertInstanceOf(\DateTimeImmutable::class, $date);
        $this->assertEquals('2021-02-01', $date->format('Y-m-d'));
        $this->assertEquals('00:00:00', $date->format('H:i:s'));
    }

    public function testSetToChangesTime(): void
    {
        $clock = new FrozenClock('2021-01-01 00:00:00');
        $newTime = new \DateTimeImmutable('2021-02-01 00:00:00');

        $clock->setTo($newTime);
        $this->assertEquals($newTime, $clock->now());

        $clock->setTo('2021-03-01 00:00:00');
        $this->assertEquals('2021-03-01', $clock->now()->format('Y-m-d'));
    }

    public function testMoveForwardAdvancesTime(): void
    {
        $clock = new FrozenClock('2021-01-01 00:00:00');

        $clock->moveForward('P1D'); // 1 Tag vorwärts
        $this->assertEquals('2021-01-02', $clock->now()->format('Y-m-d'));

        $clock->moveForward('PT1H'); // 1 Stunde vorwärts
        $this->assertEquals('2021-01-02 01:00:00', $clock->now()->format('Y-m-d H:i:s'));
    }

    public function testMoveBackwardReversesTime(): void
    {
        $clock = new FrozenClock('2021-01-10 10:00:00');

        $clock->moveBackward('P5D'); // 5 Tage zurück
        $this->assertEquals('2021-01-05', $clock->now()->format('Y-m-d'));

        $clock->moveBackward('PT5H'); // 5 Stunden zurück
        $this->assertEquals('2021-01-05 05:00:00', $clock->now()->format('Y-m-d H:i:s'));
    }

    public function testCustomTimezone(): void
    {
        $clock = new FrozenClock('2021-01-01 00:00:00', 'Europe/Berlin');
        $now = $clock->now();

        $this->assertEquals('Europe/Berlin', $now->getTimezone()->getName());
    }
}
