<?php

namespace Tests\Feature\Api\V1\System;

use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\DB;
use Tests\TestCase;

class NavBuilderControllerTest extends TestCase
{
    use RefreshDatabase;

    private function makeAdminUser(): User
    {
        $roleId = DB::table('roles')->insertGetId([
            'name' => 'admin',
            'priority' => 1,
        ]);

        $user = User::query()->create([
            'firstname' => 'Admin',
            'lastname' => 'User',
            'email' => 'admin@example.com',
            'cellphone' => '5555555555',
            'address_street' => '123 Main',
            'city' => 'City',
            'state' => 'CT',
            'zip' => '12345',
            'accept_school_policy' => 1,
            'status' => 'Active',
            'password' => 'hashed',
            'semester' => 'Fall',
        ]);

        DB::table('user_roles')->insert([
            'user_id' => $user->id,
            'role_id' => $roleId,
        ]);

        return $user;
    }

    public function test_menu_returns_items_for_roles(): void
    {
        $roleId = DB::table('roles')->insertGetId([
            'name' => 'parent',
            'priority' => 1,
        ]);

        $user = User::query()->create([
            'firstname' => 'Parent',
            'lastname' => 'User',
            'email' => 'parent@example.com',
            'cellphone' => '5555555555',
            'address_street' => '123 Main',
            'city' => 'City',
            'state' => 'CT',
            'zip' => '12345',
            'accept_school_policy' => 1,
            'status' => 'Active',
            'password' => 'hashed',
            'semester' => 'Fall',
        ]);

        DB::table('user_roles')->insert([
            'user_id' => $user->id,
            'role_id' => $roleId,
        ]);

        $navId = DB::table('nav_items')->insertGetId([
            'label' => 'Home',
            'url' => 'home',
            'sort_order' => 1,
            'is_enabled' => 1,
        ]);

        DB::table('role_nav_items')->insert([
            'role_id' => $roleId,
            'nav_item_id' => $navId,
        ]);

        $response = $this->actingAs($user, 'sanctum')->getJson('/api/v1/nav-builder/menu');

        $response->assertOk();
        $response->assertJsonPath('data.items.0.label', 'Home');
    }

    public function test_data_requires_admin(): void
    {
        $user = $this->makeAdminUser();

        $response = $this->actingAs($user, 'sanctum')->getJson('/api/v1/nav-builder/data');

        $response->assertOk();
        $response->assertJsonStructure(['data' => ['data' => ['items', 'roles', 'parentOptions']]]);
    }

    public function test_store_creates_nav_item(): void
    {
        $user = $this->makeAdminUser();

        $response = $this->actingAs($user, 'sanctum')->postJson('/api/v1/nav-builder', [
            'label' => 'Dashboard',
            'url' => 'dashboard',
            'sort_order' => 1,
            'is_enabled' => true,
            'roles' => [],
        ]);

        $response->assertCreated();
        $this->assertDatabaseHas('nav_items', ['label' => 'Dashboard']);
    }

    public function test_delete_removes_nav_item(): void
    {
        $user = $this->makeAdminUser();

        $navId = DB::table('nav_items')->insertGetId([
            'label' => 'Delete',
            'url' => 'delete',
            'sort_order' => 1,
            'is_enabled' => 1,
        ]);

        $response = $this->actingAs($user, 'sanctum')->deleteJson("/api/v1/nav-builder/{$navId}");

        $response->assertOk();
        $this->assertDatabaseMissing('nav_items', ['id' => $navId]);
    }

    public function test_reorder_updates_items(): void
    {
        $user = $this->makeAdminUser();

        $navId = DB::table('nav_items')->insertGetId([
            'label' => 'Sort',
            'url' => 'sort',
            'sort_order' => 1,
            'is_enabled' => 1,
        ]);

        $response = $this->actingAs($user, 'sanctum')->postJson('/api/v1/nav-builder/reorder', [
            'orders' => [$navId => 9],
        ]);

        $response->assertOk();
        $this->assertDatabaseHas('nav_items', ['id' => $navId, 'sort_order' => 9]);
    }
}
