@extends('eliteadmin::layouts.app')

@section('title', 'Update Admin')
@section('page_class', 'page-updates')

@push('styles')
<link href="{{ asset('assets/css/updates.css') }}" rel="stylesheet">
@endpush

@section('content')
    <div class="page-header">
        <div>
            <h1 class="page-title">Update Admin</h1>
            <nav class="breadcrumb">
                <a href="{{ route('dashboard') }}" class="breadcrumb-item">Home</a>
                <span class="breadcrumb-item active">Update Admin</span>
            </nav>
        </div>
        <div class="page-header-actions">
            <button type="button" class="btn btn-outline-secondary btn-sm" data-bs-toggle="modal" data-bs-target="#updateSettingsModal">
                <i class="bi bi-gear me-1"></i> Configure Update Settings
            </button>
        </div>
    </div>

    @if ($check->updateAvailable)
        <div class="update-alert update-alert-info mb-4">
            <i class="bi bi-info-circle-fill"></i>
            <span>An update is available.</span>
        </div>
    @elseif ($check->platformUnavailable)
        <x-eliteadmin::alert type="warning" :message="$check->message" class="mb-4" />
    @elseif ($check->success)
        <div class="update-alert update-alert-success mb-4">
            <i class="bi bi-check-circle-fill"></i>
            <span>{{ $check->message ?? 'Your admin application is up to date.' }}</span>
        </div>
    @endif

    <div class="update-version-compare mb-4">
        <div class="update-version-panel update-version-current">
            <div class="update-version-label">Your Version</div>
            <div class="update-version-number">{{ $current['version'] }}</div>
            <div class="update-version-meta">{{ $current['channel'] }}</div>
            <div class="update-version-build">{{ $current['build'] }}</div>
        </div>
        <div class="update-version-panel update-version-latest">
            <div class="update-version-label">Latest Version</div>
            <div class="update-version-number">{{ $check->latestVersion ?? $current['version'] }}</div>
            <div class="update-version-meta">{{ $check->latestChannel ?? $current['channel'] }}</div>
            <div class="update-version-build">{{ $check->latestBuild ?? $current['build'] }}</div>
        </div>
    </div>

    <div class="text-center mb-4">
        @can('update', \App\Models\Setting::class)
            @if ($canApplyUpdate)
                <form action="{{ route('updates.apply') }}" method="POST" class="d-inline">
                    @csrf
                    <button type="submit" class="btn btn-primary btn-lg px-5" onclick="return confirm('Download and stage the admin update package now?');">
                        Update Now
                    </button>
                </form>
            @else
                <button type="button" class="btn btn-primary btn-lg px-5" disabled>
                    Update Now
                </button>
            @endif
        @endcan

        <div class="update-links mt-3">
            @if ($check->releaseNotesUrl)
                <a href="{{ $check->releaseNotesUrl }}" target="_blank" rel="noopener">Release Notes</a>
            @endif
            @if ($check->changelogUrl)
                <span class="text-muted">|</span>
                <a href="{{ $check->changelogUrl }}" target="_blank" rel="noopener">Changelog</a>
            @endif
        </div>
    </div>

    @if ($check->updateAvailable && ! $tempPathConfigured)
        <div class="update-alert update-alert-warning mb-4">
            <i class="bi bi-hand-index-thumb"></i>
            <span>
                Warning: You do not currently have a path configured for temporary storage of files during updates.
                You will not be able to perform an update until one is set.
                <button type="button" class="btn btn-link p-0 align-baseline" data-bs-toggle="modal" data-bs-target="#updateSettingsModal">Configure it now</button>.
            </span>
        </div>
    @elseif ($check->updateAvailable && empty($check->downloadUrl))
        <div class="update-alert update-alert-warning mb-4">
            <i class="bi bi-hand-index-thumb"></i>
            <span>
                An update is available, but no automatic download URL is configured on the Product Owner platform.
                Review the release notes and deploy manually using your production process.
            </span>
        </div>
    @endif

    <div class="update-footer-meta text-center">
        <p class="text-muted mb-2">
            Last Checked for Updates:
            @if ($lastCheckedAt)
                {{ $lastCheckedAt->diffForHumans() }}
            @else
                never
            @endif
        </p>
        @can('update', \App\Models\Setting::class)
            <form action="{{ route('updates.check') }}" method="POST" class="d-inline">
                @csrf
                <button type="submit" class="btn btn-outline-primary btn-sm">
                    <i class="bi bi-arrow-clockwise me-1"></i> Check Now
                </button>
            </form>
        @endcan
    </div>

    @can('update', \App\Models\Setting::class)
        <div class="modal fade" id="updateSettingsModal" tabindex="-1" aria-labelledby="updateSettingsModalLabel" aria-hidden="true">
            <div class="modal-dialog">
                <div class="modal-content">
                    <form action="{{ route('updates.settings') }}" method="POST">
                        @csrf
                        @method('PUT')
                        <div class="modal-header">
                            <h5 class="modal-title" id="updateSettingsModalLabel">Configure Update Settings</h5>
                            <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
                        </div>
                        <div class="modal-body">
                            <div class="mb-3">
                                <label for="update_temp_path" class="form-label">Temporary update path</label>
                                <input type="text" name="update_temp_path" id="update_temp_path"
                                       class="form-control @error('update_temp_path') is-invalid @enderror"
                                       value="{{ old('update_temp_path', $tempPath) }}" required>
                                @error('update_temp_path')<div class="invalid-feedback">{{ $message }}</div>@enderror
                                <div class="form-text">
                                    Writable directory used to store update packages during deployment.
                                    Default: <code>{{ config('update.default_temp_path') }}</code>
                                </div>
                            </div>
                        </div>
                        <div class="modal-footer">
                            <button type="button" class="btn btn-outline-secondary" data-bs-dismiss="modal">Cancel</button>
                            <button type="submit" class="btn btn-primary">Save Settings</button>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    @endcan
@endsection

@if ($errors->has('update_temp_path'))
    @push('scripts')
        <script>
            document.addEventListener('DOMContentLoaded', function () {
                var modal = document.getElementById('updateSettingsModal');
                if (modal) {
                    bootstrap.Modal.getOrCreateInstance(modal).show();
                }
            });
        </script>
    @endpush
@endif
