<?php

declare(strict_types=1);

use App\Models\Category;
use App\Models\ContentType;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Klevze\ControlPanel\Models\Admin\AdminVerification;

uses(RefreshDatabase::class);

function createArtworkBrowserAdmin(): User
{
    $admin = User::factory()->create(['role' => 'admin']);
    $admin->forceFill([
        'isAdmin' => true,
        'activated' => true,
    ])->save();

    AdminVerification::createForUser($admin->fresh());

    return $admin->fresh();
}

it('remembers the selected content type and root scope on the categories browser', function (): void {
    $admin = createArtworkBrowserAdmin();

    $skins = ContentType::query()->create([
        'name' => 'Skins',
        'slug' => 'skins',
        'description' => 'Skin uploads',
        'order' => 1,
    ]);

    $wallpapers = ContentType::query()->create([
        'name' => 'Wallpapers',
        'slug' => 'wallpapers',
        'description' => 'Wallpaper uploads',
        'order' => 2,
    ]);

    $selectedRoot = Category::query()->create([
        'content_type_id' => $skins->id,
        'parent_id' => null,
        'name' => 'Skins Root Selected',
        'slug' => 'skins-root-selected',
        'description' => null,
        'image' => null,
        'is_active' => true,
        'sort_order' => 1,
    ]);

    Category::query()->create([
        'content_type_id' => $skins->id,
        'parent_id' => $selectedRoot->id,
        'name' => 'Skin Child Alpha',
        'slug' => 'skin-child-alpha',
        'description' => null,
        'image' => null,
        'is_active' => true,
        'sort_order' => 1,
    ]);

    Category::query()->create([
        'content_type_id' => $skins->id,
        'parent_id' => $selectedRoot->id,
        'name' => 'Skin Child Beta',
        'slug' => 'skin-child-beta',
        'description' => null,
        'image' => null,
        'is_active' => true,
        'sort_order' => 2,
    ]);

    Category::query()->create([
        'content_type_id' => $wallpapers->id,
        'parent_id' => null,
        'name' => 'Wallpaper Root Hidden',
        'slug' => 'wallpaper-root-hidden',
        'description' => null,
        'image' => null,
        'is_active' => true,
        'sort_order' => 1,
    ]);

    $response = $this->actingAs($admin)->actingAs($admin, 'controlpanel')
        ->get(route('admin.cp.artworks.categories.main', [
            'content_type_id' => $skins->id,
            'root_category_id' => $selectedRoot->id,
        ]));

    $response->assertOk()
        ->assertSessionHas('cp.artworks.categories.filters.content_type_id', (string) $skins->id)
        ->assertSessionHas('cp.artworks.categories.filters.root_category_id', (string) $selectedRoot->id)
        ->assertSee('Subcategories of Skins Root Selected')
        ->assertSee('Skin Child Alpha')
        ->assertSee('Skin Child Beta')
        ->assertDontSee('Wallpaper Root Hidden');
});

it('reorders only the currently selected sibling level', function (): void {
    $admin = createArtworkBrowserAdmin();

    $contentType = ContentType::query()->create([
        'name' => 'Skins',
        'slug' => 'skins',
        'description' => 'Skin uploads',
        'order' => 1,
    ]);

    $firstRoot = Category::query()->create([
        'content_type_id' => $contentType->id,
        'parent_id' => null,
        'name' => 'First Root',
        'slug' => 'first-root',
        'description' => null,
        'image' => null,
        'is_active' => true,
        'sort_order' => 1,
    ]);

    $secondRoot = Category::query()->create([
        'content_type_id' => $contentType->id,
        'parent_id' => null,
        'name' => 'Second Root',
        'slug' => 'second-root',
        'description' => null,
        'image' => null,
        'is_active' => true,
        'sort_order' => 2,
    ]);

    $childOne = Category::query()->create([
        'content_type_id' => $contentType->id,
        'parent_id' => $firstRoot->id,
        'name' => 'Child One',
        'slug' => 'child-one',
        'description' => null,
        'image' => null,
        'is_active' => true,
        'sort_order' => 1,
    ]);

    $childTwo = Category::query()->create([
        'content_type_id' => $contentType->id,
        'parent_id' => $firstRoot->id,
        'name' => 'Child Two',
        'slug' => 'child-two',
        'description' => null,
        'image' => null,
        'is_active' => true,
        'sort_order' => 2,
    ]);

    $otherBranchChild = Category::query()->create([
        'content_type_id' => $contentType->id,
        'parent_id' => $secondRoot->id,
        'name' => 'Other Branch Child',
        'slug' => 'other-branch-child',
        'description' => null,
        'image' => null,
        'is_active' => true,
        'sort_order' => 1,
    ]);

    $this->actingAs($admin)->actingAs($admin, 'controlpanel')
        ->postJson(route('admin.cp.artworks.categories.reorder-tree'), [
            'content_type_id' => $contentType->id,
            'parent_id' => $firstRoot->id,
            'data' => [
                ['id' => $childTwo->id],
                ['id' => $childOne->id],
            ],
        ])
        ->assertOk()
        ->assertJson([
            'status' => 200,
            'message' => 'Category order updated.',
        ]);

    expect($childOne->fresh()->sort_order)->toBe(2)
        ->and($childTwo->fresh()->sort_order)->toBe(1)
        ->and($otherBranchChild->fresh()->sort_order)->toBe(1)
        ->and($otherBranchChild->fresh()->parent_id)->toBe($secondRoot->id);
});
