<?php

namespace App\Modules\DefinedService\DefinedService\Domain;

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

class IndexDefinedService implements StorageCriteria
{

    public function __construct(
        private IdDefinedService $id
    ) {}

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

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

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