<?php

namespace Tests\Feature;

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

use App\Models\VideoText;
use App\Models\EmbedVideo;
use App\Models\User;
use App\Models\Page;

class VideoTextTest extends TestCase
{
    public function test_a_video_text_can_be_removed()
    {
        $page = Page::factory()->create();
        $content_element = $this->createContentElement(EmbedVideo::factory(), $page);
        $embed_video = $content_element->content;
        $this->assertInstanceOf(EmbedVideo::class, $embed_video);

        $video_text = VideoText::factory()->create([
            'embed_video_id' => $embed_video->id,
        ]);

        $this->json('POST', route('video-texts.remove', ['id' => $video_text->id]))
            ->assertStatus(401);

        $user = User::factory()->create();
        $this->signIn($user);

        $this->json('POST', route('video-texts.remove', ['id' => $video_text->id]))
             ->assertStatus(422)
             ->assertJsonValidationErrors([
                'pivot.contentable_id',
                'pivot.contentable_type',
             ]);

        $input = [
            'pivot' => [
                'contentable_id' => $page->id,
                'contentable_type' => $page->type,
            ],
        ];

        $this->json('POST', route('video-texts.remove', ['id' => $video_text->id]), $input)
            ->assertStatus(403);

        $user->createPermission('update', $page);

        $this->json('POST', route('video-texts.remove', ['id' => $video_text->id]), $input)
             ->assertSuccessful();

        $this->assertEquals(0, $embed_video->videoTexts()->count());
    }
}
