<?php

namespace Tests\Feature;

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

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

    /**
     * Test model creation.
     */
    public function test_can_create_operator(): void
    {
        $operator = Operator::factory()->create();

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

    /**
     * Test model factory.
     */
    public function test_operator_factory_works(): void
    {
        $operator = Operator::factory()->create();

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

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

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

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

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

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

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

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

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

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

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

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