<?php

namespace MintyPHP\Tests\Service\Auth;

use MintyPHP\Http\SessionStore;
use MintyPHP\Service\Auth\AuthSessionTenantContextService;
use MintyPHP\Service\User\UserSavedFilterService;
use MintyPHP\Service\User\UserTenantContextService;
use PHPUnit\Framework\TestCase;

class AuthSessionTenantContextServiceTest extends TestCase
{
    protected function setUp(): void
    {
        $_SESSION = [];
    }

    public function testHydrateMarksNoActiveTenantWhenUserHasNoTenants(): void
    {
        $_SESSION['current_tenant'] = ['id' => 9];

        $userTenantContextService = $this->createMock(UserTenantContextService::class);
        $userTenantContextService->method('getAvailableTenants')->willReturn([]);
        $userTenantContextService->method('getAvailableDepartmentsByTenant')->willReturn([]);
        $userTenantContextService->expects($this->never())->method('getCurrentTenant');

        $savedFilterGateway = $this->createMock(UserSavedFilterService::class);
        $savedFilterGateway->method('listForAddressBook')->willReturn([]);

        $service = new AuthSessionTenantContextService(
            $userTenantContextService,
            $savedFilterGateway,
            new SessionStore()
        );

        $service->hydrateForUser(5);

        $this->assertSame([], $_SESSION['available_tenants']);
        $this->assertSame([], $_SESSION['available_departments_by_tenant']);
        $this->assertSame([], $_SESSION['address_book_saved_filters']);
        $this->assertTrue((bool) $_SESSION['no_active_tenant']);
        $this->assertArrayNotHasKey('current_tenant', $_SESSION);
    }

    public function testHydrateUsesFirstAvailableTenantAsFallback(): void
    {
        $availableTenants = [
            ['id' => 7, 'uuid' => 'tenant-7'],
            ['id' => 8, 'uuid' => 'tenant-8'],
        ];
        $availableDepartments = ['7' => [['id' => 11]]];
        $savedFilters = [['name' => 'Favorites']];

        $userTenantContextService = $this->createMock(UserTenantContextService::class);
        $userTenantContextService->method('getAvailableTenants')->willReturn($availableTenants);
        $userTenantContextService->method('getAvailableDepartmentsByTenant')->willReturn($availableDepartments);
        $userTenantContextService->method('getCurrentTenant')->willReturn(null);

        $savedFilterGateway = $this->createMock(UserSavedFilterService::class);
        $savedFilterGateway->method('listForAddressBook')->willReturn($savedFilters);

        $service = new AuthSessionTenantContextService(
            $userTenantContextService,
            $savedFilterGateway,
            new SessionStore()
        );

        $service->hydrateForUser(5);

        $this->assertSame($availableTenants, $_SESSION['available_tenants']);
        $this->assertSame($availableDepartments, $_SESSION['available_departments_by_tenant']);
        $this->assertSame($savedFilters, $_SESSION['address_book_saved_filters']);
        $this->assertFalse((bool) $_SESSION['no_active_tenant']);
        $this->assertSame(7, (int) ($_SESSION['current_tenant']['id'] ?? 0));
    }

    public function testHydratePreservesThemeAndPolicyFieldsInSession(): void
    {
        $availableTenants = [
            [
                'id' => 3,
                'uuid' => 'tenant-3',
                'default_theme' => 'dark',
                'allow_user_theme' => '0',
            ],
        ];

        $userTenantContextService = $this->createMock(UserTenantContextService::class);
        $userTenantContextService->method('getAvailableTenants')->willReturn($availableTenants);
        $userTenantContextService->method('getAvailableDepartmentsByTenant')->willReturn([]);
        $userTenantContextService->method('getCurrentTenant')->willReturn($availableTenants[0]);

        $savedFilterGateway = $this->createMock(UserSavedFilterService::class);
        $savedFilterGateway->method('listForAddressBook')->willReturn([]);

        $service = new AuthSessionTenantContextService(
            $userTenantContextService,
            $savedFilterGateway,
            new SessionStore()
        );

        $service->hydrateForUser(10);

        $tenant = $_SESSION['current_tenant'] ?? [];
        $this->assertSame('dark', $tenant['default_theme'] ?? null);
        $this->assertSame('0', $tenant['allow_user_theme'] ?? null);
    }
}
