<?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\AcademyInteractionService;
use App\Services\Academy\AcademyCourseNavigationService;
use App\Services\Academy\AcademyCourseProgressService;
use App\Support\AcademyAnalytics\AcademyAnalyticsContentType;
use App\Support\Seo\SeoFactory;
use Illuminate\Support\Str;
use Illuminate\Http\Request;
use Inertia\Inertia;
use Inertia\Response;

final class AcademyCourseLessonController extends Controller
{
    public function __construct(
        private readonly AcademyAccessService $access,
        private readonly AcademyCourseNavigationService $navigation,
        private readonly AcademyCourseProgressService $progress,
        private readonly AcademyInteractionService $interactions,
    ) {
    }

    public function show(Request $request, AcademyCourse $course, AcademyLesson $lesson): Response
    {
        abort_unless((bool) config('academy.enabled', true), 404);
        abort_unless($course->isPublished(), 404);

        $course->load(['sections', 'courseLessons.section', 'courseLessons.lesson.category']);
        $courseLesson = $this->navigation->findCourseLesson($course, $lesson);

        abort_unless($courseLesson instanceof \App\Models\AcademyCourseLesson, 404);

        if ($request->user()) {
            $this->progress->updateLastLesson($request->user(), $course, $lesson);
            $this->progress->markCourseCompletedIfFinished($request->user(), $course);
        }

        $progress = $this->progress->getProgress($request->user(), $course);
        $previousLesson = $this->navigation->previousLesson($course, $lesson);
        $nextLesson = $this->navigation->nextLesson($course, $lesson);
        $courseOutline = $this->navigation->orderedCourseLessons($course)
            ->map(fn (\App\Models\AcademyCourseLesson $entry): array => $this->access->courseLessonPayload($entry, $request->user()))
            ->values()
            ->all();

        $payload = $this->access->courseLessonPayload($courseLesson, $request->user(), true);
        $canonical = route('academy.courses.lessons.show', ['course' => $course->slug, 'lesson' => $lesson->slug]);
        $description = Str::limit(trim((string) ($lesson->seo_description ?? $lesson->excerpt ?? 'Skinbase Academy course lesson.')), 160, '...');
        $seo = app(SeoFactory::class)->academyLessonPage(
            (string) ($lesson->seo_title ?? ($lesson->title . ' — ' . $course->title)),
            $description,
            $canonical,
            (string) ($payload['article_cover_image_url'] ?? $payload['cover_image_url'] ?? $lesson->cover_image ?? ''),
            [
                ['name' => 'Academy', 'url' => route('academy.index')],
                ['name' => 'Courses', 'url' => route('academy.courses.index')],
                ['name' => (string) $course->title, 'url' => route('academy.courses.show', ['course' => $course->slug])],
                ['name' => (string) $lesson->title, 'url' => $canonical],
            ],
            array_values((array) ($payload['tags'] ?? [])),
            $lesson->published_at?->toAtomString(),
            $lesson->updated_at?->toAtomString(),
            (string) $course->title,
        )->toArray();

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

        return Inertia::render('Academy/Show', [
            'pageType' => 'lesson',
            'item' => $payload,
            'relatedLessons' => [],
            'relatedCourses' => [],
            'previousLesson' => $previousLesson ? $this->access->courseLessonPayload($previousLesson, $request->user()) : null,
            'nextLesson' => $nextLesson ? $this->access->courseLessonPayload($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_course_lesson_show',
                'isPremium' => (string) ($payload['access_level'] ?? 'free') !== 'free',
                'isGuest' => $request->user() === null,
                'isSubscriber' => $request->user()?->hasAcademyCreatorAccess() || $request->user()?->hasAcademyProAccess(),
                'isLocked' => (bool) ($payload['locked'] ?? false),
            ],
            'courseContext' => [
                'id' => (int) $course->id,
                'title' => (string) $course->title,
                'slug' => (string) $course->slug,
                'subtitle' => (string) ($course->subtitle ?? ''),
                'showUrl' => route('academy.courses.show', ['course' => $course->slug]),
                'completePayload' => ['course_id' => $course->id],
                'progress' => [
                    'percent' => (int) ($progress['progress_percent'] ?? 0),
                    'completedRequired' => (int) ($progress['completed_required'] ?? 0),
                    'totalRequired' => (int) ($progress['total_required'] ?? 0),
                    'completed' => (bool) ($progress['completed'] ?? false),
                ],
                'outline' => $courseOutline,
            ],
        ])->rootView('collections');
    }
}