<?php

namespace Tests\Feature\Api;

use App\Models\Device;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;

class DeviceModelTest extends TestCase
{
    use RefreshDatabase;

    public function test_device_belongs_to_user(): void
    {
        $user = User::factory()->create();
        $device = Device::create([
            'user_id' => $user->id,
            'name' => 'Test PC',
            'hostname' => 'TEST-PC',
            'os_info' => 'Windows 11',
            'local_ip' => '192.168.1.10',
            'device_token_hash' => str_repeat('a', 64),
        ]);

        $this->assertSame($user->id, $device->user->id);
    }

    public function test_device_is_pairable_when_unclaimed_and_not_expired(): void
    {
        $device = Device::create([
            'name' => 'Pending',
            'hostname' => 'PENDING',
            'pairing_code' => '123-456',
            'pairing_expires_at' => now()->addMinutes(5),
            'device_token_hash' => str_repeat('b', 64),
        ]);

        $this->assertTrue($device->isPairable());
    }

    public function test_device_is_not_pairable_when_expired(): void
    {
        $device = Device::create([
            'name' => 'Stale',
            'hostname' => 'STALE',
            'pairing_code' => '999-999',
            'pairing_expires_at' => now()->subMinute(),
            'device_token_hash' => str_repeat('c', 64),
        ]);

        $this->assertFalse($device->isPairable());
    }
}
