<?php

namespace App\Modules\Leads\WebTypeClient\Domain;

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

class ExistsWebTypeClient implements StorageCriteria
{

    public function __construct(
        private IdWebTypeClient $id,
        private bool $lockInShare = false
    ) {}

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

    protected function query()
    {
        $select = WebTypeClient::select('`r`');
        $table = WebTypeClient::table();

        return DB::select(
            "SELECT $select
            FROM `$table` `r`
            WHERE `r`.`Id_WebTypeClient` = ?",
            [
                $this->id->value()
            ]
        );
    }

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