<?php

declare(strict_types=1);

namespace CpCreation\VitiCore\Tests\Cart;

use CpCreation\VitiCore\Cart\CartPricing;
use CpCreation\VitiCore\Cart\CartPromo;
use CpCreation\VitiCore\Cart\Model\Cart;
use PHPUnit\Framework\TestCase;

final class CartDiscountStateTest extends TestCase
{
    public function testCartWithoutDiscountIsDetected(): void
    {
        self::assertFalse(new Cart()->hasExistingDiscount());
    }

    public function testAppliedPromoIsDetectedAsExistingDiscount(): void
    {
        $cart = new Cart();
        $cart->addPromo(new CartPromo(discountItemsTTC: 500));

        self::assertTrue($cart->hasExistingDiscount());
    }

    public function testPersistedAppliedPromoStatusIsDetectedWithoutPricingCache(): void
    {
        $cart = new Cart();
        $cart->setPromoCodeStatus(CartPromo::STATUS_APPLIED);

        self::assertTrue($cart->hasExistingDiscount());
    }

    public function testCatalogAndDegressiveDiscountsAreDetected(): void
    {
        $catalogDiscountCart = new Cart();
        $catalogDiscountCart->setPricing($this->createPricing(discountTTC: 500));
        $degressiveDiscountCart = new Cart();
        $degressiveDiscountCart->setPricing($this->createPricing(degressiveTTC: 500));

        self::assertTrue($catalogDiscountCart->hasExistingDiscount());
        self::assertTrue($degressiveDiscountCart->hasExistingDiscount());
    }

    private function createPricing(int $discountTTC = 0, int $degressiveTTC = 0): CartPricing
    {
        return new CartPricing(
            lines: [],
            taxRate: 20.0,
            subtotalHT: 1000,
            subtotalTax: 200,
            subtotalTTC: 1200,
            discountHT: 0,
            discountTax: 0,
            discountTTC: $discountTTC,
            degressiveHT: 0,
            degressiveTax: 0,
            degressiveTTC: $degressiveTTC,
            totalHT: 1000,
            totalTax: 200,
            totalTTC: 1200,
        );
    }
}
