<?php

namespace Tests\Feature;

use App\Filament\Resources\EmotionResource\Pages\ImportEmotions;
use App\Models\Emotion;
use App\Models\User;
use Illuminate\Http\UploadedFile;
use Illuminate\Support\Facades\Storage;
use Livewire\Livewire;
use Tests\TestCase;

class EmotionResourceTest extends TestCase
{
    public function test_import_emotions_csv()
    {
        Storage::fake('public');

        $user = User::factory()->create();
        $this->actingAs($user);

        $csvData = "name_de,name_en,icon,color,description_de,description_en,sort_order,is_active,event_types\nGlück,Joy,😊,#FFD700,Gefühl des Glücks,Feeling of joy,1,1,wedding|birthday";
        $csvFile = UploadedFile::fake()->createWithContent('emotions.csv', $csvData);

        Livewire::test(ImportEmotions::class)
            ->set('file', $csvFile->getRealPath())
            ->call('doImport');

        $this->assertDatabaseHas('emotions', [
            'name' => json_encode(['de' => 'Glück', 'en' => 'Joy']),
            'icon' => '😊',
            'color' => '#FFD700',
            'description' => json_encode(['de' => 'Gefühl des Glücks', 'en' => 'Feeling of joy']),
            'sort_order' => 1,
            'is_active' => 1,
        ]);

        $emotion = Emotion::where('name->de', 'Glück')->first();
        $this->assertNotNull($emotion);
    }
}
