<?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 Illuminate\Support\Facades\Storage;
use Illuminate\Http\UploadedFile;
use App\Models\Photo;
use App\Models\FileUpload;
use App\Models\User;
use App\Models\InlinePhoto;
use App\Models\TextBlock;
use App\Models\Page;

class InlinePhotoTest extends TestCase
{
    public function test_an_inline_photo_can_be_saved()
    {
        $content_element = $this->createContentElement(TextBlock::factory());
        Storage::fake();
        $file_name = Str::random().'.jpg';
        $file = UploadedFile::fake()->image($file_name);
        $file_upload = (new FileUpload())->saveFile($file, 'photos', true);

        $input['content_element_id'] = $content_element->id;
        $input['photo'] = Photo::factory()->raw();
        $input['photo']['file_upload_id'] = $file_upload->id;

        $this->json('POST', route('inline-photos.create'), $input)
            ->assertStatus(401);

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

        $this->json('POST', route('inline-photos.create'), $input)
            ->assertStatus(403);

        $this->signInAdmin();

        $this->json('POST', route('inline-photos.create'), [])
             ->assertJsonValidationErrors([
                'photo',
                'photo.file_upload_id',
             ]);

        $this->withoutExceptionHandling();
        $this->json('POST', route('inline-photos.create'), $input)
            ->assertSuccessful()
             ->assertJsonFragment([
                'success' => 'Photo Saved',
             ]);

        $inline_photo = InlinePhoto::all()->last();

        $this->assertInstanceOf(InlinePhoto::class, $inline_photo);

        $this->assertNotNull($inline_photo->photo);
        $this->assertInstanceOf(Photo::class, $inline_photo->photo);
        $this->assertEquals(Arr::get($input, 'photo.file_upload_id'), $inline_photo->photo->fileUpload->id);
        $this->assertNotNull($inline_photo->photo->alt);
        $this->assertEquals(Arr::get($input, 'photo.alt'), $inline_photo->photo->alt);

        $this->assertNotNull($inline_photo->contentElement);
        $this->assertEquals($content_element->id, $inline_photo->contentElement->id);
    }

    public function test_an_inline_photo_has_search_results()
    {
        $page = Page::factory()->create();
        $content_element = $this->createContentElement(TextBlock::factory(), $page);
        Storage::fake();
        $file_name = Str::random().'.jpg';
        $file = UploadedFile::fake()->image($file_name);
        $file_upload = (new FileUpload())->saveFile($file, 'photos', true);

        $input['content_element_id'] = $content_element->id;
        $input['photo'] = Photo::factory()->raw();
        $input['photo']['file_upload_id'] = $file_upload->id;

        $this->signInAdmin();
        $this->enableEditing();

        $this->json('POST', route('inline-photos.create'), $input)
            ->assertSuccessful()
             ->assertJsonFragment([
                'success' => 'Photo Saved',
             ]);

        $page->publish();

        $inline_photo = InlinePhoto::all()->last();

        $terms = $inline_photo->photo->name;
        $this->assertNotNull($terms);

        $this->json('POST', route('search'), ['terms' => $terms, 'paginate_count' => 1000, 'filters' => ['pages' => true, 'photos' => true]])
             ->assertSuccessful()
             ->assertJsonFragment([
                 'title' => $content_element->getSearchResultTitle(),
             ]);
    }
}
