<?php

namespace Tests\Feature;

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

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

use App\Models\SocialMediaFeed;
use App\Models\ContentElement;
use App\Models\Page;
use App\Models\User;

class SocialMediaFeedTest extends TestCase
{
    use ContentElementsTestTrait;
    use ContentElementsSearchTestTrait;
    use WithFaker;

    protected function getClassname()
    {
        return 'social-media-feed';
    }

    // TODO
    public function an_social_media_feed_can_be_retrieved()
    {
        $this->json('POST', route('social-media.load'))
             ->assertSuccessful();
    }

    public function test_a_social_media_feed_content_element_can_be_created()
    {
        $input = ContentElement::factory()->raw();
        $input['id'] = 0;
        $input['type'] = 'social-media-feed';
        $input['content'] = SocialMediaFeed::factory()->raw();
        //$input['content']['photos'] = [$photo_input];
        $page = Page::factory()->create();
        $input['pivot'] = $this->getContentableArray($page, []);
        /*
            'contentable_id' => $page->id,
            'contentable_type' => get_class($page),
            'sort_order' => 1,
            'unlisted' => false,
            'expandable' => false,
            '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' => 'social-media-feed', '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',
             ]);

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

        $content_element = ContentElement::all()->last();
        $social_media_feed = SocialMediaFeed::all()->last();
        $this->assertEquals(Arr::get($input, 'content.body'), $social_media_feed->body);
        $this->assertEquals(Arr::get($input, 'content.header'), $social_media_feed->header);
        $this->assertEquals($content_element->id, $social_media_feed->contentElement->id);
    }
}
