<?php

namespace MintyPHP\Tests\Module\Helpdesk\Service;

use MintyPHP\Http\SessionStoreInterface;
use MintyPHP\Module\Helpdesk\Service\DebitorCacheControl;
use PHPUnit\Framework\TestCase;

class DebitorCacheControlTest extends TestCase
{
    public function testParseRefreshFlagRecognizesTruthyValues(): void
    {
        $this->assertTrue(DebitorCacheControl::parseRefreshFlag('1'));
        $this->assertTrue(DebitorCacheControl::parseRefreshFlag('true'));
        $this->assertTrue(DebitorCacheControl::parseRefreshFlag('YES'));
        $this->assertTrue(DebitorCacheControl::parseRefreshFlag(true));
    }

    public function testParseRefreshFlagReturnsFalseForNonTruthyValues(): void
    {
        $this->assertFalse(DebitorCacheControl::parseRefreshFlag(''));
        $this->assertFalse(DebitorCacheControl::parseRefreshFlag('0'));
        $this->assertFalse(DebitorCacheControl::parseRefreshFlag('false'));
        $this->assertFalse(DebitorCacheControl::parseRefreshFlag('nope'));
        $this->assertFalse(DebitorCacheControl::parseRefreshFlag(false));
        $this->assertFalse(DebitorCacheControl::parseRefreshFlag(null));
    }

    public function testResolveTenantScopeUsesTenantIdOrGlobal(): void
    {
        $this->assertSame('9', DebitorCacheControl::resolveTenantScope([
            'current_tenant' => ['id' => 9],
        ]));

        $this->assertSame('global', DebitorCacheControl::resolveTenantScope([]));
    }

    public function testInvalidateDebitorCachesRemovesDebitorAndTenantSharedKeys(): void
    {
        $sessionStore = $this->createMock(SessionStoreInterface::class);
        $sessionStore->expects($this->exactly(14))
            ->method('remove')
            ->with($this->callback(static function (string $key): bool {
                return str_contains($key, '.13.10259')
                    || $key === 'module.helpdesk.debitor.v1.escalation-definitions.13';
            }));

        DebitorCacheControl::invalidateDebitorCaches($sessionStore, '13', '10259', true);
    }

    public function testContractLinesKeyFormat(): void
    {
        $key = DebitorCacheControl::contractLinesKey('5', '10254');
        $this->assertSame('module.helpdesk.debitor.v2.contract-lines.5.10254', $key);
    }

    public function testAllDebitorCacheKeysIncludesContractLinesKey(): void
    {
        $keys = DebitorCacheControl::allDebitorCacheKeys('5', '10254');
        $this->assertContains('module.helpdesk.debitor.v2.contract-lines.5.10254', $keys);
    }

    public function testControllingTicketsKeyFormat(): void
    {
        $key = DebitorCacheControl::controllingTicketsKey('7', '10610');
        $this->assertSame('module.helpdesk.debitor.v2.controlling-tickets.7.10610', $key);
    }

    public function testAllDebitorCacheKeysIncludesControllingKey(): void
    {
        $keys = DebitorCacheControl::allDebitorCacheKeys('7', '10610');
        $this->assertContains('module.helpdesk.debitor.v2.controlling-tickets.7.10610', $keys);
    }

    public function testMeetingsKeyFormat(): void
    {
        $key = DebitorCacheControl::meetingsKey('3', '10254');
        $this->assertSame('module.helpdesk.debitor.v2.meetings.3.10254', $key);
    }

    public function testAllDebitorCacheKeysIncludesMeetingsKey(): void
    {
        $keys = DebitorCacheControl::allDebitorCacheKeys('3', '10254');
        $this->assertContains('module.helpdesk.debitor.v2.meetings.3.10254', $keys);
    }
}
