<?php

namespace App\Services\Dns;

use RuntimeException;

class FakeHetznerDnsClient implements HetznerDnsClient
{
    /** @var array<string, string> fqdn => record_id */
    public array $records = [];

    /** @var array<string, string> fqdn => published value — without this a test
     *  cannot tell which address ended up in DNS, which is the whole point for
     *  host records. */
    public array $values = [];

    public bool $failUpsert = false;

    public bool $failDelete = false;

    private int $counter = 1;

    public function upsertRecord(string $fqdn, string $type, string $value): string
    {
        if ($this->failUpsert) {
            throw new RuntimeException('hetzner dns 5xx');
        }

        $this->values[$fqdn] = $value;

        return $this->records[$fqdn] ??= 'rec-'.$this->counter++;
    }

    public function deleteRecord(string $recordId): void
    {
        if ($this->failDelete) {
            throw new RuntimeException('hetzner dns 5xx');
        }

        $this->records = array_filter($this->records, fn ($id) => $id !== $recordId);
    }
}
