<?php

if (!defined('ABSPATH')) {
    exit;
}

final class SPP_Mock_Proxmox_Client implements SPP_Proxmox_Client
{
    public function list_templates(): array
    {
        return [
            [
                'provisioningType' => 'qemu',
                'vmId' => 9001,
                'templateRef' => '',
                'storage' => '',
                'name' => 'Turnkey PBX Test Appliance',
                'cpuCores' => 2,
                'memoryMb' => 2048,
                'diskGb' => 24,
                'status' => 'stopped',
            ],
            [
                'provisioningType' => 'qemu',
                'vmId' => 9002,
                'templateRef' => '',
                'storage' => '',
                'name' => 'Windows Support Client',
                'cpuCores' => 4,
                'memoryMb' => 8192,
                'diskGb' => 80,
                'status' => 'stopped',
            ],
            [
                'provisioningType' => 'qemu',
                'vmId' => 9003,
                'templateRef' => '',
                'storage' => '',
                'name' => 'Linux Utility VM',
                'cpuCores' => 2,
                'memoryMb' => 2048,
                'diskGb' => 32,
                'status' => 'stopped',
            ],
            [
                'provisioningType' => 'lxc',
                'vmId' => 0,
                'templateRef' => 'local:vztmpl/debian-12-standard_12.7-1_amd64.tar.zst',
                'storage' => 'local',
                'name' => 'debian-12-standard',
                'cpuCores' => 1,
                'memoryMb' => 1024,
                'diskGb' => 8,
                'status' => 'available',
            ],
            [
                'provisioningType' => 'lxc',
                'vmId' => 0,
                'templateRef' => 'local:vztmpl/ubuntu-24.04-standard_24.04-2_amd64.tar.zst',
                'storage' => 'local',
                'name' => 'ubuntu-24.04-standard',
                'cpuCores' => 1,
                'memoryMb' => 1024,
                'diskGb' => 8,
                'status' => 'available',
            ],
        ];
    }

    public function provision_instance(array $input): array
    {
        $next_id = (int) get_option('spp_mock_next_vm_id', 10000);
        update_option('spp_mock_next_vm_id', $next_id + 1, false);
        update_option('spp_mock_vm_status_' . $next_id, 'stopped', false);
        update_option('spp_mock_vm_type_' . $next_id, $this->normalise_type((string) ($input['provisioning_type'] ?? 'qemu')), false);
        update_option('spp_mock_vm_tags_' . $next_id, is_array($input['tags'] ?? null) ? array_values($input['tags']) : [], false);
        update_option('spp_mock_vm_ips_' . $next_id, [sprintf('192.0.2.%d', (($next_id - 10000) % 200) + 10)], false);

        return ['vm_id' => $next_id];
    }

    public function start_instance(string $type, int $vm_id): void
    {
        $this->ensure_vm($vm_id);
        update_option('spp_mock_vm_status_' . $vm_id, 'running', false);
    }

    public function stop_instance(string $type, int $vm_id): void
    {
        $this->ensure_vm($vm_id);
        update_option('spp_mock_vm_status_' . $vm_id, 'stopped', false);
    }

    public function delete_instance(string $type, int $vm_id): void
    {
        $this->ensure_vm($vm_id);
        delete_option('spp_mock_vm_status_' . $vm_id);
        delete_option('spp_mock_vm_type_' . $vm_id);
        delete_option('spp_mock_vm_tags_' . $vm_id);
        delete_option('spp_mock_vm_ips_' . $vm_id);
    }

    public function get_status(string $type, int $vm_id): string
    {
        return (string) get_option('spp_mock_vm_status_' . $vm_id, 'unknown');
    }

    public function get_ip_addresses(string $type, int $vm_id): array
    {
        $ips = get_option('spp_mock_vm_ips_' . $vm_id, []);

        return is_array($ips) ? array_values(array_map('strval', $ips)) : [];
    }

    private function ensure_vm(int $vm_id): void
    {
        if (get_option('spp_mock_vm_status_' . $vm_id, null) === null) {
            throw new RuntimeException('Mock Proxmox resource does not exist.');
        }
    }

    private function normalise_type(string $type): string
    {
        return strtolower($type) === 'lxc' ? 'lxc' : 'qemu';
    }
}
