<?php

namespace App\Modules\DefinedService\OfficesBriefing\Domain;

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

class ExistsBriefingOffice implements StorageCriteria
{
    public function __construct(
        private IdBriefingOffice $id,
        private bool $lockInShare = false
    ) {}
    public function run()
    {
        return $this->analize($this->query());
    }
    public function query()
    {
        $select = BriefingOffice::select('`OB`');
        $table = BriefingOffice::table();
        return DB::select(
            "SELECT $select
            FROM `$table` `OB`
            WHERE `OB`.`Id_BriefingOffice` = ?",
            [
                $this->id->value()
            ]
        );
    }

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