<?php

/*
 * This file is part of the VITICORE2 package.
 *
 * (c) CP Creation <web@cpcreation.fr>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace CpCreation\VitiCore\Tests\Shop\Model;

use CpCreation\VitiCore\Shop\Model\ShopConfig;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;

final class ShopConfigTest extends TestCase
{
    #[DataProvider('priceVisibilityProvider')]
    public function testPriceVisibilityDependsOnConfigurationAndPartnerCode(
        bool $hidePriceDefault,
        ?string $partnerCode,
        bool $expected,
    ): void {
        $config = new ShopConfig();
        $config->setHidePriceDefault($hidePriceDefault);

        self::assertSame($expected, $config->shouldShowPrices($partnerCode));
    }

    public static function priceVisibilityProvider(): iterable
    {
        yield 'option disabled without partner code' => [false, null, true];
        yield 'option disabled with partner code' => [false, 'PRO', true];
        yield 'option enabled without partner code' => [true, null, false];
        yield 'option enabled with empty partner code' => [true, '', false];
        yield 'option enabled with partner code' => [true, 'PRO', true];
    }

    public function testVacationIsActiveOnlyStrictlyBetweenConfiguredDates(): void
    {
        $config = new ShopConfig();
        $config->setVacationOf(new \DateTime('2026-07-20 08:00:00'));
        $config->setVacationAt(new \DateTime('2026-07-27 18:00:00'));

        self::assertFalse($config->isVacationActive(new \DateTimeImmutable('2026-07-20 08:00:00')));
        self::assertTrue($config->isVacationActive(new \DateTimeImmutable('2026-07-23 12:00:00')));
        self::assertFalse($config->isVacationActive(new \DateTimeImmutable('2026-07-27 18:00:00')));
    }

    public function testShopIsDisabledOnlyDuringActiveVacationWhenOptionIsEnabled(): void
    {
        $config = new ShopConfig();
        $config->setVacationOf(new \DateTime('2026-07-20 08:00:00'));
        $config->setVacationAt(new \DateTime('2026-07-27 18:00:00'));
        $referenceDate = new \DateTimeImmutable('2026-07-23 12:00:00');

        self::assertFalse($config->isShopDisabledForVacation($referenceDate));

        $config->setDisableShopForVacation(true);

        self::assertTrue($config->isShopDisabledForVacation($referenceDate));
        self::assertFalse($config->isShopDisabledForVacation(new \DateTimeImmutable('2026-07-28 12:00:00')));
    }

    public function testOrderingIsDisabledOnlyDuringActiveVacationWhenOptionIsEnabled(): void
    {
        $config = new ShopConfig();
        $config->setVacationOf(new \DateTime('2026-07-20 08:00:00'));
        $config->setVacationAt(new \DateTime('2026-07-27 18:00:00'));
        $referenceDate = new \DateTimeImmutable('2026-07-23 12:00:00');

        self::assertFalse($config->isOrderingDisabledForVacation($referenceDate));

        $config->setDisableOrdersForVacation(true);

        self::assertTrue($config->isOrderingDisabledForVacation($referenceDate));
        self::assertFalse($config->isOrderingDisabledForVacation(new \DateTimeImmutable('2026-07-28 12:00:00')));
    }
}
