<?php

namespace Tests\Feature;

use App\Models\Role;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;

class UiStandardizationTest extends TestCase
{
    use RefreshDatabase;

    private User $admin;

    protected function setUp(): void
    {
        parent::setUp();
        $this->seed();
        $this->admin = User::where('email', 'admin@microfinance.local')->firstOrFail();
    }

    public function test_current_list_pages_use_the_shared_table_pattern(): void
    {
        $urls = [
            route('customers.index'),
            route('loan-products.index'),
            route('admin.branches.index'),
            route('admin.users.index'),
            route('admin.roles.index'),
            route('admin.audit.index'),
        ];

        foreach ($urls as $url) {
            $this->actingAs($this->admin)
                ->get($url)
                ->assertOk()
                ->assertSee('data-ui="page-toolbar"', false)
                ->assertSee('data-ui="table-toolbar"', false);
        }
    }

    public function test_filterable_list_pages_render_applied_filter_chips(): void
    {
        $urls = [
            route('customers.index', ['status' => 'active']),
            route('loan-products.index', ['status' => 'active']),
            route('admin.branches.index', ['status' => 'active']),
            route('admin.users.index', ['status' => 'active']),
            route('admin.roles.index', ['search' => 'manager']),
            route('admin.audit.index', ['action' => 'created']),
        ];

        foreach ($urls as $url) {
            $this->actingAs($this->admin)
                ->get($url)
                ->assertOk()
                ->assertSee('data-ui="applied-filter-chips"', false);
        }
    }

    public function test_current_crud_form_pages_use_the_shared_form_pattern(): void
    {
        $editableRole = Role::query()->create([
            'name' => 'ui-standardization-reviewer',
            'guard_name' => 'web',
            'display_name' => 'UI Standardization Reviewer',
            'is_system' => false,
        ]);

        $urls = [
            route('customers.create'),
            route('loan-products.create'),
            route('admin.branches.create'),
            route('admin.users.create'),
            route('admin.business-settings.edit'),
            route('admin.system-settings.edit'),
            route('admin.roles.create'),
            route('admin.roles.edit', $editableRole),
        ];

        foreach ($urls as $url) {
            $this->actingAs($this->admin)
                ->get($url)
                ->assertOk()
                ->assertSee('data-ui="form-shell"', false)
                ->assertSee('data-ui="form-section"', false)
                ->assertSee('class="mf-form-section-body"', false)
                ->assertSee('data-ui="form-actions"', false);
        }
    }
}
