<?php

declare(strict_types=1);

namespace Tests\Feature\Admin;

use App\Http\Middleware\ConditionalValidateCsrfToken;
use App\Models\AcademyChallenge;
use App\Models\AcademyChallengeSubmission;
use App\Models\AcademyCategory;
use App\Models\Artwork;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Cache;
use Inertia\Testing\AssertableInertia;
use Tests\TestCase;

final class AcademyAdminTest extends TestCase
{
    use RefreshDatabase;

    protected function setUp(): void
    {
        parent::setUp();

        $this->withoutMiddleware(ConditionalValidateCsrfToken::class);
    }

    public function test_admin_can_open_academy_dashboard(): void
    {
        $admin = User::factory()->create(['role' => 'admin']);

        $this->actingAs($admin)
            ->get('/moderation/academy/dashboard')
            ->assertOk()
            ->assertInertia(fn (AssertableInertia $page) => $page
                ->component('Admin/Academy/Dashboard')
                ->where('stats.lessons', 0)
                ->where('stats.prompts', 0));
    }

    public function test_admin_can_approve_and_reject_challenge_submission(): void
    {
        $admin = User::factory()->create(['role' => 'admin']);
        $user = User::factory()->create();
        $artwork = Artwork::factory()->create(['user_id' => $user->id]);
        $challenge = AcademyChallenge::query()->create([
            'title' => 'Moderated Challenge',
            'slug' => 'moderated-challenge',
            'access_level' => 'free',
            'status' => 'active',
            'active' => true,
        ]);

        $submission = AcademyChallengeSubmission::query()->create([
            'challenge_id' => $challenge->id,
            'user_id' => $user->id,
            'artwork_id' => $artwork->id,
            'moderation_status' => 'pending',
            'submitted_at' => now(),
        ]);

        $this->from('/moderation/academy/submissions')
            ->actingAs($admin)
            ->post(route('admin.academy.submissions.approve', ['academyChallengeSubmission' => $submission]))
            ->assertRedirect('/moderation/academy/submissions');

        $this->assertDatabaseHas('academy_challenge_submissions', [
            'id' => $submission->id,
            'moderation_status' => 'approved',
        ]);

        $this->from('/moderation/academy/submissions')
            ->actingAs($admin)
            ->post(route('admin.academy.submissions.reject', ['academyChallengeSubmission' => $submission]))
            ->assertRedirect('/moderation/academy/submissions');

        $this->assertDatabaseHas('academy_challenge_submissions', [
            'id' => $submission->id,
            'moderation_status' => 'rejected',
        ]);
    }

    public function test_admin_can_open_all_academy_modules(): void
    {
        $admin = User::factory()->create(['role' => 'admin']);

        foreach ([
            '/moderation/academy/dashboard',
            '/moderation/academy/categories',
            '/moderation/academy/lessons',
            '/moderation/academy/prompts',
            '/moderation/academy/packs',
            '/moderation/academy/challenges',
            '/moderation/academy/submissions',
            '/moderation/academy/badges',
        ] as $path) {
            $this->actingAs($admin)->get($path)->assertOk();
        }
    }

    public function test_admin_category_update_clears_academy_cache(): void
    {
        $admin = User::factory()->create(['role' => 'admin']);
        $category = AcademyCategory::query()->create([
            'type' => 'lesson',
            'name' => 'Prompting Basics',
            'slug' => 'prompting-basics',
            'order_num' => 10,
            'active' => true,
        ]);

        Cache::put('academy.home', ['stale' => true], 600);
        Cache::put('academy.categories.lesson', ['stale' => true], 600);

        $this->actingAs($admin)
            ->patch(route('admin.academy.categories.update', ['academyCategory' => $category]), [
                'type' => 'lesson',
                'name' => 'Prompting Basics Updated',
                'slug' => 'prompting-basics',
                'description' => 'Updated description',
                'icon' => 'fa-wand-magic-sparkles',
                'order_num' => 11,
                'active' => true,
            ])
            ->assertRedirect(route('admin.academy.categories.edit', ['academyCategory' => $category]));

        $this->assertNull(Cache::get('academy.home'));
        $this->assertNull(Cache::get('academy.categories.lesson'));
    }
}