@extends('layouts.admin')

@section('title', 'Update ' . $update->version)

@section('page-title', 'Update Details')

@section('breadcrumbs')
    <a href="{{ route('admin.updates.index') }}" class="hover:text-primary">Updates</a>
    <span class="material-symbols-outlined text-xs">chevron_right</span>
    <span class="text-primary font-medium">{{ $update->version }}</span>
@endsection

@section('header-actions')
    @if($update->download_url && $hasAccess)
        <a href="{{ $update->download_url }}" target="_blank" class="px-4 py-2 bg-primary text-white rounded-lg hover:bg-blue-600 transition-colors flex items-center gap-2">
            <span class="material-symbols-outlined text-lg">download</span>
            <span>Download Update</span>
        </a>
    @endif
    @if(!$update->is_installed)
        <form method="POST" action="{{ route('admin.updates.mark-installed', $update) }}" class="inline">
            @csrf
            <button type="submit" class="px-4 py-2 border border-[#dbe0e6] text-[#617589] rounded-lg hover:bg-background-light transition-colors flex items-center gap-2">
                <span class="material-symbols-outlined text-lg">check_circle</span>
                <span>Mark as Installed</span>
            </button>
        </form>
    @endif
@endsection

@section('content')
<div class="max-w-5xl mx-auto">
    <!-- Update Header -->
    <div class="bg-white rounded-xl shadow-sm border border-[#e5e7eb] p-6 mb-6">
        <div class="flex items-start justify-between mb-6">
            <div>
                <div class="flex items-center gap-3 mb-2">
                    <h2 class="text-2xl font-bold text-[#111418] text-gray-900">{{ $update->title }}</h2>
                    <span class="px-3 py-1 rounded-lg text-sm font-medium {{ $update->getTypeColor() }}">
                        {{ ucfirst($update->type) }} Update
                    </span>
                </div>
                <p class="text-lg text-primary font-semibold">Version {{ $update->version }}</p>
                @if($update->released_at)
                    <p class="text-sm text-[#617589] mt-1">Released: {{ $update->released_at->format('F d, Y') }}</p>
                @endif
            </div>
            <div class="text-right">
                @if($update->is_installed)
                    <span class="px-3 py-1 bg-green-100 text-green-700 rounded-lg text-sm font-medium">Installed</span>
                @else
                    <span class="px-3 py-1 bg-blue-100 text-blue-700 rounded-lg text-sm font-medium">Available</span>
                @endif
            </div>
        </div>

        @if($update->description)
            <div class="mb-6">
                <h3 class="text-sm font-semibold text-[#111418] text-gray-900 mb-2">Description</h3>
                <p class="text-sm text-[#617589]">{{ $update->description }}</p>
            </div>
        @endif

        @if(!$hasAccess)
            <div class="bg-yellow-50 border border-yellow-200 rounded-lg p-4">
                <div class="flex items-start gap-3">
                    <span class="material-symbols-outlined text-yellow-600">warning</span>
                    <div>
                        <h4 class="font-bold text-yellow-900 mb-1">License Required</h4>
                        <p class="text-sm text-yellow-800">A valid license is required to download this update. Please activate your license in <a href="{{ route('admin.license.index') }}" class="underline font-medium">License Settings</a>.</p>
                    </div>
                </div>
            </div>
        @endif
    </div>

    <!-- Changelog -->
    @if($update->changelog)
        <div class="bg-white rounded-xl shadow-sm border border-[#e5e7eb] p-6 mb-6">
            <h3 class="text-lg font-bold text-[#111418] text-gray-900 mb-4">Changelog</h3>
            <div class="prose dark:prose-invert max-w-none text-sm">
                {!! nl2br(e($update->changelog)) !!}
            </div>
        </div>
    @endif

    <!-- Update Information -->
    <div class="bg-white rounded-xl shadow-sm border border-[#e5e7eb] p-6 mb-6">
        <h3 class="text-lg font-bold text-[#111418] text-gray-900 mb-4">Update Information</h3>
        <div class="grid grid-cols-1 md:grid-cols-2 gap-4">
            <div>
                <p class="text-xs font-semibold text-[#617589] uppercase mb-1">Current Version</p>
                <p class="text-sm font-medium text-[#111418] text-gray-900">{{ $currentVersion }}</p>
            </div>
            <div>
                <p class="text-xs font-semibold text-[#617589] uppercase mb-1">Update Version</p>
                <p class="text-sm font-medium text-[#111418] text-gray-900">{{ $update->version }}</p>
            </div>
            <div>
                <p class="text-xs font-semibold text-[#617589] uppercase mb-1">Update Type</p>
                <p class="text-sm font-medium text-[#111418] text-gray-900">{{ ucfirst($update->type) }}</p>
            </div>
            @if($update->released_at)
                <div>
                    <p class="text-xs font-semibold text-[#617589] uppercase mb-1">Release Date</p>
                    <p class="text-sm font-medium text-[#111418] text-gray-900">{{ $update->released_at->format('F d, Y') }}</p>
                </div>
            @endif
        </div>
    </div>

    <!-- Manual Update Instructions -->
    <div class="bg-white rounded-xl shadow-sm border border-[#e5e7eb] p-6">
        <h3 class="text-lg font-bold text-[#111418] text-gray-900 mb-4">Installation Instructions</h3>
        <div class="prose dark:prose-invert max-w-none">
            <ol class="list-decimal list-inside space-y-3 text-sm text-[#617589]">
                <li><strong class="text-[#111418] text-gray-900">Backup your system</strong> - Create a full backup of your database and files before proceeding</li>
                <li><strong class="text-[#111418] text-gray-900">Download the update</strong> - Click the "Download Update" button above to get the update package</li>
                <li><strong class="text-[#111418] text-gray-900">Extract the files</strong> - Extract the downloaded ZIP file to a temporary location</li>
                <li><strong class="text-[#111418] text-gray-900">Upload via FTP/SFTP</strong> - Upload all files from the extracted folder to your server, overwriting existing files</li>
                <li><strong class="text-[#111418] text-gray-900">Run migrations</strong> - SSH into your server and run: <code class="bg-background-light px-2 py-1 rounded">php artisan migrate</code></li>
                <li><strong class="text-[#111418] text-gray-900">Clear caches</strong> - Run these commands:
                    <ul class="list-disc list-inside ml-4 mt-2 space-y-1">
                        <li><code class="bg-background-light px-2 py-1 rounded">php artisan cache:clear</code></li>
                        <li><code class="bg-background-light px-2 py-1 rounded">php artisan config:clear</code></li>
                        <li><code class="bg-background-light px-2 py-1 rounded">php artisan view:clear</code></li>
                    </ul>
                </li>
                <li><strong class="text-[#111418] text-gray-900">Verify installation</strong> - Check that your application is running correctly</li>
                <li><strong class="text-[#111418] text-gray-900">Mark as installed</strong> - Click "Mark as Installed" button above to track this update</li>
            </ol>
            <div class="mt-6 p-4 bg-red-50 border border-red-200 rounded-lg">
                <p class="text-sm text-red-800"><strong>⚠️ Important:</strong> This system does not support automatic updates. All updates must be performed manually. Never use shell commands or automated scripts that could compromise your system security.</p>
            </div>
        </div>
    </div>

    @if($update->release_notes_url)
        <div class="mt-6 text-center">
            <a href="{{ $update->release_notes_url }}" target="_blank" class="text-primary hover:underline text-sm">View Full Release Notes →</a>
        </div>
    @endif
</div>
@endsection



