<?php

namespace KupShop\SellerBundle\Twig\Components;

use KupShop\ComponentsBundle\Attributes\Component;
use KupShop\ComponentsBundle\Attributes\Version;
use KupShop\ComponentsBundle\Entity\Thumbnail;
use KupShop\ComponentsBundle\Twig\BaseComponent;
use KupShop\SellerBundle\Twig\SetActiveSellerTrait;
use Symfony\UX\LiveComponent\Attribute\AsLiveComponent;
use Symfony\UX\LiveComponent\Attribute\LiveProp;
use Symfony\UX\LiveComponent\ComponentToolsTrait;
use Symfony\UX\LiveComponent\DefaultActionTrait;

#[AsLiveComponent(template: '@Seller/components/SellerDetails/SellerDetails.1.html.twig')]
#[Component(1, [
    new Version(1),
])]
class SellerDetails extends BaseComponent
{
    use DefaultActionTrait;
    use ComponentToolsTrait;
    use SetActiveSellerTrait;

    // kvuli nejake casti seller pole je potreba manualni mapovani a hydrace/dehydrace
    #[LiveProp(hydrateWith: 'hydrateSeller', dehydrateWith: 'dehydrateSeller')]
    public array $seller;

    public function dehydrateSeller(array $seller)
    {
        return [
            'id' => $seller['id'],
            'x' => $seller['x'],
            'y' => $seller['y'],
            'title' => $seller['title'],
            'thumbnails' => $seller['thumbnails'] ?? [],
            'street' => $seller['street'],
            'number' => $seller['number'],
            'psc' => $seller['psc'],
            'city' => $seller['city'],
            'phone' => $seller['phone'],
            'email' => $seller['email'],
            'web' => $seller['web'],
            'opening_hours' => $seller['opening_hours'],
            'id_store' => $seller['id_store'],
            'descr' => $seller['descr'],
        ];
    }

    public function hydrateSeller(array $seller)
    {
        $thumbnails = array_map(function ($item) {
            return new Thumbnail($item['id'], $item['title'], $item['version']);
        }, $seller['thumbnails']);

        return [
            'id' => $seller['id'],
            'x' => $seller['x'],
            'y' => $seller['y'],
            'title' => $seller['title'],
            'thumbnails' => $thumbnails,
            'street' => $seller['street'],
            'number' => $seller['number'],
            'psc' => $seller['psc'],
            'city' => $seller['city'],
            'phone' => $seller['phone'],
            'email' => $seller['email'],
            'web' => $seller['web'],
            'opening_hours' => $seller['opening_hours'],
            'id_store' => $seller['id_store'],
            'descr' => $seller['descr'],
        ];
    }
}
