<?php

declare(strict_types=1);

namespace App\Http\Controllers\Academy;

use App\Http\Controllers\Controller;
use App\Models\AcademyCourse;
use App\Models\AcademyLesson;
use App\Services\Academy\AcademyAccessService;
use App\Services\Academy\AcademyAnalyticsService;
use App\Services\Academy\AcademyCacheService;
use App\Services\Academy\AcademyInteractionService;
use App\Support\AcademyAnalytics\AcademyAnalyticsContentType;
use App\Support\Seo\SeoFactory;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Str;
use Inertia\Inertia;
use Inertia\Response;

final class AcademyLessonController extends Controller
{
    public function __construct(
        private readonly AcademyAccessService $access,
        private readonly AcademyCacheService $cache,
        private readonly AcademyAnalyticsService $analytics,
        private readonly AcademyInteractionService $interactions,
    ) {}

    public function index(Request $request): Response|JsonResponse
    {
        abort_unless((bool) config('academy.enabled', true), 404);

        $filters = $request->validate([
            'q' => ['nullable', 'string', 'max:120'],
            'category' => ['nullable', 'string', 'max:140'],
            'difficulty' => ['nullable', 'string', 'max:40'],
        ]);

        $query = AcademyLesson::query()
            ->with('category')
            ->active()
            ->published()
            ->orderedForCourse();

        if (filled($filters['q'] ?? null)) {
            $query->where(function ($builder) use ($filters): void {
                $builder->where('title', 'like', '%'.$filters['q'].'%')
                    ->orWhere('excerpt', 'like', '%'.$filters['q'].'%');
            });
        }

        if (filled($filters['category'] ?? null)) {
            $query->whereHas('category', fn ($builder) => $builder->where('slug', $filters['category']));
        }

        if (filled($filters['difficulty'] ?? null)) {
            $query->where('difficulty', $filters['difficulty']);
        }

        $lessons = $query->paginate(12)->withQueryString();
        $lessons->getCollection()->transform(fn (AcademyLesson $lesson): array => $this->access->lessonPayload($lesson, $request->user()));

        if (filled($filters['q'] ?? null)) {
            $this->analytics->trackSearch((string) $filters['q'], (int) $lessons->total(), array_filter($filters), $request);
        }

        if ($request->expectsJson()) {
            return response()->json($lessons);
        }

        $seo = app(SeoFactory::class)
            ->collectionListing(
                'Academy Lessons — Skinbase',
                'Browse Skinbase AI Academy lessons covering prompting, workflow cleanup, ethics, and upload-ready creative workflows.',
                route('academy.lessons.index', $request->query()),
            )
            ->toArray();

        return Inertia::render('Academy/List', [
            'pageType' => 'lessons',
            'title' => 'Academy lessons',
            'description' => 'Step-by-step tutorials and workflow guides for AI-assisted creative work on Skinbase.',
            'seo' => $seo,
            'breadcrumbs' => [
                ['label' => 'Academy', 'href' => route('academy.index')],
                ['label' => 'Lessons', 'href' => route('academy.lessons.index')],
            ],
            'items' => $lessons,
            'filters' => $filters,
            'categories' => $this->cache->categoriesByType('lesson'),
            'pricingUrl' => route('academy.pricing'),
            'coursesUrl' => route('academy.courses.index'),
            'promptLibraryUrl' => route('academy.prompts.index'),
            'academyAccess' => array_merge($this->access->accessSummary($request->user()), [
                'billingUrl' => $request->user() && (bool) config('academy_billing.enabled', false)
                    ? route('academy.billing.account')
                    : route('academy.pricing'),
            ]),
            'analytics' => [
                'enabled' => true,
                'contentType' => filled($filters['q'] ?? null) ? AcademyAnalyticsContentType::SEARCH : null,
                'contentId' => null,
                'eventUrl' => route('academy.analytics.events.store'),
                'pageName' => 'academy_lessons_index',
                'search' => filled($filters['q'] ?? null) ? [
                    'query' => (string) $filters['q'],
                    'resultsCount' => (int) $lessons->total(),
                ] : null,
                'isPremium' => false,
                'isGuest' => $request->user() === null,
                'isSubscriber' => $request->user()?->hasAcademyCreatorAccess() || $request->user()?->hasAcademyProAccess(),
            ],
        ])->rootView('collections');
    }

    public function show(Request $request, string $slug): Response
    {
        abort_unless((bool) config('academy.enabled', true), 404);

        $lesson = AcademyLesson::query()
            ->with(['category', 'activeBlocks.activeComparisonResults'])
            ->active()
            ->published()
            ->where('slug', $slug)
            ->firstOrFail();

        $payload = $this->access->lessonPayload($lesson, $request->user(), true);
        $courseQuery = AcademyLesson::query()
            ->with('category')
            ->active()
            ->published();

        if (filled($lesson->series_name)) {
            $courseQuery->where('series_name', $lesson->series_name);
        } elseif ($lesson->category_id !== null) {
            $courseQuery->where('category_id', $lesson->category_id);
        } else {
            $courseQuery->whereKey($lesson->id);
        }

        $courseLessons = $courseQuery
            ->orderedForCourse()
            ->get()
            ->filter(fn (AcademyLesson $courseLesson): bool => $this->access->canAccessLesson($request->user(), $courseLesson))
            ->values();

        $currentIndex = $courseLessons->search(fn (AcademyLesson $courseLesson): bool => $courseLesson->is($lesson));
        $previousLesson = is_int($currentIndex) && $currentIndex > 0
            ? $courseLessons->get($currentIndex - 1)
            : null;
        $nextLesson = is_int($currentIndex) && $currentIndex < ($courseLessons->count() - 1)
            ? $courseLessons->get($currentIndex + 1)
            : null;

        $relatedLessons = $courseLessons
            ->reject(fn (AcademyLesson $courseLesson): bool => $courseLesson->is($lesson))
            ->take(6)
            ->map(fn (AcademyLesson $relatedLesson): array => $this->access->lessonPayload($relatedLesson, $request->user()))
            ->values()
            ->all();
        $relatedCourses = AcademyCourse::query()
            ->published()
            ->ordered()
            ->whereHas('courseLessons', fn ($builder) => $builder->where('lesson_id', $lesson->id))
            ->limit(3)
            ->get()
            ->map(fn (AcademyCourse $course): array => $this->access->coursePayload($course, $request->user()))
            ->values()
            ->all();
        $canonical = route('academy.lessons.show', ['slug' => $lesson->slug]);
        $description = Str::limit(trim((string) ($lesson->seo_description ?? $lesson->excerpt ?? 'Skinbase Academy lesson.')), 160, '...');
        $seo = app(SeoFactory::class)->academyLessonPage(
            (string) ($lesson->seo_title ?? ($lesson->title.' — Skinbase Academy')),
            $description,
            $canonical,
            (string) ($payload['article_cover_image_url'] ?? $payload['cover_image_url'] ?? $lesson->cover_image ?? ''),
            [
                ['name' => 'Academy', 'url' => route('academy.index')],
                ['name' => 'Lessons', 'url' => route('academy.lessons.index')],
                ['name' => (string) $lesson->title, 'url' => $canonical],
            ],
            array_values((array) ($payload['tags'] ?? [])),
            $lesson->published_at?->toAtomString(),
            $lesson->updated_at?->toAtomString(),
            (string) ($lesson->series_name ?: $lesson->category?->name ?: 'Academy'),
        )->toArray();

        $interaction = $this->interactions->getInteractionState($request->user(), AcademyAnalyticsContentType::LESSON, (int) $lesson->id);

        return Inertia::render('Academy/Show', [
            'pageType' => 'lesson',
            'item' => $payload,
            'relatedLessons' => $relatedLessons,
            'relatedCourses' => $relatedCourses,
            'previousLesson' => $previousLesson ? $this->access->lessonPayload($previousLesson, $request->user()) : null,
            'nextLesson' => $nextLesson ? $this->access->lessonPayload($nextLesson, $request->user()) : null,
            'seo' => $seo,
            'pricingUrl' => route('academy.pricing'),
            'completeUrl' => $request->user() ? route('academy.lessons.complete', ['lesson' => $lesson->id]) : null,
            'completed' => $request->user()?->academyLessonProgress()->where('lesson_id', $lesson->id)->whereNotNull('completed_at')->exists() ?? false,
            'interaction' => $interaction,
            'interactionRoutes' => [
                'like' => route('academy.interactions.like'),
                'save' => route('academy.interactions.save'),
            ],
            'loginUrl' => route('login'),
            'progressRoutes' => [
                'startLesson' => $request->user() ? route('academy.progress.lesson.start') : null,
            ],
            'analytics' => [
                'enabled' => true,
                'contentType' => AcademyAnalyticsContentType::LESSON,
                'contentId' => (int) $lesson->id,
                'eventUrl' => route('academy.analytics.events.store'),
                'pageName' => 'academy_lesson_show',
                'isPremium' => (string) ($lesson->access_level ?? 'free') !== 'free',
                'isGuest' => $request->user() === null,
                'isSubscriber' => $request->user()?->hasAcademyCreatorAccess() || $request->user()?->hasAcademyProAccess(),
                'isLocked' => (bool) ($payload['locked'] ?? false),
            ],
        ])->rootView('collections');
    }
}
