<?php

declare(strict_types=1);

namespace App\Repositories;

use System\Database\QueryBuilder;

abstract class BaseRepository
{
    protected QueryBuilder $query;

    protected string $table;

    public function __construct(QueryBuilder $query)
    {
        $this->query = $query;
    }

    public function all(): array
    {
        return $this->query
            ->table($this->table)
            ->get();
    }

    public function find(int|string $id): ?array
    {
        return $this->query
            ->table($this->table)
            ->find($id);
    }

    public function create(array $data): bool
    {
        return $this->query
            ->table($this->table)
            ->insert($data);
    }

    public function update(int|string $id, array $data): bool
    {
        return $this->query
            ->table($this->table)
            ->where('id', $id)
            ->update($data);
    }

    public function delete(int|string $id): bool
    {
        return $this->query
            ->table($this->table)
            ->where('id', $id)
            ->delete();
    }
}