<?php

declare(strict_types=1);

namespace App\Http\Controllers\Api\V1\Admin;

use App\Http\Controllers\Controller;
use App\Models\Board;
use App\Models\Skin;
use App\Services\Theme\SkinManager;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;

class SkinController extends Controller
{
    public function __construct(protected SkinManager $skins) {}

    protected function ensureAdmin(Request $request): void
    {
        $user = $request->user();
        if (! $user || ! ($user->hasRole('super_admin') || $user->hasRole('tenant_admin'))) {
            throw new AccessDeniedHttpException('ADMIN_ONLY');
        }
    }

    public function index(Request $request): JsonResponse
    {
        $this->ensureAdmin($request);
        $type = $request->query('type');
        $q = Skin::query();
        if ($type) $q->where('type', $type);
        $items = $q->orderBy('type')->orderByDesc('is_default')->orderBy('name')->get();
        return response()->json([
            'data' => $items->map(fn($s) => [
                'id' => $s->id, 'slug' => $s->slug, 'name' => $s->name,
                'version' => $s->version, 'author' => $s->author,
                'description' => $s->description, 'type' => $s->type,
                'path' => $s->path, 'screenshot_url' => $s->screenshot_url,
                'is_default' => (bool) $s->is_default,
                'settings' => $s->settings,
            ]),
        ]);
    }

    public function scan(Request $request): JsonResponse
    {
        $this->ensureAdmin($request);
        $type = $request->input('type');
        $found = $this->skins->scan($type);
        return response()->json(['scanned' => $found->count()]);
    }

    public function activateDefault(Request $request, string $type, string $slug): JsonResponse
    {
        $this->ensureAdmin($request);
        $ok = $this->skins->activateDefault($type, $slug);
        return response()->json(['ok' => $ok], $ok ? 200 : 404);
    }

    public function upload(Request $request): JsonResponse
    {
        $this->ensureAdmin($request);
        $request->validate([
            'file' => 'required|file|max:20480',
            'type' => 'required|string|in:board,member,shop,comment,page',
        ]);
        $tmp = $request->file('file')->getRealPath();
        try {
            $skin = $this->skins->installFromZip($tmp, $request->input('type'));
        } catch (\Throwable $e) {
            return response()->json(['error' => $e->getMessage()], 422);
        }
        return response()->json(['slug' => $skin?->slug, 'type' => $skin?->type], 201);
    }

    public function assignToBoard(Request $request, string $boardSlug): JsonResponse
    {
        $this->ensureAdmin($request);
        $request->validate(['skin_slug' => 'required|string']);
        $ok = $this->skins->assignToBoard($boardSlug, $request->input('skin_slug'));
        return response()->json(['ok' => $ok], $ok ? 200 : 404);
    }

    public function destroy(Request $request, string $type, string $slug): JsonResponse
    {
        $this->ensureAdmin($request);
        $ok = $this->skins->uninstall($type, $slug);
        return response()->json(['ok' => $ok], $ok ? 200 : 422);
    }
}
