@extends('layout')

@section('title', __('store.clientarea_domains_management_title') . ' - ' . $domain->domain)

@section('content')
<div class="container py-5">
    <div class="row">
        <div class="col-12">
            <x-clientarea.page-header
                icon="fas fa-globe"
                :title="$domain->domain"
                :subtitle="__('store.clientarea_domains_management_title')"
            >
                <a href="{{ route('store.auth.domains') }}" class="btn btn-outline-secondary">
                    <i class="fas fa-arrow-left me-2"></i>{{ __('store.clientarea_domains_management_back_to_domains') }}
                </a>
            </x-clientarea.page-header>

            <!-- Alert Messages -->
            @if(session('success'))
                <div class="alert alert-success alert-dismissible fade show" role="alert">
                    <i class="fas fa-check-circle me-2"></i>{{ session('success') }}
                    <button type="button" class="btn-close" data-bs-dismiss="alert"></button>
                        </div>
            @endif

            @if(session('error'))
                <div class="alert alert-danger alert-dismissible fade show" role="alert">
                    <i class="fas fa-exclamation-triangle me-2"></i>{{ session('error') }}
                    <button type="button" class="btn-close" data-bs-dismiss="alert"></button>
                        </div>
                                    @endif

            @if(isset($error))
                <div class="alert alert-danger" role="alert">
                    <i class="fas fa-exclamation-triangle me-2"></i>{{ $error }}
                </div>
            @endif

            <!-- Domain Not Active Warning — status-aware mesaj (PRC-47) -->
            @if(!($domainActive ?? true))
                @php
                    $statusKey = 'store.domain_inactive_' . str_replace('-', '_', $domain->status ?? '');
                    $inactiveMessage = __($statusKey);
                    if ($inactiveMessage === $statusKey) {
                        $inactiveMessage = __('store.domain_inactive_default');
                    }
                @endphp
                <div class="alert alert-warning border-0 shadow-sm mb-3">
                    <i class="fas fa-exclamation-triangle me-2"></i>{{ $inactiveMessage }}
                </div>
            @endif

            <!-- Main Content -->
            <div class="row">
                <!-- Left Content Area (col-md-8) -->
                <div class="col-md-8">
            <div class="card">
                <div class="card-body">
                            @if($currentFeature === 'overview')
                                @include('clientarea.domain-manage.overview')
                            @elseif($currentFeature === 'nameservers')
                                @include('clientarea.domain-manage.nameservers')
                            @elseif($currentFeature === 'lock')
                                @include('clientarea.domain-manage.lock')
                            @elseif($currentFeature === 'privacy')
                                @include('clientarea.domain-manage.privacy')
                            @elseif($currentFeature === 'contacts')
                                @include('clientarea.domain-manage.contacts')
                            @elseif($currentFeature === 'dns')
                                @include('clientarea.domain-manage.dns')
                            @elseif($currentFeature === 'child_nameservers')
                                @include('clientarea.domain-manage.child-nameservers')
                            @elseif($currentFeature === 'epp-code')
                                @include('clientarea.domain-manage.epp-code')
                            @elseif($currentFeature === 'email-forwarding')
                                @include('clientarea.domain-manage.email-forwarding')
                            @elseif($currentFeature === 'url-forwarding')
                                @include('clientarea.domain-manage.url-forwarding')
                            @endif
                                </div>
                            </div>

                    @if(!empty($clientOutput ?? ''))
                        <div class="card border-0 shadow-sm mt-4">
                            <div class="card-header">
                                <h5 class="mb-0"><i class="fas fa-info-circle me-2"></i>{{ __('store.domain_module_info') }}</h5>
                            </div>
                            <div class="card-body">
                                {!! $clientOutput !!}
                            </div>
                        </div>
                    @endif
                        </div>

                <!-- Right Sidebar (col-md-4) -->
                <div class="col-md-4">
                    @php
                        $unpaidInvoices = $domain->getUnpaidInvoices();
                        $statusClass = match($domain->status) {
                            'active' => 'success',
                            'pending' => 'warning',
                            'suspended' => 'danger',
                            'expired' => 'secondary',
                            default => 'secondary'
                        };
                        $statusIcon = match($domain->status) {
                            'active' => 'check-circle',
                            'pending' => 'clock',
                            'suspended' => 'ban',
                            'expired' => 'calendar-times',
                            default => 'question-circle'
                        };
                    @endphp

                    <!-- Domain Status Card -->
                    <div class="card mb-3 border-{{ $statusClass }}" style="border-width: 2px;">
                        <div class="card-body">
                            <div class="d-flex align-items-center mb-3">
                                <div class="flex-shrink-0">
                                    <div class="bg-{{ $statusClass }} bg-opacity-25 rounded-circle p-3">
                                        <i class="fas fa-{{ $statusIcon }} fa-2x text-{{ $statusClass }}"></i>
                                    </div>
                                </div>
                                <div class="flex-grow-1 ms-3">
                                    <h6 class="mb-1 fw-bold">{{ __('store.common_status') }}</h6>
                                    <span class="badge bg-{{ $statusClass }} fs-6">
                                        {{ ucfirst(__('store.clientarea_domains_status_' . $domain->status)) }}
                                    </span>
                                </div>
                            </div>

                            <hr class="my-3">

                            <!-- Domain Info -->
                            <div class="mb-3">
                                <div class="d-flex align-items-center text-muted small mb-2">
                                    <i class="fas fa-building fa-fw me-2"></i>
                                    <span class="me-2">{{ __('store.clientarea_domains_registrar') }}:</span>
                                    <strong class="ms-auto">{{ $domain->registrar->name ?? 'N/A' }}</strong>
                                </div>
                                <div class="d-flex align-items-center text-muted small mb-2">
                                    <i class="fas fa-calendar-plus fa-fw me-2"></i>
                                    <span class="me-2">{{ __('store.clientarea_domains_registration_date') }}:</span>
                                    <strong class="ms-auto">{{ $domain->create_date?->format($dateFormat) ?? 'N/A' }}</strong>
                                </div>
                                <div class="d-flex align-items-center text-muted small">
                                    <i class="fas fa-calendar-check fa-fw me-2"></i>
                                    <span class="me-2">{{ __('store.clientarea_domains_expiry_date') }}:</span>
                                    <strong class="ms-auto">{{ $domain->expire_date?->format($dateFormat) ?? 'N/A' }}</strong>
                                </div>
                            </div>

                            @if($domain->expire_date)
                                @php
                                    $daysToExpiry = now()->diffInDays($domain->expire_date, false);
                                    $progressPercent = 0;
                                    $progressClass = 'success';

                                    if ($domain->create_date) {
                                        $totalDays = $domain->create_date->diffInDays($domain->expire_date);
                                        $elapsedDays = $domain->create_date->diffInDays(now());
                                        $progressPercent = $totalDays > 0 ? min(100, ($elapsedDays / $totalDays) * 100) : 0;

                                        if ($progressPercent > 90) {
                                            $progressClass = 'danger';
                                        } elseif ($progressPercent > 75) {
                                            $progressClass = 'warning';
                                        }
                                    }
                                @endphp

                                <!-- Expiry Progress -->
                                <div class="mt-3">
                                    <div class="d-flex justify-content-between align-items-center mb-2">
                                        <small class="text-body-secondary">{{ __('store.clientarea_domains_days_to_expiry') }}</small>
                                        <span class="badge bg-{{ $daysToExpiry < 30 ? 'danger' : ($daysToExpiry < 90 ? 'warning' : 'success') }}">
                                            {{ ceil($daysToExpiry) }} {{ __('store.common_days') }}
                                        </span>
                                    </div>
                                    <div class="progress bg-body-secondary" style="height: 8px;">
                                        <div class="progress-bar bg-{{ $progressClass }}" role="progressbar"
                                             style="width: {{ $progressPercent }}%"
                                             aria-valuenow="{{ $progressPercent }}"
                                             aria-valuemin="0"
                                             aria-valuemax="100">
                                        </div>
                                    </div>
                                </div>

                                @if($daysToExpiry < 30)
                                    <div class="alert alert-warning mt-3 mb-0 py-2 px-3">
                                        <i class="fas fa-exclamation-triangle me-2"></i>
                                        <small>{{ __('store.clientarea_domains_expiry_warning') }}</small>
                                    </div>
                                @endif
                            @endif
                        </div>
                    </div>

                    @include('clientarea.partials.unpaid-invoices-warning', ['unpaidInvoices' => $unpaidInvoices])

                    <!-- Features Menu -->
                    <div class="card">
                        <div class="card-header">
                            <h6 class="mb-0">
                                <i class="fas fa-tools me-2"></i>{{ __('store.clientarea_domains_management_features') }}
                            </h6>
                        </div>
                        <div class="list-group list-group-flush">
                            <a href="{{ route('store.auth.domains.manage', $domain->id) }}"
                               class="list-group-item list-group-item-action {{ $currentFeature === 'overview' ? 'active' : '' }}">
                                <i class="fas fa-info-circle me-2"></i>{{ __('store.clientarea_domains_management_tabs_overview') }}
                            </a>

                            @if(in_array('nameservers', $supportedFeatures))
                                <a href="{{ route('store.auth.domains.manage.nameservers', $domain->id) }}"
                                   class="list-group-item list-group-item-action {{ $currentFeature === 'nameservers' ? 'active' : '' }}">
                                    <i class="fas fa-server me-2"></i>{{ __('store.clientarea_domains_management_tabs_nameservers') }}
                                </a>
                            @endif

                            @if(in_array('lock', $supportedFeatures))
                                <a href="{{ route('store.auth.domains.manage.lock', $domain->id) }}"
                                   class="list-group-item list-group-item-action {{ $currentFeature === 'lock' ? 'active' : '' }}">
                                    <i class="fas fa-lock me-2"></i>{{ __('store.clientarea_domains_management_tabs_lock') }}
                                </a>
                            @endif

                            @if(in_array('privacy', $supportedFeatures) && $domain->getEffectiveIdProtection())
                                <a href="{{ route('store.auth.domains.manage.privacy', $domain->id) }}"
                                   class="list-group-item list-group-item-action {{ $currentFeature === 'privacy' ? 'active' : '' }}">
                                    <i class="fas fa-shield-alt me-2"></i>{{ __('store.clientarea_domains_management_tabs_privacy') }}
                                </a>
                            @endif

                            @if(in_array('contacts', $supportedFeatures))
                                <a href="{{ route('store.auth.domains.manage.contacts', $domain->id) }}"
                                   class="list-group-item list-group-item-action {{ $currentFeature === 'contacts' ? 'active' : '' }}">
                                    <i class="fas fa-address-book me-2"></i>{{ __('store.clientarea_domains_management_tabs_contacts') }}
                                </a>
                            @endif

                            @if(in_array('dns', $supportedFeatures) && $domain->getEffectiveDnsManagement())
                                <a href="{{ route('store.auth.domains.manage.dns', $domain->id) }}"
                                   class="list-group-item list-group-item-action {{ $currentFeature === 'dns' ? 'active' : '' }}">
                                    <i class="fas fa-network-wired me-2"></i>{{ __('store.clientarea_domains_management_tabs_dns') }}
                                </a>
                            @endif

                            @if(in_array('child_nameservers', $supportedFeatures))
                                <a href="{{ route('store.auth.domains.manage.child-nameservers', $domain->id) }}"
                                   class="list-group-item list-group-item-action {{ $currentFeature === 'child_nameservers' ? 'active' : '' }}">
                                    <i class="fas fa-sitemap me-2"></i>{{ __('store.clientarea_domains_management_tabs_child_nameservers') }}
                                </a>
                            @endif

                            @if(in_array('epp_code', $supportedFeatures) && $domain->getEffectiveEppCode())
                                <a href="{{ route('store.auth.domains.manage.epp-code', $domain->id) }}"
                                   class="list-group-item list-group-item-action {{ $currentFeature === 'epp-code' ? 'active' : '' }}">
                                    <i class="fas fa-key me-2"></i>{{ __('store.clientarea_domains_management_tabs_epp_code') }}
                                </a>
                            @endif

                            @if(in_array('email_forwarding', $supportedFeatures) && $domain->getEffectiveEmailForwarding())
                                <a href="{{ route('store.auth.domains.manage.email-forwarding', $domain->id) }}"
                                   class="list-group-item list-group-item-action {{ $currentFeature === 'email-forwarding' ? 'active' : '' }}">
                                    <i class="fas fa-envelope me-2"></i>{{ __('store.clientarea_domains_management_tabs_email_forwarding') }}
                                </a>
                            @endif

                            {{-- PRC-777 — URL Forwarding: url_forwarding capability + DNS yazımı şart --}}
                            @if(in_array('url_forwarding', $supportedFeatures) && $domain->getEffectiveDnsManagement())
                                <a href="{{ route('store.auth.domains.manage.url-forwarding', $domain->id) }}"
                                   class="list-group-item list-group-item-action {{ $currentFeature === 'url-forwarding' ? 'active' : '' }}">
                                    <i class="fas fa-directions me-2"></i>{{ __('store.clientarea_domains_management_tabs_url_forwarding') }}
                                </a>
                            @endif
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
@endsection
