<?php

namespace FluxCms\Components\Tests\Feature\Backend;

use FluxCms\Components\Livewire\Backend\BlogEditor;
use FluxCms\Core\Models\BlogPost;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Livewire\Livewire;
use Orchestra\Testbench\TestCase;

class BlogEditorTest extends TestCase
{
    use RefreshDatabase;

    public function test_can_render_blog_editor()
    {
        $post = BlogPost::factory()->create();
        Livewire::test(BlogEditor::class, ['post' => $post])
            ->assertSee($post->title);
    }

    public function test_can_update_post_and_sync_tags()
    {
        $post = BlogPost::factory()->create();
        $post->attachTag('Old Tag');

        Livewire::test(BlogEditor::class, ['post' => $post])
            ->set('post.title', 'Updated Title')
            ->set('tags', 'new tag 1, new tag 2')
            ->call('save');

        $post->refresh();

        $this->assertEquals('Updated Title', $post->title);
        $this->assertCount(2, $post->tags);
        $this->assertEquals(['new tag 1', 'new tag 2'], $post->tags->pluck('name')->toArray());
    }
}
