<?php

namespace App\Http\Controllers\Owner\GroupProviderMedia;

use App\Http\Controllers\Controller;
use App\Http\Helpers\ResponseBuilder;
use App\Modules\GroupProviderMedia\Application\GroupProviderMediaResponses;
use App\Modules\GroupProviderMedia\Application\List\ListGroupProviderMediaQuery;
use App\Modules\Shared\Domain\Bus\Query\QueryBus;
use App\Modules\Shared\Domain\Transaction;
use App\Providers\StorageProvider;

class GroupProviderMediaListController extends Controller
{
    private $domain;

    public function __construct(
        private QueryBus $queryBus,
        private Transaction $transaction,
        private StorageProvider $storage
    ) {
        $this->domain = "GroupProviderMedia";
    }

    public function __invoke($Id_GroupProvider, $Action)
    {
        $this->transaction->begin();
        /** @var GroupProviderMediaResponses $response */
        $response = $this->queryBus->ask(new ListGroupProviderMediaQuery($Id_GroupProvider, $Action));
        $data = array_map([$this, 'appendPath'], $response->data());
        $this->transaction->commit();

        return ResponseBuilder::Response([
            "Response_Status"           => 200,
            "Response_Code"             => 200,
            "Response_Data"             => $data,
            "Response_Domain"           => $this->domain,
            "Response_Message"          => 'Listado con éxito',
            "Response_Error_Message"    => '',
            "Response_Error_Reason"     => '',
        ]);
    }

    public function appendPath($gpm)
    {
        $this->storage->appendPublicToObject($gpm, 'GroupProviderMedia_Path');
        $this->storage->appendPublicToObject($gpm, 'GroupProviderMedia_Path_Thumb');
        $this->storage->appendPublicToObject($gpm, 'GroupProviderMedia_Path_LargeThumb');
        return $gpm;
    }
}
