Skip to content

An open-source, self-hostable PaaS alternative to Vercel, Heroku & Netlify that lets you easily deploy static sites, databases, full-stack applications and 360+ one-click services on your own servers.

82.9 KB
Raw Blame
<?php

namespace App\Livewire\Project\Application;

use App\Actions\Shared\CheckDomainDns;
use App\Jobs\CheckDomainDnsJob;
use App\Livewire\Concerns\InteractsWithCloudflareDomainConnect;
use App\Livewire\Concerns\InteractsWithDnsProviders;
use App\Livewire\Project\Shared\ConfigurationChecker;
use App\Models\Application;
use App\Models\Server;
use App\Support\DomainPortOverrides;
use App\Support\DomainUrlParts;
use App\Support\ValidationPatterns;
use App\Traits\AuditsApplicationSettings;
use App\Traits\ListensToTeamChannel;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\DB;
use Livewire\Attributes\Locked;
use Livewire\Component;

class Domains extends Component
{
    use AuditsApplicationSettings;
    use AuthorizesRequests;
    use InteractsWithCloudflareDomainConnect;
    use InteractsWithDnsProviders;
    use ListensToTeamChannel;

    protected bool $notifyRedirectUpdate = true;

    public Application $application;

    public string $redirect = 'both';

    public bool $isForceHttpsEnabled = true;

    public bool $isPermanentRedirectEnabled = false;

    /**
     * Per compose-service www/non-www redirect direction.
     * Keys are wire-safe (dots encoded) — use serviceRedirectWireKey().
     *
     * @var array<string, string>
     */
    public array $serviceRedirects = [];

    /** Compose service name when a pending domain conflict belongs to setServiceRedirect. */
    public ?string $pendingRedirectService = null;

    public string $newDomain = '';

    public array $newDomainParts = ['scheme' => 'https', 'host' => '', 'port' => '', 'path' => ''];

    public bool $newDomainPartsChanged = false;

    public ?string $newDomainService = null;

    public ?int $editingIndex = null;

    public string $editingDomain = '';

    public array $editingDomainParts = ['scheme' => 'https', 'host' => '', 'port' => '', 'path' => ''];

    public bool $editingDomainPartsChanged = false;

    public ?string $editingService = null;

    public string $editingIndexing = 'index';

    public string $editingRedirect = 'both';

    public string $editingOriginalRedirect = 'both';

    public bool $editingDomainWasRegenerated = false;

    public ?string $editingGeneratedHost = null;

    /** The internal port the edited domain uses when its Port field is empty. */
    public ?int $editingInheritedPort = null;

    /** @var array<int, array{url: string, service: ?string, dns_status: string, dns_message: string, expected_ip: ?string, checked_at?: ?string, is_suggested?: bool, suggested_for?: ?string, suggestion_label?: ?string, needs_force_add?: bool, internal_port?: ?int, has_port_override?: bool, inherited_port?: ?int}> */
    #[Locked]
    public array $domainRows = [];

    /** When set, the next addSuggestedDomain call for this index skips the DNS block. */
    #[Locked]
    public ?int $forceAddSuggestedIndex = null;

    /** @var array<int, string> */
    public array $composeServices = [];

    public array $domainConflicts = [];

    public bool $showDomainConflictModal = false;

    public bool $showPortWarningModal = false;

    public bool $forceUseUnknownPort = false;

    public ?int $unrecognizedPort = null;

    public ?string $pendingPortAction = null;

    public bool $forceSaveDomains = false;

    public bool $forceSaveDns = false;

    public bool $addDomainDnsFailed = false;

    public string $addDomainDnsMessage = '';

    public bool $showEditDomainModal = false;

    public bool $forceSaveEditDns = false;

    public bool $editDomainDnsFailed = false;

    public string $editDomainDnsMessage = '';

    /** Pending save path after conflict confirmation: add | update | suggested | redirect */
    public ?string $pendingAction = null;

    public bool $isCompose = false;

    public bool $labelsAreWritable = false;

    public bool $isCheckingDns = false;

    #[Locked]
    public bool $dnsValidationEnabled = true;

    /** Resolved or literal IP users should point DNS at. */
    #[Locked]
    public ?string $serverIp = null;

    /** Raw server IP/hostname as configured (may be a hostname). */
    #[Locked]
    public ?string $serverIpConfigured = null;

    protected $listeners = [
        'configurationChanged' => 'refreshDomains',
        'confirmDomainUsage',
    ];

    public function getListeners(): array
    {
        return array_merge($this->listeners, $this->teamChannelListeners(['DnsRecordConfigurationFinished' => 'dnsRecordConfigurationFinished']));
    }

    protected function rules(): array
    {
        return [
            'newDomain' => ValidationPatterns::applicationDomainRules(),
            'editingDomain' => ValidationPatterns::applicationDomainRules(),
            'editingIndexing' => 'string|required|in:index,noindex',
            'editingRedirect' => 'string|required|in:both,www,non-www',
            'redirect' => 'string|required|in:both,www,non-www',
            'isForceHttpsEnabled' => 'boolean',
            'isPermanentRedirectEnabled' => 'boolean',
            'serviceRedirects' => 'array',
            'serviceRedirects.*' => 'string|in:both,www,non-www',
        ];
    }

    protected function messages(): array
    {
        return array_merge(
            ValidationPatterns::combinedMessages(),
            [
                'redirect.required' => 'The Redirect setting is required.',
                'redirect.string' => 'The Redirect setting must be a string.',
                'redirect.in' => 'The Redirect setting must be both, www, or non-www.',
                'serviceRedirects.*.in' => 'The Redirect setting must be both, www, or non-www.',
            ]
        );
    }

    public function mount(): void
    {
        $this->authorize('view', $this->application);
        $this->loadDomainState();
    }

    public function refreshDomains(bool $parseCompose = true): void
    {
        $editingRow = $this->editingIndex !== null ? ($this->domainRows[$this->editingIndex] ?? null) : null;

        $this->loadDomainState($parseCompose);

        if ($editingRow !== null) {
            $index = collect($this->domainRows)->search(fn (array $row): bool => $row['url'] === $editingRow['url']
                && ($row['service'] ?? null) === ($editingRow['service'] ?? null));
            $this->editingIndex = $index === false ? null : (int) $index;
        }
    }

    public function pollDnsChecks(): void
    {
        $this->authorize('view', $this->application);

        $checkingRows = collect($this->domainRows)
            ->where('dns_status', 'checking')
            ->values();

        // The poll only refreshes DNS results. Parsing the compose file here would
        // queue file sync jobs on every tick and starve the DNS check jobs.
        $this->refreshDomains(parseCompose: false);

        foreach ($checkingRows as $checkingRow) {
            $row = collect($this->domainRows)->first(fn (array $row): bool => $row['url'] === $checkingRow['url']
                && ($row['service'] ?? null) === ($checkingRow['service'] ?? null));

            if (! is_array($row) || $row['dns_status'] === 'checking') {
                continue;
            }

            $this->dispatchDnsCheckNotification($row['url'], $row['dns_status']);
        }
    }

    protected function dispatchDnsCheckNotification(string $url, string $status): void
    {
        $host = parse_url($url, PHP_URL_HOST) ?: $url;

        match ($status) {
            'ok' => $this->dispatch('success', "DNS is configured correctly for {$host}."),
            'failed' => $this->dispatch('error', "DNS is not configured for {$host}. Review the required DNS record. If you changed it recently, DNS propagation can take some time, so please try again later."),
            default => $this->dispatch('info', "DNS check skipped for {$host}."),
        };
    }

    public function toggleNoindexDomain(string $domain, string|bool $indexing): void
    {
        $this->authorize('update', $this->application);

        $noindex = $indexing === true || $indexing === 'noindex';
        $domains = $this->application->noindexDomains();
        $domains = $noindex ? $domains->push($domain) : $domains->reject(fn (string $item) => $item === $domain);

        $this->application->setNoindexDomains($domains);
        $this->application->save();
        $this->application->refresh();
        $this->resetDefaultLabels();
        $this->dispatch('configurationChanged')->to(ConfigurationChecker::class);
        $this->dispatch('success', 'Search engine indexing updated.');
    }

    public function updateRedirect(string $redirect): void
    {
        $this->redirect = $redirect;
        $this->setRedirect();
    }

    public function updateForceHttps(): void
    {
        $this->authorize('update', $this->application);
        $this->validateOnly('isForceHttpsEnabled');

        $this->application->settings->is_force_https_enabled = $this->isForceHttpsEnabled;
        $this->saveApplicationSettingsWithAudit($this->application);
        $this->resetDefaultLabels();
        $this->dispatch('configurationChanged')->to(ConfigurationChecker::class);
        $this->dispatch('success', 'HTTP to HTTPS redirect updated.');
    }

    public function updatePermanentRedirect(): void
    {
        $this->authorize('update', $this->application);
        $this->validateOnly('isPermanentRedirectEnabled');

        $this->application->settings->is_permanent_redirect_enabled = $this->isPermanentRedirectEnabled;
        $this->saveApplicationSettingsWithAudit($this->application);
        $this->resetDefaultLabels();
        $this->dispatch('configurationChanged')->to(ConfigurationChecker::class);
        $this->dispatch('success', 'Redirect type updated.');
    }

    public function loadDomainState(bool $parseCompose = true): void
    {
        $this->application->refresh();
        $this->application->loadMissing(['destination.server', 'settings', 'additional_servers']);

        $this->isCompose = $this->application->build_pack === 'dockercompose';
        $this->labelsAreWritable = $this->application->settings->is_container_label_readonly_enabled === false;
        if ($this->pendingAction !== 'redirect' || $this->isCompose) {
            $this->redirect = $this->application->redirect ?? 'both';
        }
        $this->isForceHttpsEnabled = $this->application->isForceHttpsEnabled();
        $this->isPermanentRedirectEnabled = $this->application->isPermanentRedirectEnabled();

        $settings = instanceSettings();
        $this->dnsValidationEnabled = (bool) data_get($settings, 'is_dns_validation_enabled', true);

        $server = $this->application->destination?->server;
        if ($server) {
            if ($server->id === 0) {
                $configured = data_get($settings, 'public_ipv4')
                    ?: data_get($settings, 'public_ipv6')
                    ?: $server->ip;
            } else {
                $configured = $server->ip;
            }

            $resolved = resolveServerIpAddress(is_string($configured) ? $configured : null);
            $this->serverIpConfigured = $resolved['configured'];
            // Prefer real IP so users know what DNS record to set even when the
            // server address is stored as a hostname (e.g. coolify-testing-host).
            $this->serverIp = $resolved['ip'] ?? $resolved['configured'];
        } else {
            $this->serverIp = null;
            $this->serverIpConfigured = null;
        }

        $pendingRedirect = $this->pendingRedirectService !== null
            ? ($this->serviceRedirects[$this->serviceRedirectWireKey($this->pendingRedirectService)] ?? null)
            : null;
        $this->serviceRedirects = [];
        if (! $this->isCompose) {
            $this->composeServices = [];
        } elseif ($parseCompose) {
            $this->composeServices = [];
            foreach ($this->application->composeServicesWithoutParse() as $serviceName => $service) {
                if (! isDatabaseImage(data_get($service, 'image'))) {
                    $this->composeServices[] = (string) $serviceName;
                }
            }
        }
        if ($this->isCompose) {
            if ($this->newDomainService === null && count($this->composeServices) > 0) {
                $this->newDomainService = $this->composeServices[0];
            }

            $domains = $this->application->docker_compose_domains
                ? json_decode($this->application->docker_compose_domains, true)
                : [];
            if (! is_array($domains)) {
                $domains = [];
            }

            $serviceNames = $this->composeServices;
            foreach (array_keys($domains) as $serviceName) {
                if (! in_array($serviceName, $serviceNames, true)) {
                    $serviceNames[] = $serviceName;
                }
            }

            foreach ($serviceNames as $serviceName) {
                // data_get treats dots as path separators; read the service entry by array key.
                $serviceEntry = $domains[$serviceName] ?? null;
                $storedRedirect = is_array($serviceEntry) ? ($serviceEntry['redirect'] ?? null) : null;
                $this->serviceRedirects[$this->serviceRedirectWireKey($serviceName)] = $this->normalizeRedirect(
                    $this->pendingAction === 'redirect' && $serviceName === $this->pendingRedirectService
                        ? $pendingRedirect
                        : (is_string($storedRedirect) ? $storedRedirect : null)
                );
            }
        }

        // Do not auto-promote www/non-www pairs here: load/refresh also runs after
        // removeDomain, and re-adding counterparts would undo intentional deletes.
        // Pairs are still ensured on setRedirect, addDomain, and generateDomain.

        $this->domainRows = $this->buildDomainRows();
    }

    /**
     * Livewire wire:model treats dots as nesting (serviceRedirects.api.test).
     * Encode them so compose service names like "api.test" stay flat string values.
     */
    public function serviceRedirectWireKey(string $serviceName): string
    {
        return str_replace('.', '__dot__', $serviceName);
    }

    protected function normalizeRedirect(?string $redirect): string
    {
        return in_array($redirect, ['www', 'non-www', 'both'], true) ? $redirect : 'both';
    }

    protected function serviceRedirectFor(?string $serviceName): string
    {
        if ($serviceName === null) {
            return $this->normalizeRedirect($this->redirect);
        }

        return $this->normalizeRedirect(
            $this->serviceRedirects[$this->serviceRedirectWireKey($serviceName)] ?? null
        );
    }

    /**
     * @return array<int, array{url: string, service: ?string, dns_status: string, dns_message: string, expected_ip: ?string, checked_at: ?string, is_suggested: bool, suggested_for: ?string, suggestion_label: ?string, needs_force_add: bool}>
     */
    protected function buildDomainRows(): array
    {
        $rows = [];
        $stored = $this->storedDomainDnsStatuses();

        if ($this->isCompose) {
            $domains = $this->application->docker_compose_domains
                ? json_decode($this->application->docker_compose_domains, true)
                : [];

            if (! is_array($domains)) {
                $domains = [];
            }

            $serviceNames = $this->composeServices;
            foreach (array_keys($domains) as $serviceName) {
                if (! in_array($serviceName, $serviceNames, true)) {
                    $serviceNames[] = $serviceName;
                }
            }

            foreach ($serviceNames as $serviceName) {
                $configured = [];
                // Array key access: data_get() would treat dots in service names as path separators.
                $domainString = is_array($domains[$serviceName] ?? null)
                    ? ($domains[$serviceName]['domain'] ?? null)
                    : null;
                foreach ($this->splitDomains(is_string($domainString) ? $domainString : null) as $url) {
                    $row = $this->domainRowFromStored($url, $serviceName, $stored);
                    $rows[] = $row;
                    $configured[] = $row;
                }

            }

            return $this->sortDomainRowsByDnsStatus($rows);
        }

        foreach ($this->splitDomains($this->application->fqdn) as $url) {
            $rows[] = $this->domainRowFromStored($url, null, $stored);
        }

        return $this->sortDomainRowsByDnsStatus($rows);
    }

    /**
     * @param  array<int, array<string, mixed>>  $rows
     * @return array<int, array<string, mixed>>
     */
    protected function sortDomainRowsByDnsStatus(array $rows): array
    {
        return collect($rows)
            ->sortBy(fn (array $row): int => ($row['dns_status'] ?? null) === 'failed' ? 0 : 1)
            ->values()
            ->all();
    }

    /**
     * Missing www/non-www counterparts as full domain rows (DNS-checkable, not yet saved).
     *
     * Messaging depends on Direction:
     * - both: both hosts are first-class and need DNS to the server
     * - www / non-www: the missing pair is for Coolify's redirect (still must resolve to
     *   the server so the proxy can redirect; not framed as a separate primary site)
     *
     * @param  array<int, array<string, mixed>>  $configuredRows
     * @param  array<string, array{status?: string, message?: string, expected_ip?: ?string, checked_at?: ?string}>  $stored
     * @return array<int, array{url: string, service: ?string, dns_status: string, dns_message: string, expected_ip: ?string, checked_at: ?string, is_suggested: bool, suggested_for: ?string, suggestion_label: ?string, needs_force_add: bool, suggestion_role: ?string}>
     */
    protected function buildSuggestedWwwRows(array $configuredRows, array $stored, ?string $service = null): array
    {
        $knownHosts = [];
        foreach ($configuredRows as $row) {
            $host = $this->domainHost((string) ($row['url'] ?? ''));
            if ($host !== null) {
                $knownHosts[strtolower($host)] = true;
            }
        }

        $suggested = [];
        foreach ($configuredRows as $row) {
            $url = (string) ($row['url'] ?? '');
            $rowService = $service ?? ($row['service'] ?? null);
            $counterpart = $this->wwwCounterpartUrl($url);
            if ($counterpart === null) {
                continue;
            }

            $counterpartHost = $this->domainHost($counterpart);
            if ($counterpartHost === null) {
                continue;
            }

            $hostKey = strtolower($counterpartHost);
            if (isset($knownHosts[$hostKey])) {
                continue;
            }
            $knownHosts[$hostKey] = true;

            $base = $this->domainRowFromStored($counterpart, $rowService, $stored);
            $isWww = str_starts_with($hostKey, 'www.');
            $meta = $this->suggestedDomainMeta($isWww, $this->serviceRedirectFor(is_string($rowService) ? $rowService : null));

            $base['is_suggested'] = true;
            $base['suggested_for'] = $url;
            $base['suggestion_label'] = null;
            $base['suggestion_role'] = $meta['role'];
            $base['needs_force_add'] = false;
            $base['dns_message'] = $meta['pending_message'];

            $suggested[] = $base;
        }

        return $suggested;
    }

    /**
     * Labels/copy for a suggested www or non-www host based on Direction.
     *
     * @return array{label: string, role: string, pending_message: string, dns_suffix: string}
     */
    protected function suggestedDomainMeta(bool $suggestedIsWww, ?string $redirectOverride = null): array
    {
        $pendingMessage = 'Not configured yet.';
        $redirect = $redirectOverride ?? ($this->redirect ?: 'both');

        return match ($redirect) {
            'www' => $suggestedIsWww
                ? [
                    'label' => 'Not added · canonical www',
                    'role' => 'canonical',
                    'pending_message' => $pendingMessage,
                    'dns_suffix' => '',
                ]
                : [
                    'label' => 'Not added · redirect source',
                    'role' => 'redirect_source',
                    'pending_message' => $pendingMessage,
                    'dns_suffix' => '',
                ],
            'non-www' => $suggestedIsWww
                ? [
                    'label' => 'Not added · redirect source',
                    'role' => 'redirect_source',
                    'pending_message' => $pendingMessage,
                    'dns_suffix' => '',
                ]
                : [
                    'label' => 'Not added · canonical non-www',
                    'role' => 'canonical',
                    'pending_message' => $pendingMessage,
                    'dns_suffix' => '',
                ],
            default => [
                'label' => $suggestedIsWww ? 'Not added · www' : 'Not added · non-www',
                'role' => 'pair',
                'pending_message' => $pendingMessage,
                'dns_suffix' => '',
            ],
        };
    }

    /**
     * @param  array<string, array{status?: string, message?: string, expected_ip?: ?string, checked_at?: ?string}>  $stored
     * @return array{url: string, service: ?string, dns_status: string, dns_message: string, expected_ip: ?string, checked_at: ?string, is_suggested: bool, suggested_for: ?string, suggestion_label: ?string, needs_force_add: bool, internal_port: ?int, has_port_override: bool, inherited_port?: ?int}
     */
    protected function domainRowFromStored(string $url, ?string $service, array $stored): array
    {
        $key = $this->domainDnsStatusKey($url, $service);
        $entry = $stored[$key] ?? null;
        $port = $this->effectiveDomainInternalPort($url, $service);

        $row = [
            'url' => $url,
            'service' => $service,
            'internal_port' => $port['internal_port'],
            'has_port_override' => $port['has_port_override'],
            'inherited_port' => $this->inheritedDomainInternalPort($service),
            'dns_status' => 'pending',
            'dns_message' => 'Not checked yet.',
            'expected_ip' => $this->serverIp,
            'checked_at' => null,
            'check_id' => null,
            'is_suggested' => false,
            'suggested_for' => null,
            'suggestion_label' => null,
            'needs_force_add' => false,
        ];

        if (is_array($entry) && filled(data_get($entry, 'status'))) {
            $row['dns_status'] = (string) data_get($entry, 'status', 'pending');
            $row['dns_message'] = (string) data_get($entry, 'message', 'Not checked yet.');
            $row['expected_ip'] = data_get($entry, 'expected_ip') ?: $this->serverIp;
            $row['checked_at'] = data_get($entry, 'checked_at');
            $row['check_id'] = data_get($entry, 'check_id');
        }

        return $row;
    }

    /**
     * @return array{internal_port: ?int, has_port_override: bool}
     */
    protected function effectiveDomainInternalPort(string $url, ?string $service = null): array
    {
        $canonical = DomainPortOverrides::withoutPort($url);
        $overrides = $this->application->domain_port_overrides ?? [];
        $legacyPortPart = DomainUrlParts::split($url)['port'] ?? '';
        $legacyPort = $legacyPortPart !== '' ? (int) $legacyPortPart : null;
        $hasMapEntry = array_key_exists($canonical, $overrides);

        if ($hasMapEntry) {
            return [
                'internal_port' => (int) $overrides[$canonical],
                'has_port_override' => true,
            ];
        }

        if ($legacyPort !== null) {
            return [
                'internal_port' => $legacyPort,
                'has_port_override' => true,
            ];
        }

        return [
            'internal_port' => $this->inheritedDomainInternalPort($service),
            'has_port_override' => false,
        ];
    }

    /**
     * The internal port a domain uses when it has no per-domain override.
     */
    protected function inheritedDomainInternalPort(?string $service = null): ?int
    {
        $composePort = dockerComposeServicePort($this->application->docker_compose_raw, $service);
        if ($composePort !== null) {
            return $composePort;
        }

        if ($this->isCompose && $service !== null) {
            return null;
        }

        if ($this->application->settings?->is_static) {
            return 80;
        }

        $exposed = $this->application->ports_exposes_array;

        return isset($exposed[0]) && is_numeric($exposed[0]) && (int) $exposed[0] > 0
            ? (int) $exposed[0]
            : null;
    }

    /**
     * @param  array{scheme: string, host: string, port: string, path: string}  $parts
     */
    protected function portFromParts(array $parts): ?int
    {
        $port = trim((string) ($parts['port'] ?? ''));
        if ($port === '' || ! ctype_digit($port) || (int) $port <= 0) {
            return null;
        }

        return (int) $port;
    }

    protected function currentRowPort(string $url): ?int
    {
        $canonical = DomainPortOverrides::withoutPort($url);
        $override = ($this->application->domain_port_overrides ?? [])[$canonical] ?? null;
        if (filled($override) && (int) $override > 0) {
            return (int) $override;
        }

        $legacy = DomainUrlParts::split($url)['port'] ?? '';

        return $legacy !== '' && ctype_digit($legacy) ? (int) $legacy : null;
    }

    protected function shouldConfirmPort(?int $port, ?int $currentPort = null, ?string $serviceName = null): bool
    {
        if ($this->forceUseUnknownPort || $port === null) {
            return false;
        }
        if ($currentPort !== null && $port === $currentPort) {
            return false;
        }

        return $this->application->portRequiresConfirmation($port, $serviceName);
    }

    protected function openPortWarning(?int $port, string $action): void
    {
        $this->unrecognizedPort = $port;
        $this->pendingPortAction = $action;
        $this->showPortWarningModal = true;
    }

    /**
     * @return array<string, array{status: string, message: string, expected_ip: ?string, checked_at: ?string}>
     */
    protected function storedDomainDnsStatuses(): array
    {
        $stored = $this->application->domain_dns_statuses;

        return is_array($stored) ? $stored : [];
    }

    protected function domainDnsStatusKey(string $url, ?string $service = null): string
    {
        return $service ? "{$service}|{$url}" : $url;
    }

    /**
     * @return array<int, string>
     */
    protected function splitDomains(?string $value): array
    {
        if (blank($value)) {
            return [];
        }

        return collect(explode(',', $value))
            ->map(fn ($domain) => trim((string) $domain))
            ->filter()
            ->values()
            ->all();
    }

    protected function authorizeUpdateForDomainConnect(): void
    {
        $this->authorize('update', $this->application);
    }

    protected function usesInstanceNetworkAddressesForDnsHints(): bool
    {
        return $this->application->destination?->server?->id === 0;
    }

    public function checkAllDns(): void
    {
        $this->authorize('update', $this->application);

        foreach ($this->domainRows as $row) {
            $this->queueUrlsDns([$row['url']], $row['service'] ?? null);
        }
    }

    public function checkDomainDns(int $index): void
    {
        $this->authorize('update', $this->application);

        if (! isset($this->domainRows[$index])) {
            return;
        }

        $row = $this->domainRows[$index];
        $this->queueUrlsDns([$row['url']], $row['service'] ?? null);
    }

    protected function applyDnsStatus(int $index, Server $server): void
    {
        $this->applyDnsStatuses([$index], $server);
    }

    /**
     * @param  array<int, int>  $indexes
     */
    protected function applyDnsStatuses(array $indexes, Server $server): void
    {
        $entries = [];

        foreach ($indexes as $index) {
            $entries[(string) $index] = $this->domainRows[$index]['url'];
        }

        $results = CheckDomainDns::run($entries, $server, $this->serverIp);

        foreach ($results as $index => $result) {
            $index = (int) $index;
            $this->domainRows[$index]['dns_status'] = $result['status'];
            $this->domainRows[$index]['dns_message'] = $result['message'];

            // Keep suggested-row copy short after DNS checks (no role badge).
            if ($this->domainRows[$index]['is_suggested'] ?? false) {
                $isWww = str_starts_with(strtolower((string) $this->domainHost((string) $this->domainRows[$index]['url'])), 'www.');
                $serviceName = $this->domainRows[$index]['service'] ?? null;
                $meta = $this->suggestedDomainMeta(
                    $isWww,
                    $this->serviceRedirectFor(is_string($serviceName) ? $serviceName : null)
                );
                $this->domainRows[$index]['dns_message'] = $meta['pending_message'];
                $this->domainRows[$index]['suggestion_label'] = null;
                $this->domainRows[$index]['suggestion_role'] = $meta['role'];
            }

            $this->domainRows[$index]['expected_ip'] = $result['expected_ip'];
            $this->domainRows[$index]['checked_at'] = $result['checked_at'];
            $this->domainRows[$index]['check_id'] = null;
        }
    }

    /**
     * Persist current domain row DNS results and drop statuses for removed domains.
     *
     * @param  array<int, string>  $startingCheckKeys  Status keys whose check is being started by this call.
     *                                                 They are allowed to replace a stored completed result.
     */
    protected function persistDomainDnsStatuses(array $startingCheckKeys = []): void
    {
        $statuses = [];

        foreach ($this->domainRows as $row) {
            $url = $row['url'] ?? null;
            if (! is_string($url) || $url === '') {
                continue;
            }

            $status = $row['dns_status'] ?? 'pending';
            if ($status === 'pending') {
                // Keep previously stored value for unchecked domains if present.
                $key = $this->domainDnsStatusKey($url, $row['service'] ?? null);
                $existing = data_get($this->storedDomainDnsStatuses(), $key);
                if (is_array($existing) && filled(data_get($existing, 'status'))) {
                    $statuses[$key] = $existing;
                }

                continue;
            }

            $key = $this->domainDnsStatusKey($url, $row['service'] ?? null);
            $statuses[$key] = [
                'status' => $status,
                'message' => (string) ($row['dns_message'] ?? ''),
                'expected_ip' => $row['expected_ip'] ?? $this->serverIp,
                'checked_at' => $row['checked_at'] ?? now()->toIso8601String(),
                'check_id' => $row['check_id'] ?? null,
            ];
        }

        DB::transaction(function () use (&$statuses, $startingCheckKeys): void {
            $application = Application::query()->lockForUpdate()->findOrFail($this->application->id);
            $storedStatuses = $application->domain_dns_statuses ?? [];

            foreach ($statuses as $key => $status) {
                if (in_array($key, $startingCheckKeys, true)) {
                    continue;
                }

                $localCheckId = $status['check_id'] ?? null;
                $storedCheckId = $storedStatuses[$key]['check_id'] ?? null;

                if ($storedCheckId !== null && $localCheckId !== $storedCheckId) {
                    $statuses[$key] = $storedStatuses[$key];

                    continue;
                }

                if ($status['status'] === 'checking' && isset($storedStatuses[$key]) && $storedStatuses[$key]['status'] !== 'checking') {
                    $statuses[$key] = $storedStatuses[$key];
                }
            }

            $application->domain_dns_statuses = $statuses === [] ? null : $statuses;
            $application->save();
        });

        $this->application->domain_dns_statuses = $statuses === [] ? null : $statuses;
    }

    protected function pruneDomainDnsStatusesToCurrentDomains(): void
    {
        $this->application->refresh();
        // Rebuild rows from current domain list, keep only matching stored keys.
        $this->domainRows = $this->buildDomainRows();
        $this->persistDomainDnsStatuses();
    }

    /**
     * Human-readable DNS target: real IP, with configured hostname when it differs.
     */
    public function dnsTargetLabel(): ?string
    {
        if (blank($this->serverIp)) {
            return $this->serverIpConfigured;
        }

        if (
            filled($this->serverIpConfigured)
            && $this->serverIpConfigured !== $this->serverIp
            && filter_var($this->serverIpConfigured, FILTER_VALIDATE_IP) === false
        ) {
            return "{$this->serverIp} ({$this->serverIpConfigured})";
        }

        return $this->serverIp;
    }

    public function updatedNewDomain(): void
    {
        $this->resetAddDomainDnsGate();
    }

    public function updatedNewDomainParts(): void
    {
        $this->newDomainPartsChanged = true;
        $this->resetAddDomainDnsGate();
    }

    public function updatedNewDomainService(): void
    {
        $this->resetAddDomainDnsGate();
    }

    public function resetAddDomainDnsGate(): void
    {
        $this->addDomainDnsFailed = false;
        $this->addDomainDnsMessage = '';
        $this->forceSaveDns = false;
    }

    public function resetAddDomainForm(): void
    {
        $this->newDomain = '';
        $this->newDomainParts = DomainUrlParts::empty();
        $this->newDomainPartsChanged = false;
        $this->resetAddDomainDnsGate();
        $this->resetErrorBag('newDomain');
    }

    public function confirmAddDomainDespiteDns(): void
    {
        $this->forceSaveDns = true;
        $this->addDomain();
    }

    public function confirmDomainUsage(): void
    {
        $this->forceSaveDomains = true;
        $this->showDomainConflictModal = false;

        if ($this->pendingAction === 'update') {
            $this->updateDomain();

            return;
        }

        if ($this->pendingAction === 'suggested') {
            $this->addSuggestedDomain((int) ($this->editingIndex ?? -1));

            return;
        }

        if ($this->pendingAction === 'redirect') {
            if ($this->isCompose && filled($this->pendingRedirectService)) {
                $this->setServiceRedirect($this->pendingRedirectService);

                return;
            }

            $this->setRedirect();

            return;
        }

        $this->addDomain();
    }

    public function confirmUseUnknownPort(): void
    {
        $this->authorize('update', $this->application);
        $this->forceUseUnknownPort = true;
        $this->showPortWarningModal = false;
        $action = $this->pendingPortAction;
        $this->pendingPortAction = null;

        if ($action === 'update') {
            $this->updateDomain();

            return;
        }

        $this->addDomain();
    }

    public function cancelUseUnknownPort(): void
    {
        $this->showPortWarningModal = false;
        $this->forceUseUnknownPort = false;
        $this->unrecognizedPort = null;
        $this->pendingPortAction = null;
    }

    /**
     * Clear pending conflict state when the modal is dismissed without confirmation.
     * confirmDomainUsage sets forceSaveDomains before closing the modal.
     */
    public function updatedShowDomainConflictModal(bool $value): void
    {
        if ($value || $this->forceSaveDomains) {
            return;
        }

        $this->authorize('update', $this->application);
        $wasRedirect = $this->pendingAction === 'redirect';
        $this->pendingAction = null;
        $this->pendingRedirectService = null;
        if ($wasRedirect) {
            $this->refreshDomains();
        }
    }

    public function addDomain(): void
    {
        try {
            $this->authorize('update', $this->application);

            if ($this->labelsAreWritable) {
                $this->dispatch('error', 'Domains cannot be edited while container labels are writable. Set domains in the Labels section on General.');

                return;
            }

            if ($this->newDomainPartsChanged || filled($this->newDomainParts['host'] ?? null)) {
                $this->newDomain = DomainUrlParts::compose(...$this->newDomainParts);
            }
            $this->validateOnly('newDomain');

            $normalized = ValidationPatterns::normalizeApplicationDomains($this->newDomain);
            if (blank($normalized)) {
                $this->addError('newDomain', 'Please enter a valid domain URL.');

                return;
            }

            $newUrls = $this->splitDomains($normalized);
            $pairedUrls = collect($newUrls)
                ->map(fn (string $url) => $this->wwwCounterpartUrl($url))
                ->filter()
                ->values()
                ->all();
            $current = $this->currentDomainList($this->newDomainService);
            $currentCanonicalDomains = $current->map(
                fn (string $url): string => DomainPortOverrides::withoutPort($url)
            );

            foreach ($newUrls as $url) {
                if ($currentCanonicalDomains->contains(DomainPortOverrides::withoutPort($url))) {
                    $this->addError('newDomain', "Domain {$url} is already configured.");

                    return;
                }
            }

            if ($this->shouldConfirmPort($this->portFromParts($this->newDomainParts), serviceName: $this->newDomainService)) {
                $this->openPortWarning($this->portFromParts($this->newDomainParts), 'add');

                return;
            }

            $merged = $current->merge($newUrls)->merge($pairedUrls)->unique()->values();
            $this->pendingAction = 'add';
            if (! $this->saveDomainList($merged, $this->newDomainService)) {
                return;
            }

            $this->forceSaveDomains = false;
            $this->pendingAction = null;
            $this->forceUseUnknownPort = false;
            $serviceForCheck = $this->newDomainService;
            $this->resetAddDomainForm();
            $this->dispatch('close-modal');
            $this->refreshDomains();
            $addedUrls = array_values(array_unique(array_merge($newUrls, $pairedUrls)));
            if ($this->configureDnsAfterDomainAdd($addedUrls)) {
                $this->dispatch('success', 'Domain added.');

                return;
            }

            $dnsChecks = collect($this->dnsEntriesForUrls($addedUrls, $serviceForCheck))
                ->map(fn (string $url, string $statusKey) => [
                    'status_key' => $statusKey,
                    'url' => $url,
                    'check_id' => new_public_id(),
                ]);

            foreach ($dnsChecks as $dnsCheck) {
                $this->markUrlsAsChecking([$dnsCheck['url']], $serviceForCheck, $dnsCheck['check_id']);
            }
            $this->persistDomainDnsStatuses($dnsChecks->pluck('status_key')->all());

            $failedDnsChecks = 0;
            foreach ($dnsChecks as $dnsCheck) {
                try {
                    CheckDomainDnsJob::dispatch(
                        $this->application,
                        $dnsCheck['status_key'],
                        $dnsCheck['url'],
                        $this->application->destination?->server,
                        $this->serverIp,
                        $dnsCheck['check_id'],
                        $this->application->additional_servers->count() > 0,
                    );
                } catch (\Throwable) {
                    $failedDnsChecks++;
                    $this->markUrlsDnsCheckUnavailable([$dnsCheck['url']], $serviceForCheck, $dnsCheck['check_id']);
                }
            }

            if ($failedDnsChecks > 0) {
                $this->persistDomainDnsStatuses();
                $this->dispatch('error', 'Some DNS checks could not be started. Try again from the Domains page.');
            }

            $this->dispatch('success', $failedDnsChecks === $dnsChecks->count()
                ? 'Domain added.'
                : 'Domain added. DNS check started.');
        } catch (\Throwable $e) {
            handleError($e, $this);
        }
    }

    /**
     * @param  array<int, string>  $urls
     */
    protected function markUrlsAsChecking(array $urls, ?string $service = null, ?string $checkId = null): void
    {
        $indexesToCheck = [];

        foreach ($this->domainRows as $index => $row) {
            if (! in_array($row['url'], $urls, true)) {
                continue;
            }

            if ($service !== null && ($row['service'] ?? null) !== $service) {
                continue;
            }

            $this->domainRows[$index]['dns_status'] = 'checking';
            $this->domainRows[$index]['dns_message'] = 'Checking DNS...';
            $this->domainRows[$index]['check_id'] = $checkId;
        }
    }

    /**
     * @param  array<int, string>  $urls
     */
    protected function markUrlsDnsCheckUnavailable(array $urls, ?string $service = null, ?string $checkId = null): void
    {
        $this->markUrlsAsChecking($urls, $service, $checkId);

        foreach ($this->domainRows as $index => $row) {
            if (! in_array($row['url'], $urls, true)) {
                continue;
            }

            if ($service !== null && ($row['service'] ?? null) !== $service) {
                continue;
            }

            $this->domainRows[$index]['dns_status'] = 'skipped';
            $this->domainRows[$index]['dns_message'] = 'DNS check could not be started.';
            $this->domainRows[$index]['checked_at'] = now()->toIso8601String();
        }
    }

    /**
     * @param  array<int, string>  $urls
     * @return array<string, string>
     */
    protected function dnsEntriesForUrls(array $urls, ?string $service = null): array
    {
        $entries = [];

        foreach ($urls as $url) {
            $entries[$this->domainDnsStatusKey($url, $service)] = $url;
        }

        return $entries;
    }

    /**
     * Run a first-time DNS check for newly added/updated domain URLs and persist results.
     *
     * @param  array<int, string>  $urls
     */
    protected function checkUrlsDns(array $urls, ?string $service = null): void
    {
        if ($urls === []) {
            return;
        }

        $urlSet = array_fill_keys($urls, true);
        $server = $this->application->destination?->server;
        $skipDns = ! $this->dnsValidationEnabled
            || ! $server
            || $this->application->additional_servers->count() > 0;
        $indexesToCheck = [];

        foreach ($this->domainRows as $index => $row) {
            $url = $row['url'] ?? null;
            if (! is_string($url) || ! isset($urlSet[$url])) {
                continue;
            }

            if ($service !== null && ($row['service'] ?? null) !== $service) {
                continue;
            }

            if ($skipDns) {
                $reason = ! $this->dnsValidationEnabled
                    ? 'DNS validation is disabled in instance settings.'
                    : ($this->application->additional_servers->count() > 0
                        ? 'DNS check skipped for multi-server applications.'
                        : 'No server available for DNS validation.');

                $this->domainRows[$index]['dns_status'] = 'skipped';
                $this->domainRows[$index]['dns_message'] = $reason;
                $this->domainRows[$index]['checked_at'] = now()->toIso8601String();
                $this->domainRows[$index]['expected_ip'] = $this->serverIp;

                continue;
            }

            $indexesToCheck[] = $index;
        }

        if ($server && $indexesToCheck !== []) {
            $this->applyDnsStatuses($indexesToCheck, $server);
        }

        $this->persistDomainDnsStatuses();
    }

    /**
     * @param  array<int, string>  $urls
     */
    protected function queueUrlsDns(array $urls, ?string $service = null): void
    {
        foreach ($this->dnsEntriesForUrls($urls, $service) as $statusKey => $url) {
            $checkId = new_public_id();
            $this->markUrlsAsChecking([$url], $service, $checkId);
            $this->persistDomainDnsStatuses([$statusKey]);

            try {
                CheckDomainDnsJob::dispatch(
                    $this->application,
                    $statusKey,
                    $url,
                    $this->application->destination?->server,
                    $this->serverIp,
                    $checkId,
                    $this->application->additional_servers->count() > 0,
                );
            } catch (\Throwable) {
                $this->markUrlsDnsCheckUnavailable([$url], $service, $checkId);
                $this->persistDomainDnsStatuses();
                $this->dispatch('error', 'The DNS check could not be started. Try again from the Domains page.');
            }
        }
    }

    protected function shouldValidateDnsForAdd(): bool
    {
        if (! $this->dnsValidationEnabled) {
            return false;
        }

        $server = $this->application->destination?->server;
        if (! $server) {
            return false;
        }

        if ($this->application->additional_servers->count() > 0) {
            return false;
        }

        return true;
    }

    /**
     * @param  array<int, string>  $urls
     */
    protected function findDnsFailureMessage(array $urls): ?string
    {
        $server = $this->application->destination?->server;
        if (! $server) {
            return null;
        }

        $results = CheckDomainDns::run(array_combine($urls, $urls), $server, $this->serverIp);

        foreach ($results as $result) {
            if ($result['status'] === 'failed') {
                return $result['message'];
            }
        }

        return null;
    }

    public function updatedEditingDomain(): void
    {
        $this->resetEditDomainDnsGate();
    }

    public function updatedEditingDomainParts(): void
    {
        $this->editingDomainPartsChanged = true;
        $this->resetEditDomainDnsGate();
    }

    public function resetEditDomainDnsGate(): void
    {
        $this->editDomainDnsFailed = false;
        $this->editDomainDnsMessage = '';
        $this->forceSaveEditDns = false;
    }

    public function startEdit(int $index): void
    {
        if (! isset($this->domainRows[$index]) || ($this->domainRows[$index]['is_suggested'] ?? false)) {
            return;
        }

        $this->editingIndex = $index;
        $this->editingDomain = $this->domainRows[$index]['url'];
        $this->editingDomainParts = DomainUrlParts::split($this->editingDomain);
        $canonical = DomainPortOverrides::withoutPort($this->editingDomain);
        $savedPort = ($this->application->domain_port_overrides ?? [])[$canonical] ?? null;
        if (filled($savedPort)) {
            $this->editingDomainParts['port'] = (string) $savedPort;
        }
        $this->editingDomainPartsChanged = false;
        $this->editingService = $this->domainRows[$index]['service'];
        $this->editingIndexing = $this->application->isDomainNoindexed($this->editingDomain) ? 'noindex' : 'index';
        $this->editingRedirect = $this->serviceRedirectFor($this->editingService);
        $this->editingOriginalRedirect = $this->editingRedirect;
        $this->editingDomainWasRegenerated = false;
        $this->editingGeneratedHost = null;
        $this->editingInheritedPort = $this->domainRows[$index]['inherited_port'] ?? null;
        $this->resetEditDomainDnsGate();
        $this->resetErrorBag('editingDomain');
        $this->showEditDomainModal = true;
        $this->dispatch('open-edit-domain');
    }

    public function addSuggestedDomain(int $index): void
    {
        try {
            $this->authorize('update', $this->application);

            if ($this->labelsAreWritable) {
                $this->dispatch('error', 'Domains cannot be edited while container labels are writable.');

                return;
            }

            if (! isset($this->domainRows[$index]) || ! ($this->domainRows[$index]['is_suggested'] ?? false)) {
                return;
            }

            $url = (string) $this->domainRows[$index]['url'];
            $normalized = ValidationPatterns::normalizeApplicationDomains($url);
            if (blank($normalized)) {
                $this->dispatch('error', 'Invalid suggested domain.');

                return;
            }

            $serviceName = $this->domainRows[$index]['service'] ?? null;
            $newUrls = $this->splitDomains($normalized);
            $current = $this->currentDomainList($serviceName);
            foreach ($newUrls as $newUrl) {
                if ($current->contains($newUrl)) {
                    $this->refreshDomains();

                    return;
                }
            }

            $force = $this->forceAddSuggestedIndex === $index
                || (bool) ($this->domainRows[$index]['needs_force_add'] ?? false);

            if (! $force && $this->shouldValidateDnsForAdd()) {
                $dnsFailure = $this->findDnsFailureMessage($newUrls);
                if ($dnsFailure !== null) {
                    $this->domainRows[$index]['dns_status'] = 'failed';
                    $this->domainRows[$index]['dns_message'] = $dnsFailure;
                    $this->domainRows[$index]['checked_at'] = now()->toIso8601String();
                    $this->domainRows[$index]['needs_force_add'] = true;
                    $this->forceAddSuggestedIndex = $index;
                    $this->editingIndex = $index;
                    $this->persistDomainDnsStatuses();

                    return;
                }
            }

            $merged = $current->merge($newUrls)->unique()->values();
            $this->pendingAction = 'suggested';
            $this->editingIndex = $index;
            if (! $this->saveDomainList($merged, $serviceName)) {
                return;
            }

            $this->forceAddSuggestedIndex = null;
            $this->editingIndex = null;
            $this->pendingAction = null;
            $this->forceSaveDomains = false;
            $this->dispatch('success', 'Domain added.');
            $this->refreshDomains();
            $this->checkUrlsDns($newUrls, $serviceName);
        } catch (\Throwable $e) {
            handleError($e, $this);
        }
    }

    public function cancelEdit(): void
    {
        $this->showEditDomainModal = false;
        $this->editingIndex = null;
        $this->editingDomain = '';
        $this->editingDomainParts = DomainUrlParts::empty();
        $this->editingDomainPartsChanged = false;
        $this->editingService = null;
        $this->editingIndexing = 'index';
        $this->editingRedirect = 'both';
        $this->editingOriginalRedirect = 'both';
        $this->editingDomainWasRegenerated = false;
        $this->editingGeneratedHost = null;
        $this->editingInheritedPort = null;
        $this->resetEditDomainDnsGate();
        $this->resetErrorBag('editingDomain');
        if ($this->pendingAction === 'update') {
            $this->pendingAction = null;
            $this->forceSaveDomains = false;
            $this->showDomainConflictModal = false;
        }
    }

    public function confirmUpdateDomainDespiteDns(): void
    {
        $this->forceSaveEditDns = true;
        $this->updateDomain();
    }

    public function regenerateEditingDomain(): void
    {
        $this->authorize('update', $this->application);

        if ($this->labelsAreWritable || $this->editingIndex === null || ! isset($this->domainRows[$this->editingIndex])) {
            return;
        }

        $server = data_get($this->application, 'destination.server');
        if (! $server) {
            $this->dispatch('error', 'No server found for this application.');

            return;
        }

        $generatedHost = parse_url(generateUrl(server: $server, random: new_public_id()), PHP_URL_HOST);
        if (! is_string($generatedHost) || $generatedHost === '') {
            $this->dispatch('error', 'Could not generate a domain.');

            return;
        }

        $currentHost = (string) ($this->editingDomainParts['host'] ?? '');
        $this->editingGeneratedHost = $generatedHost;
        $this->editingDomainParts['host'] = str_starts_with(strtolower($currentHost), 'www.')
            ? 'www.'.$generatedHost
            : $generatedHost;
        $this->editingDomainPartsChanged = true;
        $this->editingDomainWasRegenerated = true;
        $this->resetEditDomainDnsGate();
        $this->resetErrorBag('editingDomain');
    }

    public function updateDomain(): void
    {
        try {
            $this->authorize('update', $this->application);

            if ($this->labelsAreWritable) {
                $this->dispatch('error', 'Domains cannot be edited while container labels are writable.');

                return;
            }

            if ($this->editingIndex === null || ! isset($this->domainRows[$this->editingIndex])) {
                return;
            }

            if ($this->editingDomainPartsChanged || filled($this->editingDomainParts['host'] ?? null)) {
                $this->editingDomain = DomainUrlParts::compose(...$this->editingDomainParts);
            }
            $this->validateOnly('editingDomain');
            $this->validateOnly('editingIndexing');
            $this->validateOnly('editingRedirect');

            $normalized = ValidationPatterns::normalizeApplicationDomains($this->editingDomain);
            if (blank($normalized) || count($this->splitDomains($normalized)) !== 1) {
                $this->addError('editingDomain', 'Please enter a single valid domain URL.');

                return;
            }

            $newUrl = $this->splitDomains($normalized)[0];
            $oldUrl = $this->domainRows[$this->editingIndex]['url'];
            $service = $this->editingService;
            if (blank(DomainUrlParts::split($newUrl)['port'] ?? null)) {
                $portOverrides = $this->application->domain_port_overrides ?? [];
                unset($portOverrides[DomainPortOverrides::withoutPort($oldUrl)]);
                unset($portOverrides[DomainPortOverrides::withoutPort($newUrl)]);
                $this->application->domain_port_overrides = $portOverrides ?: null;
            }

            $current = $this->currentDomainList($service);
            $otherCanonicalDomains = $current
                ->reject(fn (string $url): bool => $url === $oldUrl)
                ->map(fn (string $url): string => DomainPortOverrides::withoutPort($url));
            if ($otherCanonicalDomains->contains(DomainPortOverrides::withoutPort($newUrl))) {
                $this->addError('editingDomain', "Domain {$newUrl} is already configured.");

                return;
            }

            if ($this->shouldConfirmPort($this->portFromParts($this->editingDomainParts), $this->currentRowPort($oldUrl), $service)) {
                $this->openPortWarning($this->portFromParts($this->editingDomainParts), 'update');

                return;
            }

            $replacements = [$oldUrl => $newUrl];
            if ($this->editingDomainWasRegenerated && filled($this->editingGeneratedHost) && in_array($this->editingRedirect, ['www', 'non-www'], true)) {
                $oldCounterpartHost = parse_url((string) $this->wwwCounterpartUrl($oldUrl, true), PHP_URL_HOST);
                $oldCounterpart = $current->first(fn (string $url): bool => parse_url($url, PHP_URL_HOST) === $oldCounterpartHost);
                if (is_string($oldCounterpart)) {
                    $counterpartParts = DomainUrlParts::split($oldCounterpart);
                    $counterpartPort = $this->currentRowPort($oldCounterpart);
                    if ($counterpartPort !== null) {
                        $counterpartParts['port'] = (string) $counterpartPort;
                    }
                    $counterpartParts['host'] = str_starts_with(strtolower($counterpartParts['host']), 'www.')
                        ? 'www.'.$this->editingGeneratedHost
                        : $this->editingGeneratedHost;
                    $replacements[$oldCounterpart] = DomainUrlParts::compose(...$counterpartParts);
                }
            }

            $updated = $current->map(fn (string $url) => $replacements[$url] ?? $url)->unique()->values();
            if ($this->editingRedirect !== $this->editingOriginalRedirect && in_array($this->editingRedirect, ['www', 'non-www'], true)) {
                foreach ($updated->all() as $url) {
                    $counterpart = $this->wwwCounterpartUrl($url, true);
                    $counterpartHost = is_string($counterpart) ? parse_url($counterpart, PHP_URL_HOST) : null;
                    $hasCounterpart = filled($counterpartHost) && $updated->contains(
                        fn (string $candidate): bool => parse_url($candidate, PHP_URL_HOST) === $counterpartHost
                    );
                    if (filled($counterpart) && ! $hasCounterpart) {
                        $updated->push($counterpart);
                    }
                }
            }
            $urlsToCheck = $updated
                ->reject(fn (string $url): bool => $current->contains(
                    fn (string $existingUrl): bool => ! DomainUrlParts::hasDnsRelevantChange($existingUrl, $url)
                ))
                ->map(fn (string $url): string => DomainPortOverrides::withoutPort($url))
                ->unique()
                ->values()
                ->all();

            $noindexDomains = $this->application->noindexDomains();
            foreach ($replacements as $previousUrl => $replacementUrl) {
                $wasNoindexed = $previousUrl === $oldUrl
                    ? $this->editingIndexing === 'noindex'
                    : $this->application->isDomainNoindexed($previousUrl);
                $noindexDomains = $noindexDomains->reject(fn (string $domain): bool => $domain === $previousUrl);
                if ($wasNoindexed) {
                    $noindexDomains->push($replacementUrl);
                }
            }
            if ($this->isCompose) {
                $allDomains = json_decode($this->application->docker_compose_domains ?: '[]', true);
                $existing = is_array($allDomains[$service] ?? null) ? $allDomains[$service] : [];
                $allDomains[$service] = array_merge($existing, ['redirect' => $this->editingRedirect]);
                $this->application->docker_compose_domains = json_encode($allDomains);
            } else {
                $this->application->redirect = $this->editingRedirect;
            }

            $this->pendingAction = 'update';
            $previousDnsHostnames = $this->managedDnsHostnamesOf($this->application);
            if (! $this->saveDomainList($updated, $service, noindexDomains: $noindexDomains)) {
                return;
            }

            $this->releaseManagedDnsForEditedDomains($this->application, $previousDnsHostnames);
            $this->resetDefaultLabels();

            $this->forceSaveDomains = false;
            $this->pendingAction = null;
            $this->forceUseUnknownPort = false;
            $this->cancelEdit();
            $this->dispatch('edit-domain-saved');
            $this->dispatch('success', 'Domain updated.');
            $this->refreshDomains();
            if ($urlsToCheck !== []) {
                $this->queueUrlsDns($urlsToCheck, $service);
            }
        } catch (\Throwable $e) {
            handleError($e, $this);
        }
    }

    public function removeDomain(int $index, string $password = '', array $selectedActions = []): void
    {
        try {
            $this->authorize('update', $this->application);

            if ($this->labelsAreWritable) {
                $this->dispatch('error', 'Domains cannot be edited while container labels are writable.');

                return;
            }

            if (! isset($this->domainRows[$index]) || ($this->domainRows[$index]['is_suggested'] ?? false)) {
                return;
            }

            $url = $this->domainRows[$index]['url'];
            $service = $this->domainRows[$index]['service'];
            $updated = $this->currentDomainList($service)->reject(fn (string $item) => $item === $url)->values();

            if (! $this->saveDomainList($updated, $service, checkConflicts: false)) {
                return;
            }

            $this->releaseManagedDnsForUrl($url, $this->application, in_array('deleteManagedDns', $selectedActions, true));

            if ($this->editingIndex === $index) {
                $this->cancelEdit();
            }

            $this->dispatch('success', 'Domain removed.');
            $this->refreshDomains();
            $this->pruneDomainDnsStatusesToCurrentDomains();
        } catch (\Throwable $e) {
            handleError($e, $this);
        }
    }

    public function removeDomainByKey(string $domainKey, string $password = '', array $selectedActions = []): void
    {
        $index = collect($this->domainRows)->search(
            fn (array $row): bool => ! ($row['is_suggested'] ?? false)
                && hash_equals($domainKey, $this->domainRowKey($row))
        );

        if ($index === false) {
            return;
        }

        $this->removeDomain((int) $index, $password, $selectedActions);
    }

    /**
     * @param  array{url: string, service?: ?string}  $row
     */
    private function domainRowKey(array $row): string
    {
        return hash('sha256', $row['url'].'|'.($row['service'] ?? ''));
    }

    protected function dnsResourceForHostname(string $hostname): ?Model
    {
        return $this->application;
    }

    public function generateDomain(?string $serviceName = null): void
    {
        try {
            $this->authorize('update', $this->application);

            if ($this->labelsAreWritable) {
                $this->dispatch('error', 'Domains cannot be edited while container labels are writable.');

                return;
            }

            $server = data_get($this->application, 'destination.server');
            if (! $server) {
                $this->dispatch('error', 'No server found for this application.');

                return;
            }

            if ($this->isCompose) {
                $serviceName = $serviceName ?: $this->newDomainService ?: ($this->composeServices[0] ?? null);
                if (! $serviceName) {
                    $this->dispatch('error', 'No compose service available for domain generation.');

                    return;
                }

                $domain = generateUrl(server: $server, random: new_public_id());
                $current = $this->currentDomainList($serviceName);
                $merged = $current->push($domain)->unique()->values();

                if (! $this->saveDomainList($merged, $serviceName, checkConflicts: false)) {
                    return;
                }

                $pairedUrls = $this->syncRedirectDomainPairs($serviceName);
                $this->resetAddDomainForm();
                $this->dispatch('close-modal');
                $this->dispatch('success', 'Domain generated.');
                $this->refreshDomains();
                $this->checkUrlsDns(array_values(array_unique(array_merge([$domain], $pairedUrls))), $serviceName);

                return;
            }

            $fqdn = generateUrl(server: $server, random: $this->application->uuid);
            $merged = $this->currentDomainList()->push($fqdn)->unique()->values();
            if (! $this->saveDomainList($merged, null, checkConflicts: false)) {
                return;
            }

            $pairedUrls = $this->syncRedirectDomainPairs(null);
            $this->resetAddDomainForm();
            $this->dispatch('close-modal');
            $this->dispatch('success', 'Domain generated.');
            $this->refreshDomains();
            $this->checkUrlsDns(array_values(array_unique(array_merge([$fqdn], $pairedUrls))));
        } catch (\Throwable $e) {
            handleError($e, $this);
        }
    }

    public function setRedirect(): void
    {
        try {
            $this->authorize('update', $this->application);

            if ($this->isCompose) {
                $this->dispatch('error', 'Set the redirect direction per compose service.');

                return;
            }

            $this->validateOnly('redirect');

            $this->application->redirect = $this->redirect;

            // www / non-www redirects need both hosts configured as real domains so the
            // proxy can serve the canonical host and redirect the other. Auto-add missing
            // counterparts instead of leaving them as optional suggestions.
            $addedDomains = [];
            if (in_array($this->redirect, ['www', 'non-www'], true)) {
                $domainsBeforePairing = $this->currentDomainList();
                if (! $this->ensureWwwNonWwwPairsConfigured(null)) {
                    return;
                }

                $this->application->refresh();
                $this->application->redirect = $this->redirect;
                $addedDomains = $this->currentDomainList()->diff($domainsBeforePairing)->values()->all();
            }

            $domains = collect($this->application->fqdns);
            if (! $this->assertRedirectDomainsPresent($this->redirect, $domains)) {
                return;
            }

            $this->application->save();
            $this->pendingAction = null;
            $this->pendingRedirectService = null;
            $this->forceSaveDomains = false;
            $this->resetDefaultLabels();
            $this->dispatch('success', 'Redirect updated.');
            $this->refreshDomains();
            $this->queueUrlsDns($addedDomains);
            $this->pruneDomainDnsStatusesToCurrentDomains();
        } catch (\Throwable $e) {
            handleError($e, $this);
        }
    }

    public function updateServiceRedirect(string $serviceName, string $redirect): void
    {
        $this->serviceRedirects[$this->serviceRedirectWireKey($serviceName)] = $redirect;
        $this->setServiceRedirect($serviceName);
    }

    /**
     * @param  mixed  ...$modalArgs  Extra args from modal-confirmation (password, etc.)
     */
    public function setServiceRedirect(string $serviceName, mixed ...$modalArgs): void
    {
        try {
            $this->authorize('update', $this->application);

            if (! $this->isCompose) {
                $this->dispatch('error', 'Per-service redirect is only available for Docker Compose applications.');

                return;
            }

            // modal-confirmation passes string args with surrounding quotes intact.
            $serviceName = trim($serviceName, " \t\n\r\0\x0B'\"");

            if (blank($serviceName)) {
                $this->dispatch('error', 'A service is required.');

                return;
            }

            // Drop any nested arrays left from broken wire:model paths (e.g. service names with dots).
            $this->serviceRedirects = collect($this->serviceRedirects)
                ->filter(fn ($value) => is_string($value) || is_numeric($value))
                ->map(fn ($value) => $this->normalizeRedirect(is_string($value) ? $value : (string) $value))
                ->all();

            $wireKey = $this->serviceRedirectWireKey($serviceName);
            if (! array_key_exists($wireKey, $this->serviceRedirects)) {
                $this->serviceRedirects[$wireKey] = 'both';
            }

            $this->validateOnly("serviceRedirects.{$wireKey}");
            $redirect = $this->normalizeRedirect($this->serviceRedirects[$wireKey] ?? null);
            $this->serviceRedirects[$wireKey] = $redirect;
            $this->pendingRedirectService = $serviceName;

            // Promote the optional www/non-www suggestion to a real domain for redirects.
            $addedDomains = [];
            if (in_array($redirect, ['www', 'non-www'], true)) {
                $domainsBeforePairing = $this->currentDomainList($serviceName);
                if (! $this->ensureWwwNonWwwPairsConfigured($serviceName)) {
                    return;
                }
                // Ensure we re-read domains after pair save before writing redirect.
                $this->application->refresh();
                $addedDomains = $this->currentDomainList($serviceName)->diff($domainsBeforePairing)->values()->all();
            }

            $allDomains = $this->application->docker_compose_domains
                ? json_decode($this->application->docker_compose_domains, true)
                : [];
            if (! is_array($allDomains)) {
                $allDomains = [];
            }

            $existing = is_array($allDomains[$serviceName] ?? null) ? $allDomains[$serviceName] : [];
            $allDomains[$serviceName] = array_merge($existing, [
                'redirect' => $redirect,
            ]);

            // Keep domain key present when only redirect is set.
            if (! array_key_exists('domain', $allDomains[$serviceName])) {
                $allDomains[$serviceName]['domain'] = null;
            }

            $this->application->docker_compose_domains = json_encode($allDomains);
            $this->application->save();

            $domains = $this->currentDomainList($serviceName);
            if (! $this->assertRedirectDomainsPresent($redirect, $domains)) {
                return;
            }

            $this->pendingAction = null;
            $this->pendingRedirectService = null;
            $this->forceSaveDomains = false;
            $this->resetDefaultLabels();
            if ($this->notifyRedirectUpdate) {
                $this->dispatch('success', "Redirect updated for {$serviceName}.");
            }
            $this->refreshDomains();
            $this->queueUrlsDns($addedDomains, $serviceName);
            $this->pruneDomainDnsStatusesToCurrentDomains();
        } catch (\Throwable $e) {
            handleError($e, $this);
        }
    }

    /**
     * @param  Collection<int, string>  $domains
     */
    protected function assertRedirectDomainsPresent(string $redirect, Collection $domains): bool
    {
        if (! in_array($redirect, ['www', 'non-www'], true)) {
            return true;
        }

        $hasWww = $domains->filter(
            fn ($fqdn) => str_starts_with(strtolower((string) $this->domainHost((string) $fqdn)), 'www.')
        )->count();
        $hasNonWww = $domains->filter(
            function ($fqdn) {
                $host = strtolower((string) $this->domainHost((string) $fqdn));

                return $host !== '' && ! str_starts_with($host, 'www.');
            }
        )->count();

        $dnsHint = dnsMismatchGuidanceMessage(
            $this->dnsTargetLabel() ?? $this->serverIp,
            $this->serverIp,
        );

        // Redirects need both hosts: canonical target + source the proxy redirects from.
        if ($hasWww === 0 || $hasNonWww === 0) {
            $missing = $hasWww === 0 ? 'www' : 'non-www';
            $this->dispatch(
                'error',
                "Redirect requires both www and non-www domains, but the {$missing} host could not be added automatically (e.g. only IP/sslip hosts).<br><br>Please add the {$missing} domain manually ({$dnsHint})."
            );

            return false;
        }

        return true;
    }

    /**
     * When saved redirect is www/non-www, ensure missing counterparts exist as real domains
     * (not suggestion rows the user must click Add domain for).
     *
     * @return array<int, string> newly added domain URLs
     */
    protected function syncRedirectDomainPairs(?string $serviceName = null): array
    {
        if ($this->labelsAreWritable) {
            return [];
        }

        $user = auth()->user();
        if ($user === null || ! $user->can('update', $this->application)) {
            return [];
        }

        if ($this->isCompose && $serviceName === null) {
            $added = [];
            $serviceNames = $this->composeServices;
            $domains = $this->application->docker_compose_domains
                ? json_decode($this->application->docker_compose_domains, true)
                : [];
            if (is_array($domains)) {
                foreach (array_keys($domains) as $name) {
                    if (! in_array($name, $serviceNames, true)) {
                        $serviceNames[] = $name;
                    }
                }
            }
            foreach ($serviceNames as $name) {
                $added = array_merge($added, $this->syncRedirectDomainPairs($name));
            }

            return array_values(array_unique($added));
        }

        $redirect = $this->savedRedirectForService($serviceName);
        if (! in_array($redirect, ['www', 'non-www'], true)) {
            return [];
        }

        $before = $this->currentDomainList($serviceName)->all();
        if (! $this->ensureWwwNonWwwPairsConfigured($serviceName)) {
            return [];
        }

        $this->application->refresh();
        $after = $this->currentDomainList($serviceName);

        return $after->reject(fn (string $url) => in_array($url, $before, true))->values()->all();
    }

    protected function savedRedirectForService(?string $serviceName): string
    {
        if ($this->isCompose && filled($serviceName)) {
            $domains = $this->application->docker_compose_domains
                ? json_decode($this->application->docker_compose_domains, true)
                : [];
            $entry = is_array($domains) ? ($domains[$serviceName] ?? null) : null;
            $stored = is_array($entry) ? ($entry['redirect'] ?? null) : null;

            return $this->normalizeRedirect(is_string($stored) ? $stored : null);
        }

        return $this->normalizeRedirect($this->application->redirect ?? null);
    }

    /**
     * Persist missing www/non-www counterparts as normal domains (not suggestions).
     *
     * @return bool false when save was blocked (e.g. domain conflict modal shown)
     */
    protected function ensureWwwNonWwwPairsConfigured(?string $serviceName = null): bool
    {
        $current = $this->currentDomainList($serviceName);
        $knownHosts = [];

        foreach ($current as $url) {
            $host = $this->domainHost($url);
            if ($host !== null) {
                $knownHosts[strtolower($host)] = true;
            }
        }

        $toAdd = collect();
        foreach ($current as $url) {
            // Include sslip/nip so compose/dev hosts can still get redirect pairs.
            $counterpart = $this->wwwCounterpartUrl($url, forRedirectPairing: true);
            if ($counterpart === null) {
                continue;
            }

            $counterpartHost = $this->domainHost($counterpart);
            if ($counterpartHost === null) {
                continue;
            }

            $hostKey = strtolower($counterpartHost);
            if (isset($knownHosts[$hostKey])) {
                continue;
            }

            $knownHosts[$hostKey] = true;
            $toAdd->push($counterpart);
        }

        if ($toAdd->isEmpty()) {
            return true;
        }

        $merged = $current->merge($toAdd)->unique()->values();
        $this->pendingAction = 'redirect';
        $this->pendingRedirectService = $serviceName;

        // Skip DNS: pairing for redirects must still be configured even when DNS is not ready.
        if (! $this->saveDomainList($merged, $serviceName)) {
            return false;
        }

        $this->pendingAction = null;
        $this->pendingRedirectService = null;

        return true;
    }

    protected function domainHost(string $url): ?string
    {
        try {
            $host = parse_url($url, PHP_URL_HOST);

            return is_string($host) && $host !== '' ? $host : null;
        } catch (\Throwable) {
            return null;
        }
    }

    /**
     * Build the www/non-www counterpart URL for a host.
     *
     * @param  bool  $forRedirectPairing  When true, also pair sslip/nip hosts so www↔non-www
     *                                    redirects can be configured (suggestions still skip them).
     */
    protected function wwwCounterpartUrl(string $url, bool $forRedirectPairing = false): ?string
    {
        $host = $this->domainHost($url);
        if ($host === null) {
            return null;
        }

        $lowerHost = strtolower($host);

        // Always skip bare IPs and localhost.
        if (filter_var(trim($host, '[]'), FILTER_VALIDATE_IP) !== false || $lowerHost === 'localhost') {
            return null;
        }

        // Optional suggestions skip auto-generated hosts; redirect pairing includes them so
        // Set Direction can promote the missing side to a real domain.
        if (
            ! $forRedirectPairing
            && (str_contains($lowerHost, 'sslip.io') || str_contains($lowerHost, 'nip.io'))
        ) {
            return null;
        }

        $counterpartHost = str_starts_with($lowerHost, 'www.')
            ? substr($host, 4)
            : 'www.'.$host;

        if ($counterpartHost === '' || $counterpartHost === $host) {
            return null;
        }

        $parts = parse_url($url);
        if ($parts === false) {
            return null;
        }

        $scheme = $parts['scheme'] ?? 'https';
        $port = isset($parts['port']) ? ':'.$parts['port'] : '';
        $path = $parts['path'] ?? '';
        $query = isset($parts['query']) ? '?'.$parts['query'] : '';
        $fragment = isset($parts['fragment']) ? '#'.$parts['fragment'] : '';

        return "{$scheme}://{$counterpartHost}{$port}{$path}{$query}{$fragment}";
    }

    /**
     * @return Collection<int, string>
     */
    protected function currentDomainList(?string $serviceName = null): Collection
    {
        if ($this->isCompose) {
            $domains = $this->application->docker_compose_domains
                ? json_decode($this->application->docker_compose_domains, true)
                : [];

            if (! is_array($domains)) {
                $domains = [];
            }

            $domainString = is_array($domains[$serviceName] ?? null)
                ? ($domains[$serviceName]['domain'] ?? null)
                : null;

            return collect($this->splitDomains(is_string($domainString) ? $domainString : null));
        }

        return collect($this->splitDomains($this->application->fqdn));
    }

    /**
     * @param  Collection<int, string>  $domains
     */
    protected function saveDomainList(
        Collection $domains,
        ?string $serviceName = null,
        bool $checkConflicts = true,
        ?Collection $noindexDomains = null,
    ): bool {
        $domainString = $domains->filter()->unique()->implode(',');
        $domainString = $domainString === '' ? null : ValidationPatterns::normalizeApplicationDomains($domainString);

        if ($domainString) {
            $errors = ValidationPatterns::validateApplicationDomains($domainString);
            if ($errors !== []) {
                $this->dispatch('error', $errors[0]);

                return false;
            }
        }

        $intendedComposeOverrides = null;

        if ($this->isCompose) {
            if (blank($serviceName)) {
                $this->dispatch('error', 'A service is required for compose domains.');

                return false;
            }

            $allDomains = $this->application->docker_compose_domains
                ? json_decode($this->application->docker_compose_domains, true)
                : [];

            if (! is_array($allDomains)) {
                $allDomains = [];
            }

            $previousServiceUrls = $this->currentDomainList($serviceName);
            $normalizedPorts = DomainPortOverrides::normalize($domainString, $this->application->domain_port_overrides);
            $domainString = $normalizedPorts['fqdn'];
            $intendedComposeOverrides = $this->mergeComposeDomainPortOverrides(
                $previousServiceUrls,
                $domainString,
                $normalizedPorts['overrides'] ?? null,
            );

            $existing = is_array($allDomains[$serviceName] ?? null) ? $allDomains[$serviceName] : [];
            // Preserve stored redirect only — pending Direction dropdown values must not
            // persist until setServiceRedirect() runs.
            $allDomains[$serviceName] = array_merge($existing, [
                'domain' => $domainString,
            ]);

            $this->application->docker_compose_domains = json_encode($allDomains);
            $this->application->domain_port_overrides = $intendedComposeOverrides;
            $this->application->fqdn = null;
        } else {
            $this->application->fqdn = $domainString;
        }

        if ($noindexDomains !== null) {
            $this->application->setNoindexDomains($noindexDomains);
        }

        if ($checkConflicts && ! $this->forceSaveDomains) {
            $result = checkDomainUsage(resource: $this->application);
            if ($result['hasConflicts']) {
                $this->domainConflicts = $result['conflicts'];
                $this->showDomainConflictModal = true;

                // Revert in-memory mutation so a cancelled conflict leaves model clean
                $this->application->refresh();

                return false;
            }
        } else {
            $this->forceSaveDomains = false;
        }

        $warning = sslipDomainWarning($domainString ?? '');
        if ($warning) {
            $this->dispatch('warning', __('warning.sslipdomain'));
        }

        $this->application->save();

        if ($this->isCompose && ($this->application->domain_port_overrides ?? null) !== $intendedComposeOverrides) {
            $this->application->domain_port_overrides = $intendedComposeOverrides;
            $this->application->save();
        }

        $this->resetDefaultLabels();
        $this->dispatch('configurationChanged');

        return true;
    }

    /**
     * @param  Collection<int, string>  $previousServiceUrls
     * @param  array<string, int>|null  $incomingOverrides
     * @return array<string, int>|null
     */
    protected function mergeComposeDomainPortOverrides(
        Collection $previousServiceUrls,
        ?string $newDomainString,
        ?array $incomingOverrides,
    ): ?array {
        $merged = $this->application->domain_port_overrides ?? [];
        $newCanonical = collect($this->splitDomains($newDomainString))
            ->map(fn (string $url): string => DomainPortOverrides::withoutPort($url))
            ->all();

        foreach ($previousServiceUrls as $url) {
            $canonical = DomainPortOverrides::withoutPort($url);
            if (! in_array($canonical, $newCanonical, true)) {
                unset($merged[$canonical]);
            }
        }

        foreach ($incomingOverrides ?? [] as $url => $port) {
            $merged[$url] = (int) $port;
        }

        return $merged ?: null;
    }

    protected function resetDefaultLabels(): void
    {
        try {
            if (! $this->application->settings->is_container_label_readonly_enabled) {
                return;
            }

            $customLabels = str(implode('|coolify|', generateLabelsApplication($this->application)))->replace('|coolify|', "\n");
            $this->application->custom_labels = base64_encode($customLabels);
            $this->application->save();
            $this->dispatch('configurationChanged');
        } catch (\Throwable $e) {
            handleError($e, $this);
        }
    }

    public function render()
    {
        return view('livewire.project.application.domains');
    }
}