<?php

namespace App\Modules\Leads\WebPage\Domain;

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

class IndexWebPage implements StorageCriteria
{

    public function __construct(
        private IdWebPage $id
    ) {}

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

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

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