<?php
namespace Tests\Feature\Filament;

use App\Filament\Resources\CampaignResource\Pages\CreateCampaign;
use App\Filament\Resources\CampaignResource\Pages\EditCampaign;
use App\Filament\Resources\CampaignResource\Pages\ListCampaigns;
use App\Filament\Resources\CategoryResource\Pages\CreateCategory;
use App\Filament\Resources\CategoryResource\Pages\ListCategories;
use App\Filament\Resources\NewsArticleResource\Pages\CreateNewsArticle;
use App\Filament\Resources\NewsArticleResource\Pages\ListNewsArticles;
use App\Filament\Resources\ProductResource\Pages\CreateProduct;
use App\Filament\Resources\ProductResource\Pages\EditProduct;
use App\Filament\Resources\ProductResource\Pages\ListProducts;
use App\Models\Brand;
use App\Models\Campaign;
use App\Models\Category;
use App\Models\NewsArticle;
use App\Models\Product;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Livewire\Livewire;
use Tests\TestCase;

class ContentResourceTest extends TestCase
{
    use RefreshDatabase;

    protected function setUp(): void
    {
        parent::setUp();
        $this->actingAs(User::factory()->create());
    }

    public function test_can_list_categories(): void
    {
        Category::factory()->count(3)->create();

        Livewire::test(ListCategories::class)
            ->assertCanSeeTableRecords(Category::all());
    }

    public function test_can_create_category(): void
    {
        Livewire::test(CreateCategory::class)
            ->fillForm([
                'name' => 'Novo Açúcar',
                'slug' => 'novo-acucar',
                'show_in_hero' => true,
                'sort_order' => 10,
                'is_active' => true,
            ])
            ->call('create')
            ->assertHasNoFormErrors();

        $this->assertDatabaseHas('categories', ['name' => 'Novo Açúcar']);
    }

    public function test_can_list_products(): void
    {
        $category = Category::factory()->create();
        Product::factory()->count(3)->create(['category_id' => $category->id]);

        Livewire::test(ListProducts::class)
            ->assertCanSeeTableRecords(Product::all());
    }

    public function test_can_create_product(): void
    {
        $category = Category::factory()->create();
        $brand = Brand::factory()->create();

        Livewire::test(CreateProduct::class)
            ->fillForm([
                'category_id' => $category->id,
                'brand_id' => $brand->id,
                'name' => 'Produto Teste',
                'slug' => 'produto-teste',
                'price' => 1000.00,
                'is_active' => true,
            ])
            ->call('create')
            ->assertHasNoFormErrors();

        $this->assertDatabaseHas('products', ['name' => 'Produto Teste']);
    }

    public function test_can_edit_product(): void
    {
        $category = Category::factory()->create();
        $product = Product::factory()->create(['category_id' => $category->id]);

        Livewire::test(EditProduct::class, ['record' => $product->id])
            ->fillForm(['price' => 2500.00])
            ->call('save')
            ->assertHasNoFormErrors();

        $this->assertDatabaseHas('products', [
            'id' => $product->id,
            'price' => 2500.00,
        ]);
    }

    public function test_can_list_campaigns(): void
    {
        Campaign::factory()->count(3)->create();

        Livewire::test(ListCampaigns::class)
            ->assertCanSeeTableRecords(Campaign::all());
    }

    public function test_can_create_campaign(): void
    {
        Livewire::test(CreateCampaign::class)
            ->fillForm([
                'name' => 'Campanha Teste',
                'slug' => 'campanha-teste',
                'objective' => 'Testar a criação de campanhas',
                'status' => 'draft',
            ])
            ->call('create')
            ->assertHasNoFormErrors();

        $this->assertDatabaseHas('campaigns', ['name' => 'Campanha Teste']);
    }

    public function test_can_edit_campaign(): void
    {
        $campaign = Campaign::factory()->create();

        Livewire::test(EditCampaign::class, ['record' => $campaign->id])
            ->fillForm(['objective' => 'Objectivo actualizado'])
            ->call('save')
            ->assertHasNoFormErrors();

        $this->assertDatabaseHas('campaigns', [
            'id' => $campaign->id,
            'objective' => 'Objectivo actualizado',
        ]);
    }

    public function test_can_list_news_articles(): void
    {
        NewsArticle::factory()->count(3)->create(['status' => 'published']);

        Livewire::test(ListNewsArticles::class)
            ->assertCanSeeTableRecords(NewsArticle::all());
    }

    public function test_can_create_news_article(): void
    {
        Livewire::test(CreateNewsArticle::class)
            ->fillForm([
                'title' => 'Notícia Teste',
                'slug' => 'noticia-teste',
                'content' => 'Conteúdo da notícia de teste.',
                'category' => 'Notícias',
                'status' => 'draft',
            ])
            ->call('create')
            ->assertHasNoFormErrors();

        $this->assertDatabaseHas('news_articles', ['title' => 'Notícia Teste']);
    }

    public function test_product_validation_requires_price(): void
    {
        $category = Category::factory()->create();

        Livewire::test(CreateProduct::class)
            ->fillForm([
                'category_id' => $category->id,
                'name' => 'Produto Sem Preço',
                'slug' => 'produto-sem-preco',
            ])
            ->call('create')
            ->assertHasFormErrors(['price' => 'required']);
    }

    public function test_create_validation_requires_content(): void
    {
        Livewire::test(CreateNewsArticle::class)
            ->fillForm([
                'title' => 'Notícia Sem Conteúdo',
            ])
            ->call('create')
            ->assertHasFormErrors(['content']);
    }
}
