<?php

namespace Tests\Feature;

use App\Models\News;
use App\Models\Programme;
use App\Models\Role;
use App\Models\School;
use Database\Seeders\CmsSeeder;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;

/**
 * Every page a real visitor, applicant or member of staff can reach should
 * render without error. This is the cheap net that catches a broken view
 * before anyone else does.
 */
class PageRenderTest extends TestCase
{
    use RefreshDatabase;

    protected function setUp(): void
    {
        parent::setUp();
        $this->seedPlatform();
        $this->seed(CmsSeeder::class);
    }

    public function test_every_public_page_renders(): void
    {
        $urls = [
            '/',
            '/programmes',
            '/academics',
            '/admissions',
            '/admissions/requirements',
            '/admissions/faqs',
            '/news',
            '/events',
            '/contact',
            '/about',
            '/vice-chancellors-message',
            '/student-life',
            '/research',
            '/applicant/login',
            '/applicant/register',
            '/forgot-password',
            route('programmes.show', Programme::first()),
            route('schools.show', School::first()),
            route('news.show', News::published()->first()),
            route('events.show', \App\Models\Event::published()->first()),
        ];

        foreach ($urls as $url) {
            $this->get($url)->assertOk();
        }
    }

    public function test_the_homepage_carries_the_university_identity(): void
    {
        $this->get('/')
            ->assertOk()
            ->assertSee('King Ceasor University')
            ->assertSee('Celebrating Excellence')
            ->assertSee('School of Medicine, Health &amp; Life Sciences', false)
            ->assertSee('Veritas');
    }

    public function test_the_programme_catalogue_can_be_searched_and_filtered(): void
    {
        $this->get('/programmes?q=nursing')
            ->assertOk()
            ->assertSee('Bachelor of Science in Nursing')
            ->assertDontSee('Bachelor of Laws');

        $this->get('/programmes?level=diploma')
            ->assertOk()
            ->assertSee('Diploma in Clinical Medicine and Community Health');
    }

    public function test_every_applicant_page_renders(): void
    {
        $applicant = $this->completeApplicant();
        $application = $this->startApplication($applicant);

        $urls = [
            route('applicant.dashboard'),
            route('applicant.applications.index'),
            route('applicant.applications.show', $application),
            route('applicant.notifications'),
            route('applicant.profile'),
            route('applicant.payments.show', $application),
        ];

        foreach ($urls as $url) {
            $this->actingAs($applicant->user)->get($url)->assertOk();
        }
    }

    public function test_every_wizard_step_renders(): void
    {
        $applicant = $this->completeApplicant();
        $application = $this->startApplication($applicant);

        foreach (\App\Enums\ApplicationStep::cases() as $step) {
            $this->actingAs($applicant->user)
                ->get(route('applicant.applications.wizard', [$application, $step->value]))
                ->assertOk();
        }
    }

    public function test_every_admin_page_renders(): void
    {
        $admin = $this->staff(Role::SUPER_ADMIN);
        $application = $this->startApplication($this->completeApplicant());

        $urls = [
            '/admin',
            route('admin.applications.index'),
            route('admin.applications.show', $application),
            route('admin.admissions.index'),
            route('admin.payments.index'),
            route('admin.programmes.index'),
            route('admin.programmes.edit', Programme::first()),
            route('admin.intakes.index'),
            route('admin.reports.index'),
            route('admin.settings.index'),
            route('admin.audit-logs'),
        ];

        foreach ($urls as $url) {
            $this->actingAs($admin)->get($url)->assertOk();
        }
    }

    public function test_an_unknown_page_returns_a_not_found_response(): void
    {
        $this->get('/no-such-page')->assertNotFound();
    }
}
