<?php

declare(strict_types=1);

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

uses(RefreshDatabase::class);

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

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

    return $admin->fresh();
}

it('stores plain ampersands when categories are updated in control panel', function (): void {
    $admin = createArtworkTaxonomyAdmin();

    $contentType = ContentType::query()->create([
        'name' => 'Digital Art',
        'slug' => 'digital-art',
        'description' => 'Digital art uploads',
        'order' => 1,
    ]);

    $category = Category::query()->create([
        'content_type_id' => $contentType->id,
        'parent_id' => null,
        'name' => 'Anime',
        'slug' => 'anime',
        'description' => 'Initial description',
        'image' => null,
        'is_active' => true,
        'sort_order' => 1,
    ]);

    $this->actingAs($admin)->actingAs($admin, 'controlpanel')
        ->post(route('admin.cp.artworks.categories.update', $category->id), [
            'content_type_id' => $contentType->id,
            'parent_id' => '',
            'name' => 'Anime & Manga',
            'slug' => 'anime',
            'description' => 'Anime & Manga artwork',
            'image' => '',
            'sort_order' => 1,
            'is_active' => '1',
        ])
        ->assertRedirect(route('admin.cp.artworks.categories.main'));

    $category->refresh();

    expect(DB::table('categories')->where('id', $category->id)->value('name'))->toBe('Anime & Manga')
        ->and(DB::table('categories')->where('id', $category->id)->value('description'))->toBe('Anime & Manga artwork')
        ->and($category->name)->toBe('Anime & Manga')
        ->and($category->description)->toBe('Anime & Manga artwork');
});

it('renders legacy encoded category names decoded in control panel edit form', function (): void {
    $admin = createArtworkTaxonomyAdmin();

    $contentTypeId = DB::table('content_types')->insertGetId([
        'name' => 'Digital Art',
        'slug' => 'digital-art',
        'description' => 'Digital art uploads',
        'order' => 1,
        'created_at' => now(),
        'updated_at' => now(),
    ]);

    $categoryId = DB::table('categories')->insertGetId([
        'content_type_id' => $contentTypeId,
        'parent_id' => null,
        'name' => 'Anime &amp;amp; Manga',
        'slug' => 'anime-manga',
        'description' => 'Anime &amp;amp; Manga artwork',
        'image' => null,
        'is_active' => true,
        'sort_order' => 1,
        'created_at' => now(),
        'updated_at' => now(),
    ]);

    $html = $this->actingAs($admin)->actingAs($admin, 'controlpanel')
        ->get(route('admin.cp.artworks.categories.edit', $categoryId))
        ->assertOk()
        ->getContent();

    expect($html)
        ->toContain('value="Anime &amp; Manga"')
        ->not->toContain('value="Anime &amp;amp; Manga"')
        ->toContain('Anime &amp; Manga artwork')
        ->not->toContain('Anime &amp;amp; Manga artwork');
});