<?php

namespace Tests\Feature\Filament;

use App\Models\AiModel;
use App\Models\ApiProvider;
use App\Models\Image;
use App\Models\Role;
use App\Models\Style;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;

class ResourcePagesTest extends TestCase
{
    use RefreshDatabase;

    protected Role $adminRole;

    protected Role $standardRole;

    protected User $adminUser;

    protected User $managedUser;

    protected ApiProvider $apiProvider;

    protected AiModel $aiModel;

    protected Style $style;

    protected Image $image;

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

        $this->seedAdminFixtures();
    }

    public function test_admin_resource_index_pages_render(): void
    {
        foreach ($this->indexRoutes() as $route) {
            $this->get($route)->assertOk();
        }
    }

    public function test_admin_resource_create_pages_render(): void
    {
        foreach ($this->createRoutes() as $route) {
            $this->get($route)->assertOk();
        }
    }

    public function test_admin_resource_edit_pages_render(): void
    {
        foreach ($this->editRoutes() as $route) {
            $this->get($route)->assertOk();
        }
    }

    protected function seedAdminFixtures(): void
    {
        $this->adminRole = Role::create(['name' => 'Admin']);
        $this->standardRole = Role::create(['name' => 'User']);

        $this->adminUser = User::factory()->create([
            'role_id' => $this->adminRole->id,
        ]);

        $this->managedUser = User::factory()->create([
            'role_id' => $this->standardRole->id,
            'email' => 'managed@example.com',
        ]);

        $this->actingAs($this->adminUser);

        $this->apiProvider = ApiProvider::create([
            'name' => 'Test Provider',
            'enabled' => true,
            'api_url' => 'https://api.example.com',
        ]);

        $this->aiModel = AiModel::create([
            'name' => 'Test Model',
            'model_id' => 'model-1',
            'model_type' => 'text',
            'api_provider_id' => $this->apiProvider->id,
        ]);

        $this->style = Style::create([
            'title' => 'Test Style',
            'prompt' => 'prompt',
            'description' => 'description',
            'preview_image' => 'style_previews/test.png',
            'ai_model_id' => $this->aiModel->id,
            'enabled' => true,
        ]);

        $this->image = Image::create([
            'path' => 'uploads/test.png',
            'style_id' => $this->style->id,
            'is_public' => true,
        ]);
    }

    protected function indexRoutes(): array
    {
        return [
            route('filament.admin.resources.api-providers.index'),
            route('filament.admin.resources.ai-models.index'),
            route('filament.admin.resources.styles.index'),
            route('filament.admin.resources.images.index'),
            route('filament.admin.resources.users.index'),
            route('filament.admin.resources.roles.index'),
        ];
    }

    protected function createRoutes(): array
    {
        return [
            route('filament.admin.resources.api-providers.create'),
            route('filament.admin.resources.ai-models.create'),
            route('filament.admin.resources.styles.create'),
            route('filament.admin.resources.users.create'),
            route('filament.admin.resources.roles.create'),
        ];
    }

    protected function editRoutes(): array
    {
        return [
            route('filament.admin.resources.api-providers.edit', $this->apiProvider),
            route('filament.admin.resources.ai-models.edit', $this->aiModel),
            route('filament.admin.resources.styles.edit', $this->style),
            route('filament.admin.resources.images.edit', $this->image),
            route('filament.admin.resources.users.edit', $this->managedUser),
            route('filament.admin.resources.roles.edit', $this->standardRole),
        ];
    }
}
