<?php

namespace Tests\Feature;

use Illuminate\Support\Str;

use App\Models\Tag;
use App\Models\Page;

trait TagExcludeTestTrait
{
    abstract protected function getModel();
    abstract protected function getClassname();

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

        $input = $this->createContentElement($this->getFactory())->load('content')->toArray();
        $page = Page::factory()->create();
        $input['pivot'] = $this->getContentableArray($page, []);
        $input['content']['tags'] = [
            ['id' => $tag1->id, 'pivot' => [ 'exclude' => false ]],
            ['id' => $tag2->id, 'pivot' => [ 'exclude' => true ]],
        ];

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

        $this->json('POST', route('content-elements.store'), $input)
             ->assertSuccessful()
             ->assertJsonFragment([
                'success' => Str::title(Str::replace('-', ' ', $this->getClassname())).' Saved',
             ]);

        $classname = 'App\\Models\\'.Str::studly($this->getClassname());
        $list = (new $classname())->all()->last();
        $this->assertEquals(2, $list->tags->count());

        $this->assertEquals($tag1->id, $list->tags->first()->id);
        $this->assertNotNull($list->tags->first()->pivot);
        $this->assertEquals(0, $list->tags->first()->pivot->exclude);

        $this->assertEquals($tag2->id, $list->tags->last()->id);
        $this->assertNotNull($list->tags->last()->pivot);
        $this->assertEquals(1, $list->tags->last()->pivot->exclude);
    }
}
