<?php

namespace App\Modules\Template\TourTemplate\Domain;

use App\Modules\Shared\Domain\Storage\Criteria\StorageCriteria;
use Illuminate\Support\Facades\DB;

class IndexTourTemplate implements StorageCriteria
{

    public function __construct(
        private IdTourTemplate $id
    ) {}

    public function run()
    {
        return $this->analize($this->query());
    }

    protected function query()
    {
        $select = TourTemplate::select('`l`');
        $table = TourTemplate::table();
        return DB::select(
            "SELECT $select
            FROM `$table` `l`
            WHERE `l`.`Id_TourTemplate` = ?
            AND `l`.`TourTemplate_Status` <> 0",
            [
                $this->id->value()
            ]
        );
    }

    protected function analize($data)
    {
        if (!count($data)) {
            throw new TourTemplateNotFound();
        }
        return TourTemplate::fromDTO($data[0]);
    }
}
