<?php

namespace App\Http\Controllers\Admin;

use App\Core\Services\Update\UpdateChecker;
use App\Http\Controllers\Controller;
use App\Models\Update;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\View\View;

class UpdateController extends Controller
{
    public function __construct(
        private UpdateChecker $updateChecker
    ) {}

    /**
     * Display update management page
     */
    public function index(): View
    {
        $currentVersion = $this->updateChecker->getCurrentVersion();
        $hasAccess = $this->updateChecker->hasUpdateAccess();
        $availableUpdates = $this->updateChecker->getAvailableUpdates();
        $latestUpdate = $this->updateChecker->getLatestUpdate();
        $installedUpdates = Update::where('is_installed', true)
            ->orderBy('installed_at', 'desc')
            ->limit(10)
            ->get();

        return view('admin.updates.index', [
            'currentVersion' => $currentVersion,
            'hasAccess' => $hasAccess,
            'availableUpdates' => $availableUpdates,
            'latestUpdate' => $latestUpdate,
            'installedUpdates' => $installedUpdates,
        ]);
    }

    /**
     * Check for updates (AJAX)
     */
    public function check(Request $request): JsonResponse
    {
        $force = $request->boolean('force', false);

        $result = $this->updateChecker->checkForUpdates($force);

        return response()->json($result);
    }

    /**
     * Show update details
     */
    public function show(Update $update): View
    {
        $currentVersion = $this->updateChecker->getCurrentVersion();
        $hasAccess = $this->updateChecker->hasUpdateAccess();

        return view('admin.updates.show', [
            'update' => $update,
            'currentVersion' => $currentVersion,
            'hasAccess' => $hasAccess,
        ]);
    }

    /**
     * Mark update as installed (manual update tracking)
     */
    public function markInstalled(Update $update): RedirectResponse
    {
        $this->updateChecker->markAsInstalled($update);

        return redirect()->route('admin.updates.index')
            ->with('success', "Update {$update->version} marked as installed.");
    }

    /**
     * Get update status (AJAX)
     */
    public function status(): JsonResponse
    {
        $currentVersion = $this->updateChecker->getCurrentVersion();
        $hasAccess = $this->updateChecker->hasUpdateAccess();
        $latestUpdate = $this->updateChecker->getLatestUpdate();
        $hasUpdate = $latestUpdate !== null;

        return response()->json([
            'current_version' => $currentVersion,
            'has_access' => $hasAccess,
            'has_update' => $hasUpdate,
            'latest_version' => $latestUpdate?->version,
            'latest_update' => $latestUpdate ? [
                'version' => $latestUpdate->version,
                'title' => $latestUpdate->title,
                'type' => $latestUpdate->type,
            ] : null,
        ]);
    }
}
