<?php

declare(strict_types=1);

namespace CalendarPlugin\Infrastructure\WordPress;

use CalendarPlugin\Contracts\DatabaseAdapterInterface;

final class WordPressDatabaseAdapter implements DatabaseAdapterInterface
{
    public function __construct(private readonly object $wpdb)
    {
    }

    public function getPrefix(): string
    {
        return (string) $this->wpdb->prefix;
    }

    public function prepare(string $query, mixed ...$args): string
    {
        return (string) $this->wpdb->prepare($query, ...$args);
    }

    public function query(string $query): int|false
    {
        return $this->wpdb->query($query);
    }

    public function getResults(string $query): array
    {
        return $this->wpdb->get_results($query) ?: [];
    }

    public function getRow(string $query): ?object
    {
        $row = $this->wpdb->get_row($query);
        return is_object($row) ? $row : null;
    }

    public function insert(string $table, array $data, array $formats = []): int|false
    {
        return $this->wpdb->insert($table, $data, $formats);
    }

    public function update(string $table, array $data, array $where, array $formats = [], array $whereFormats = []): int|false
    {
        return $this->wpdb->update($table, $data, $where, $formats, $whereFormats);
    }

    public function delete(string $table, array $where, array $whereFormats = []): int|false
    {
        return $this->wpdb->delete($table, $where, $whereFormats);
    }

    public function insertId(): int
    {
        return (int) $this->wpdb->insert_id;
    }
}
