<?php

use App\Models\Host;

// Der Altbestand lag in der KONFIGURIERTEN Zone (im Test `clupilot.cloud`) —
// etwas anderes konnte der Client nie schreiben, und seit dem Umzug auf die
// Cloud-API weist er einen fremden Namen auch ab, statt ihn stillschweigend an
// die Zone anzuhängen. Vorher stand hier `.com`, ein Wert, den es so nie gab.

it('reports nothing to do when no host carries a public record id', function () {
    fakeServices();
    Host::factory()->active()->create(['dns_record_id' => null]);

    $this->artisan('clupilot:prune-host-dns')
        ->assertSuccessful()
        ->expectsOutputToContain('Nothing to do');
});

it('without --force only reports what it would delete, and changes nothing', function () {
    $s = fakeServices();
    $recordId = $s['dns']->upsertRecord('fsn-legacy.node.clupilot.cloud', 'A', '10.66.0.55');
    $host = Host::factory()->active()->create(['dns_record_id' => $recordId]);

    $this->artisan('clupilot:prune-host-dns')->assertSuccessful();

    expect($host->fresh()->dns_record_id)->toBe($recordId)
        ->and($s['dns']->records)->toHaveKey('fsn-legacy.node.clupilot.cloud');
});

it('with --force deletes the record and clears the column', function () {
    $s = fakeServices();
    $recordId = $s['dns']->upsertRecord('fsn-legacy.node.clupilot.cloud', 'A', '10.66.0.55');
    $host = Host::factory()->active()->create(['dns_record_id' => $recordId]);

    $this->artisan('clupilot:prune-host-dns --force')->assertSuccessful();

    expect($host->fresh()->dns_record_id)->toBeNull()
        ->and($s['dns']->records)->not->toHaveKey('fsn-legacy.node.clupilot.cloud');
});

it('with --force leaves the id in place when the provider refuses the deletion', function () {
    $s = fakeServices();
    $recordId = $s['dns']->upsertRecord('fsn-legacy.node.clupilot.cloud', 'A', '10.66.0.55');
    $host = Host::factory()->active()->create(['dns_record_id' => $recordId]);
    $s['dns']->failDelete = true;

    // A failed provider call must not silently forget which record still
    // needs cleaning up — nulling the column here would do exactly that.
    $this->artisan('clupilot:prune-host-dns --force')->assertFailed();

    expect($host->fresh()->dns_record_id)->toBe($recordId);
});
