<?php

declare(strict_types=1);

namespace App\CreatorsApi\Model;

/**
 * Adattatore di compatibilità (mantiene proprietà pubbliche simili all’originale).
 */
final class AmazonItem
{
    private ProductItem $item;

    public string $title = '';
    public float $price = 0.0;
    public float $fullprice = 0.0;

    public int $saving = 0;
    public string $link = '';
    public string $asin = '';
    public string $image = '';

    public bool $hasPrimePrice = false;
    public array $primePrices = [];

    private string $partnerTag;
    private string $trackingPlaceholder;

    public function __construct(ProductItem $item, string $partnerTag = 'blazemedia-21', string $trackingPlaceholder = 'booBLZTRKood')
    {
        $this->item = $item;
        $this->partnerTag = $partnerTag;
        $this->trackingPlaceholder = $trackingPlaceholder;

        $this->asin = $this->item->getAsin() ?? '';
        $this->title = $this->item->getTitle() ?? '';
        $this->price = $this->getPrice();
        $this->saving = $this->getSaving();
        $this->fullprice = $this->getFullPrice();
        $this->link = $this->addTrackingPlaceholder($this->item->getDetailPageURL() ?? '');
        $this->image = $this->getImage();

        $this->hasPrimePrice = $this->hasPrimeExclusive();
        $this->primePrices = $this->hasPrimePrice ? $this->getPrimePrice() : [];
    }

    public static function fromApiData(array $itemData, string $partnerTag = 'blazemedia-21', string $trackingPlaceholder = 'booBLZTRKood'): self
    {
        $productItem = new ProductItem($itemData);
        return new self($productItem, $partnerTag, $trackingPlaceholder);
    }

    public function toArray(): array
    {
        return [
            'title' => $this->title,
            'asin' => $this->asin,
            'price' => $this->price,
            'fullprice' => $this->fullprice,
            'saving' => $this->saving,
            'link' => $this->link,
            'images' => $this->image,
            'hasPrimeExclusive' => $this->hasPrimePrice,
            'primePrices' => $this->primePrices,
        ];
    }

    private function addTrackingPlaceholder(string $link): string
    {
        if ($link === '') {
            return '';
        }
        return str_replace($this->partnerTag, $this->trackingPlaceholder, $link);
    }

    private function getImage(): string
    {
        return $this->item->getImageUrl('large') ?? '';
    }

    private function getPrice(): float
    {
        $price = $this->item->getPrice();
        return $price ? $price->getAmount() : 0.0;
    }

    private function getSaving(): int
    {
        $discount = $this->item->getDiscountPercentage();
        return $discount ? (int) round($discount) : 0;
    }

    private function getFullPrice(): float
    {
        $originalPrice = $this->item->getOriginalPrice();
        if ($originalPrice) {
            return $originalPrice->getAmount();
        }
        return $this->getPrice();
    }

    private function hasPrimeExclusive(): bool
    {
        return $this->item->hasPrimeOffer();
    }

    private function getPrimePrice(): array
    {
        if (!$this->hasPrimeExclusive()) {
            return [];
        }

        $currentPrice = $this->getPrice();
        $fullPrice = $this->getFullPrice();

        $savingAmount = $fullPrice - $currentPrice;
        $savingPercentage = $fullPrice > 0 ? (($savingAmount / $fullPrice) * 100) : 0;

        return [
            'price' => $currentPrice,
            'saving' => round($savingAmount, 2),
            'fullprice' => $fullPrice,
            'saving_percentage' => round($savingPercentage, 2),
        ];
    }

    public function __get(string $name)
    {
        if (property_exists($this, $name)) {
            return $this->$name;
        }
        return null;
    }

    public function __isset(string $name): bool
    {
        return property_exists($this, $name);
    }
}
