<?php

namespace Tests\Feature;

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

use Tests\Feature\ContentElementsTestTrait;
use Tests\Feature\TagExcludeTestTrait;
use Tests\Feature\ContentElementsSearchTestTrait;

use App\Models\Tag;
use App\Models\Page;
use App\Models\User;
use App\Models\LivestreamList;

class LivestreamListTest extends TestCase
{
    use ContentElementsTestTrait;
    use WithFaker;
    use TagExcludeTestTrait;
    use ContentElementsSearchTestTrait;

    protected function getClassname()
    {
        return 'livestream-list';
    }

    public function test_a_livestream_list_can_be_created()
    {
        $tag1 = Tag::factory()->create();
        $tag2 = Tag::factory()->create();

        $input = $this->createContentElement(LivestreamList::factory())->toArray();
        $input['id'] = 0;
        $input['content_id'] = 0;
        $input['content_type'] = null;
        $input['content.id'] = 0;
        $input['content']['tags'] = [
            $tag1,
            $tag2,
        ];

        $page = Page::factory()->create();
        $input['pivot'] = $this->getContentableArray($page, []);
        /*
            'contentable_id' => $page->id,
            'contentable_type' => get_class($page),
            'sort_order' => 1,
            'unlisted' => false,
            'expandable' => null,
            'guest' => false,
        ];
         */

        $this->json('POST', route('content-elements.store'), [])
            ->assertStatus(401);

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

        $this->json('POST', route('content-elements.store'), [])
             ->assertStatus(422)
             ->assertJsonValidationErrors([
                 'pivot.contentable_id',
                 'pivot.contentable_type',
             ]);

        $this->json('POST', route('content-elements.store'), ['pivot' => ['contentable_id' => $page->id, 'contentable_type' => 'page']])
             ->assertStatus(403);

        $this->signInAdmin();

        $this->json('POST', route('content-elements.store'), ['pivot' => ['contentable_id' => $page->id, 'contentable_type' => 'page']])
             ->assertStatus(422)
             ->assertJsonValidationErrors([
                 'type',
             ]);

        $this->json('POST', route('content-elements.store'), ['type' => 'livestream-list', 'pivot' => ['contentable_id' => $page->id, 'contentable_type' => 'page']])
             ->assertStatus(422)
             ->assertJsonValidationErrors([
                'pivot.sort_order',
                'pivot.unlisted',
                //'pivot.expandable',
                'pivot.guest',
                'pivot.no_margin',
                'pivot.randomize',
             ]);

        /*
         * We dont have any required fields so we dont need this part of the test
        $content = $input['content'];
        $input['content'] = [];

        $this->json('POST', route('content-elements.store'), $input)
             ->assertStatus(422)
             ->assertJsonValidationErrors([
                'content.show_student_info',
                'content.show_interests',
                'content.show_livestreams',
                'content.show_livestreams_first',
                'content.create_password',
             ]);

        $input['content'] = $content;
         */

        $this->withoutExceptionHandling();
        $this->json('POST', route('content-elements.store'), $input)
             ->assertSuccessful()
             ->assertJsonFragment([
                'success' => 'Livestream List Saved',
             ]);

        $livestream_index = LivestreamList::all()->last();
        $this->assertEquals(Arr::get($input, 'content.header'), $livestream_index->header);
        $this->assertTrue($livestream_index->tags->contains('id', $tag1->id));
        $this->assertTrue($livestream_index->tags->contains('id', $tag2->id));
        $this->assertEquals(1, $livestream_index->show_past);
        $this->assertEquals(1, $livestream_index->show_future);
    }
}
