<?php

namespace Tests\Feature;

use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Foundation\Testing\WithFaker;
use Tests\TestCase;
use Illuminate\View\View;

use App\Models\TextBlock;
use App\Models\Page;
use App\Models\Livestream;

use App\Utilities\PageResponse;

class PageResponseTest extends TestCase
{
    public function test_a_page_response_can_be_created()
    {
        $content_element = $this->createContentElement(TextBlock::factory(), Page::factory()->create());
        $page = $content_element->pages()->first();

        $this->assertInstanceOf(Page::class, $page);

        $page->publish();
        $page->refresh();

        $response = (new PageResponse())->view($page, 'pages.view');

        $this->assertInstanceOf(View::class, $response);

        $this->withoutExceptionHandling();
        $this->get($page->full_slug)
             ->assertSuccessful()
             ->assertViewIs('pages.view')
             ->assertViewHas('page', $page);

        $this->signInAdmin();
    }

    public function test_a_page_response_can_be_created_and_a_url_passed_for_the_browser()
    {
        $livestream = Livestream::factory()->create();
        $page = (new Page())->findByFullSlug('live/view');
        $this->assertInstanceOf(Page::class, $page);

        $this->get(route('livestreams.view', ['id' => $livestream]))
             ->assertSuccessful()
             ->assertViewHas([
                'slug' => 'livestreams/'.$livestream->id,
            ]);
    }
}
