<?php

namespace App\Http\Controllers\Admin;

use App\Addons\Services\AddonLoader;
use App\Addons\Services\AddonManager;
use App\Http\Controllers\Controller;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\View\View;

/**
 * Addon Controller
 * Handles addon management in admin panel
 */
class AddonController extends Controller
{
    public function __construct(
        private AddonLoader $addonLoader,
        private AddonManager $addonManager
    ) {}

    /**
     * Display addons management page
     */
    public function index(): View
    {
        $addons = $this->addonLoader->getAllAddons();

        // Get status for each addon
        foreach ($addons as $slug => $addon) {
            $addons[$slug]['status'] = $this->addonManager->getAddonStatus($slug);
        }

        return view('admin.addons.index', [
            'addons' => $addons,
        ]);
    }

    /**
     * Enable an addon
     */
    public function enable(Request $request, string $slug): JsonResponse
    {
        $result = $this->addonManager->enable($slug);

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

    /**
     * Disable an addon
     */
    public function disable(Request $request, string $slug): JsonResponse
    {
        $result = $this->addonManager->disable($slug);

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

    /**
     * Get addon status
     */
    public function status(string $slug): JsonResponse
    {
        $status = $this->addonManager->getAddonStatus($slug);
        $addon = $this->addonLoader->loadAddon($slug);

        return response()->json([
            'status' => $status,
            'addon' => $addon,
        ]);
    }
}
