<?php

namespace CouponsPlus\App\Conditions;

use CouponsPlus\Original\Collections\Collection;

/**
 * This represents a cart item that is pending to be added or removed to and from the cart.
 *
 * This class alongside with VirtualCartItem in their current implementations feel like a hack because
 * they were added after the main architecture was already in place.
 *
 * I want to refactor this when i have time to make (ha!)
 */
class PendingCartItem extends CartItem
{
    /** @var 'add'|'remove' $action */
    protected string $action = 'add';
    private int $quantity = 0;

    /**
     * @param CartItem $item
     * @param int $quantity
     * @param 'add'|'remove' $action
     * @return self
     */
    public static function create(?CartItem $item = null, int $quantity = 1, ?string $action = null): self
    {
        $instance = new static(
            $item->getOriginalState()->asArray(),
        );

        $instance->setQuantity($quantity);
        $action && $instance->setAction($action);

        return $instance;
    }
    public function setQuantity(int $quantity): void
    {
        $this->quantity = $quantity;
    }

    public function getQuantity(): int
    {
        return $this->quantity;
    }

    protected function getStateCollection(): Collection
    {
        return $this->getOriginalState();
    }

    public function setAction(string $action): void
    {
        $this->action = $action;
    }

    public function shouldAdd(): bool
    {
        return $this->action === 'add';
    }

    public function shouldRemove(): bool
    {
        return $this->action === 'remove';
    }
}