<?php

namespace MintyPHP\Tests\Service\Tenant;

use MintyPHP\Service\Tenant\TenantLogoService;
use PHPUnit\Framework\TestCase;

/**
 * Service behaviour test. Upload flows are covered via path-independent
 * assertions (MIME + SVG guards + dir creation semantics) because
 * move_uploaded_file() requires a real HTTP-uploaded file and cannot run
 * under phpunit. Integration coverage for the move step lives in the
 * manual smoketest documented in the execution report.
 */
class TenantLogoServiceTest extends TestCase
{
    private TenantLogoService $service;

    protected function setUp(): void
    {
        $this->service = new TenantLogoService();
    }

    public function testIsValidThemeAcceptsWhitelistedThemes(): void
    {
        $this->assertTrue($this->service->isValidTheme('light'));
        $this->assertTrue($this->service->isValidTheme('dark'));
    }

    public function testIsValidThemeRejectsUnknownThemes(): void
    {
        $this->assertFalse($this->service->isValidTheme(''));
        $this->assertFalse($this->service->isValidTheme('dark-green'));
        $this->assertFalse($this->service->isValidTheme('LIGHT'));
    }

    public function testIsValidUuidRejectsMalformedInput(): void
    {
        $this->assertFalse($this->service->isValidUuid('not-a-uuid'));
        $this->assertFalse($this->service->isValidUuid(''));
    }

    public function testIsValidUuidAcceptsCanonicalUuid(): void
    {
        $this->assertTrue($this->service->isValidUuid('11111111-2222-3333-4444-555555555555'));
    }

    public function testFindLogoPathRejectsInvalidTheme(): void
    {
        $this->assertNull($this->service->findLogoPath('11111111-2222-3333-4444-555555555555', 'neon'));
    }

    public function testFindLogoPathRejectsInvalidUuid(): void
    {
        $this->assertNull($this->service->findLogoPath('bogus', 'light'));
    }

    public function testHasLogoReturnsFalseForMissingDirectory(): void
    {
        $this->assertFalse($this->service->hasLogo('11111111-2222-3333-4444-555555555555', 'light'));
    }

    public function testTenantLogoDirIncludesThemeSegment(): void
    {
        $uuid = '11111111-2222-3333-4444-555555555555';
        $this->assertStringEndsWith('/tenants/' . $uuid . '/logo/light', $this->service->tenantLogoDir($uuid, 'light'));
        $this->assertStringEndsWith('/tenants/' . $uuid . '/logo/dark', $this->service->tenantLogoDir($uuid, 'dark'));
    }

    public function testSaveUploadRejectsInvalidUuid(): void
    {
        $result = $this->service->saveUpload('not-uuid', 'light', [
            'tmp_name' => '/tmp/whatever',
            'error' => UPLOAD_ERR_OK,
            'size' => 100,
        ]);
        $this->assertFalse($result['ok']);
    }

    public function testSaveUploadRejectsInvalidTheme(): void
    {
        $result = $this->service->saveUpload('11111111-2222-3333-4444-555555555555', 'neon', [
            'tmp_name' => '/tmp/whatever',
            'error' => UPLOAD_ERR_OK,
            'size' => 100,
        ]);
        $this->assertFalse($result['ok']);
    }

    public function testSaveUploadRejectsOversizedFile(): void
    {
        $result = $this->service->saveUpload('11111111-2222-3333-4444-555555555555', 'light', [
            'tmp_name' => '/tmp/whatever',
            'error' => UPLOAD_ERR_OK,
            'size' => 10 * 1024 * 1024,
        ]);
        $this->assertFalse($result['ok']);
    }

    public function testSaveUploadRejectsMissingFile(): void
    {
        $result = $this->service->saveUpload('11111111-2222-3333-4444-555555555555', 'light', []);
        $this->assertFalse($result['ok']);
    }

    public function testDeleteRejectsInvalidUuid(): void
    {
        $this->assertFalse($this->service->delete('bogus', 'light'));
    }

    public function testDeleteRejectsInvalidTheme(): void
    {
        $this->assertFalse($this->service->delete('11111111-2222-3333-4444-555555555555', 'neon'));
    }
}
