<?php

namespace Tests\Feature;

use App\Models\ReviewImage;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Foundation\Testing\WithFaker;
use Tests\TestCase;

/**
 * ReviewImage Model Feature Tests
 * 
 * Tests for ThrillWiki ReviewImage model functionality
 */
class ReviewImageTest extends TestCase
{
    use RefreshDatabase, WithFaker;

    /**
     * Test model creation.
     */
    public function test_can_create_reviewimage(): void
    {
        $reviewimage = ReviewImage::factory()->create();

        $this->assertDatabaseHas('review_images', [
            'id' => $reviewimage->id,
            'name' => $reviewimage->name,
        ]);
    }

    /**
     * Test model factory.
     */
    public function test_reviewimage_factory_works(): void
    {
        $reviewimage = ReviewImage::factory()->create();

        $this->assertInstanceOf(ReviewImage::class, $reviewimage);
        $this->assertNotEmpty($reviewimage->name);
        $this->assertIsBool($reviewimage->is_active);
    }

    /**
     * Test active scope.
     */
    public function test_active_scope_filters_correctly(): void
    {
        ReviewImage::factory()->active()->create();
        ReviewImage::factory()->inactive()->create();

        $activeCount = ReviewImage::active()->count();
        $totalCount = ReviewImage::count();

        $this->assertEquals(1, $activeCount);
        $this->assertEquals(2, $totalCount);
    }

    /**
     * Test cache key generation.
     */
    public function test_cache_key_generation(): void
    {
        $reviewimage = ReviewImage::factory()->create();

        $cacheKey = $reviewimage->getCacheKey();
        $expectedKey = strtolower('reviewimage') . '.' . $reviewimage->id;

        $this->assertEquals($expectedKey, $cacheKey);
    }

    /**
     * Test cache key with suffix.
     */
    public function test_cache_key_with_suffix(): void
    {
        $reviewimage = ReviewImage::factory()->create();

        $cacheKey = $reviewimage->getCacheKey('details');
        $expectedKey = strtolower('reviewimage') . '.' . $reviewimage->id . '.details';

        $this->assertEquals($expectedKey, $cacheKey);
    }

    /**
     * Test soft deletes.
     */
    public function test_soft_deletes_work(): void
    {
        $reviewimage = ReviewImage::factory()->create();
        $reviewimage->delete();

        $this->assertSoftDeleted($reviewimage);
        
        // Test that it's excluded from normal queries
        $this->assertEquals(0, ReviewImage::count());
        
        // Test that it's included in withTrashed queries
        $this->assertEquals(1, ReviewImage::withTrashed()->count());
    }
}