<?php

namespace Src\Modules\Auth\Infrastructure\PersistLayer;

use Illuminate\Support\Facades\DB;
use Src\Modules\Auth\Interfaces\Infrastructure\PersistLayer\IAdminsLayer;
use Src\Modules\Auth\Interfaces\Dto\Admin\IAdminPersist;

class AdminsLayer implements IAdminsLayer
{
    private string $table;

    public function setTable(string $table): void
    {
        $this->table = $table;
    }

    public function insert(IAdminPersist $dto): int
    {
        $qb = DB::table($this->table);
        $attrs = $dto->getInsertAttrs();
        return $qb->insertGetId($attrs);
    }

    public function update(IAdminPersist $dto): int
    {
        $qb = DB::table($this->table);
        $attrs = $dto->getUpdatedAttrs();
        if($attrs){
            return $qb->where($this->table . '.id', '=', $dto->getId())->update($attrs);
        }
        return 0;
    }

    public function delete(string $adminId): int
    {
        $qb = DB::table($this->table);
        return $qb->where($this->table . '.id', '=', $adminId)->delete();
    }
}