<?php

namespace App\Domains\Domain\Actions;

use App\Domains\Domain\Models\Domain;

class VerifyDomain
{
    public function handle(Domain $domain): bool
    {
        $records = @dns_get_record("_nimuli.{$domain->hostname}", DNS_TXT);

        if (! is_array($records)) {
            return false;
        }

        foreach ($records as $record) {
            if (($record['txt'] ?? '') === "nimuli-verify={$domain->verification_token}") {
                $domain->update([
                    'verified_at' => now(),
                    'ssl_status' => 'pending',
                ]);

                return true;
            }
        }

        return false;
    }
}
