<?php

use PHPUnit\Framework\TestCase;
use SHServ\Entities\Device;
use SHServ\Entities\DeviceAuth;

class DeviceAuthCacheTest extends TestCase {
	private $tb;

	protected function setUp(): void {
		$this -> tb = app() -> thin_builder;
		$this -> create_tables();
	}

	protected function tearDown(): void {
		$this -> tb -> query("DROP TABLE IF EXISTS devices");
		$this -> tb -> query("DROP TABLE IF EXISTS device_auth");
	}

	private function create_tables(): void {
		$this -> tb -> query("CREATE TABLE devices (
			id INTEGER PRIMARY KEY AUTOINCREMENT,
			area_id INTEGER DEFAULT 0,
			alias VARCHAR(255),
			name VARCHAR(255),
			device_type VARCHAR(50),
			device_ip VARCHAR(50),
			device_mac VARCHAR(50),
			device_hard_id VARCHAR(255),
			firmware_version VARCHAR(50),
			connection_status VARCHAR(50),
			status VARCHAR(50),
			description TEXT,
			last_contact DATETIME,
			create_at DATETIME,
			update_at DATETIME
		)");

		$this -> tb -> query("CREATE TABLE device_auth (
			id INTEGER PRIMARY KEY AUTOINCREMENT,
			device_id INTEGER,
			device_token VARCHAR(255),
			status VARCHAR(50),
			create_at DATETIME
		)");
	}

	public function test_device_auth_cache_cleared_after_kill(): void {
		$this -> tb -> insert('devices', [
			'alias' => 'test_dev',
			'name' => 'Test',
			'device_type' => 'relay',
			'device_ip' => '192.168.1.10',
			'device_hard_id' => 'dev_123',
			'connection_status' => 'online',
			'status' => 'active',
			'create_at' => date('Y-m-d H:i:s'),
			'update_at' => date('Y-m-d H:i:s'),
		]);

		$this -> tb -> insert('device_auth', [
			'device_id' => 1,
			'device_token' => 'tok_abc',
			'status' => 'active',
			'create_at' => date('Y-m-d H:i:s'),
		]);

		$device = new Device(1);
		$auth = $device -> auth();
		$this -> assertNotNull($auth);
		$this -> assertTrue($auth -> is_active());

		$auth -> kill();

		// After kill, the device's auth() should reflect the killed state
		$device -> clear_device_auth_cache();
		$new_auth = $device -> auth();
		$this -> assertNotNull($new_auth);
		$this -> assertSame('killed', $new_auth -> status);
		$this -> assertFalse($new_auth -> is_active());
	}

	public function test_kill_with_device_reference_clears_cache(): void {
		$this -> tb -> insert('devices', [
			'alias' => 'test_dev2',
			'name' => 'Test',
			'device_type' => 'relay',
			'device_ip' => '192.168.1.11',
			'device_hard_id' => 'dev_124',
			'connection_status' => 'online',
			'status' => 'active',
			'create_at' => date('Y-m-d H:i:s'),
			'update_at' => date('Y-m-d H:i:s'),
		]);

		$this -> tb -> insert('device_auth', [
			'device_id' => 1,
			'device_token' => 'tok_def',
			'status' => 'active',
			'create_at' => date('Y-m-d H:i:s'),
		]);

		$device = new Device(1);
		$auth = $device -> auth();

		// Verify the DeviceAuth was constructed with the device reference
		$auth -> kill();

		// Since kill() calls clear_device_auth_cache() via the device reference,
		// the next auth() call should fetch fresh data from DB
		$fresh_auth = $device -> auth();
		$this -> assertSame('killed', $fresh_auth -> status);
	}
}
