<?php

declare(strict_types=1);

namespace QUI\CalendarControls\Tests\Unit;

use PHPUnit\Framework\TestCase;
use SimpleXMLElement;

use function file_get_contents;
use function getimagesize;
use function json_decode;

use const JSON_THROW_ON_ERROR;

final class PackageStructureTest extends TestCase
{
    private const PACKAGE_ROOT = __DIR__ . '/../..';

    public function testComposerMetadataUsesCurrentPackageStandards(): void
    {
        $json = file_get_contents(self::PACKAGE_ROOT . '/composer.json');
        self::assertNotFalse($json);

        $composer = json_decode($json, true, flags: JSON_THROW_ON_ERROR);

        self::assertSame('quiqqer/calendar-controls', $composer['name']);
        self::assertSame('quiqqer-module', $composer['type']);
        self::assertSame('GPL-2.0-or-later', $composer['license']);
        self::assertSame('^8.2', $composer['require']['php']);
        self::assertArrayNotHasKey('version', $composer);
    }

    public function testPackageMetadataReferencesExistingAssets(): void
    {
        $Package = new SimpleXMLElement(
            (string)file_get_contents(self::PACKAGE_ROOT . '/package.xml')
        );

        self::assertSame(
            'URL_OPT_DIR/quiqqer/calendar-controls/bin/images/Logo.png',
            (string)$Package->package->image['src']
        );
        self::assertSame('support@quiqqer.com', (string)$Package->package->support->email);
        self::assertSame('GPL-2.0-or-later', (string)$Package->package->copyright->license);
    }

    /**
     * @dataProvider requiredImageProvider
     */
    public function testRequiredImagesHaveExpectedDimensions(string $file, int $width, int $height): void
    {
        $size = getimagesize(self::PACKAGE_ROOT . '/bin/images/' . $file);

        self::assertNotFalse($size);
        self::assertSame($width, $size[0]);
        self::assertSame($height, $size[1]);
    }

    /**
     * @return iterable<string, array{string, int, int}>
     */
    public static function requiredImageProvider(): iterable
    {
        yield 'README header' => ['Readme.png', 1200, 600];
        yield 'package logo' => ['Logo.png', 400, 300];
        yield 'GitLab avatar' => ['Gitlab.png', 100, 100];
    }
}
