<?php

declare(strict_types=1);

namespace Ferienpass\AdminBundle\Tests\Normalizer;

use Ferienpass\AdminBundle\Normalizer\CurrencyExcelNormalizer;
use Ferienpass\CoreBundle\Dto\Currency;
use PhpOffice\PhpSpreadsheet\Style\NumberFormat;
use PhpOffice\PhpSpreadsheet\Style\Style;
use PHPUnit\Framework\TestCase;

final class CurrencyExcelNormalizerTest extends TestCase
{
    private CurrencyExcelNormalizer $normalizer;

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

    public function testSupportsNormalizationForCurrencyWithXlsxFormat(): void
    {
        $currency = new Currency(1000, 100);

        self::assertTrue($this->normalizer->supportsNormalization($currency, 'xlsx'));
    }

    public function testDoesNotSupportNormalizationForNonXlsxFormat(): void
    {
        $currency = new Currency(1000, 100);

        self::assertFalse($this->normalizer->supportsNormalization($currency, 'json'));
    }

    public function testDoesNotSupportNormalizationForNonCurrency(): void
    {
        self::assertFalse($this->normalizer->supportsNormalization(new \stdClass(), 'xlsx'));
    }

    public function testNormalizeReturnsAmountDividedByDivisor(): void
    {
        $currency = new Currency(1000, 100);

        $result = $this->normalizer->normalize($currency, 'xlsx');

        self::assertSame(10.0, (float) $result);
    }

    public function testNormalizeWithSmallAmount(): void
    {
        $currency = new Currency(50, 100);

        $result = $this->normalizer->normalize($currency, 'xlsx');

        self::assertSame(0.5, (float) $result);
    }

    public function testGetSupportedTypes(): void
    {
        $types = $this->normalizer->getSupportedTypes('xlsx');

        self::assertArrayHasKey(Currency::class, $types);
        self::assertTrue($types[Currency::class]);
    }

    public function testNormalizeAppliesStyleWhenContextHasCellStyle(): void
    {
        $currency = new Currency(500, 100);

        $numberFormat = $this->createMock(NumberFormat::class);
        $numberFormat->expects(self::once())
            ->method('setFormatCode')
            ->with(NumberFormat::FORMAT_CURRENCY_EUR);

        $style = $this->createMock(Style::class);
        $style->method('getNumberFormat')->willReturn($numberFormat);

        $result = $this->normalizer->normalize($currency, 'xlsx', ['xlsx_cell_style' => $style]);

        self::assertSame(5.0, (float) $result);
    }
}
