<?php

namespace App\Modules\DefinedService\BriefingPassenger\Infrastructure;

use App\Modules\DefinedService\BriefingPassenger\Domain\BriefingPassenger;
use App\Modules\DefinedService\BriefingPassenger\Domain\BriefingPassengerRepository;
use App\Modules\DefinedService\BriefingPassenger\Domain\IdBriefingPassenger;
use App\Modules\Shared\Domain\Storage\Criteria\StorageCriteria;
use Illuminate\Support\Facades\DB;

final class MysqlBriefingPassengerRepository implements BriefingPassengerRepository
{
    public function find(StorageCriteria $criteria): BriefingPassenger
    {
        /** @var BriefingPassenger $entity */
        $entity = $criteria->run();
        return $entity;
    }

    public function search(StorageCriteria $criteria): array
    {
        return $criteria->run();
    }

    public function insert(BriefingPassenger $value): IdBriefingPassenger
    {
        $data = $value->toArray();
        $id = DB::table('t_briefing_passenger')->insertGetId($data);
        return new IdBriefingPassenger((int) $id);
    }

    public function update(BriefingPassenger $value)
    {
        $data = $value->toArray();
        $id   = (int) $value->id()->value();
        unset($data['Id_BriefingPassenger']);

        return DB::table('t_briefing_passenger')
            ->where('Id_BriefingPassenger', $id)
            ->update($data);
    }
}
