<?php
function vv_local_ip(): string {
    static $ip = null;
    if ($ip !== null) return $ip;
    $ip = trim(shell_exec("ip route get 8.8.8.8 2>/dev/null | awk '/src/{for(i=1;i<=NF;i++)if(\$i==\"src\")print \$(i+1)}'") ?? '');
    if (!$ip) $ip = gethostbyname(gethostname());
    return $ip;
}

function vv_container_webui(string $name, array $portMap): string {
    $template = '/boot/config/plugins/dockerMan/templates-user/my-' . $name . '.xml';
    if (!file_exists($template)) return '';

    $xml = @file_get_contents($template) ?: '';
    if (!preg_match('/<WebUI>(.*?)<\/WebUI>/s', $xml, $m)) return '';

    $url = trim($m[1]);
    if (!$url) return '';

    $url = str_replace('[IP]', vv_local_ip(), $url);

    // [PORT:XXXX] → mapped host port
    $url = preg_replace_callback('/\[PORT:(\d+)\]/', function($pm) use ($name, $portMap) {
        return $portMap[$name][$pm[1]] ?? $pm[1];
    }, $url);

    return $url;
}

function vv_get_docker_folders(): array {
    $folderFile = '/boot/config/plugins/folder.view3/docker.json';
    $folderData = file_exists($folderFile)
        ? (json_decode(@file_get_contents($folderFile), true) ?: [])
        : [];

    // One docker ps call: names, status, port mappings
    $raw = shell_exec("docker ps -a --format '{{.Names}}\t{{.Status}}\t{{.Ports}}' 2>/dev/null") ?? '';
    $statusMap = [];
    $portMap   = [];
    foreach (explode("\n", trim($raw)) as $line) {
        $parts = explode("\t", $line, 3);
        if (count($parts) < 2) continue;
        [$cname, $status, $ports] = array_pad($parts, 3, '');
        $cname = trim($cname);
        if ($cname === '') continue;
        $statusMap[$cname] = trim($status);
        foreach (explode(',', $ports) as $entry) {
            if (preg_match('/(\d+)->(\d+)\/tcp/', trim($entry), $pm)) {
                $portMap[$cname][$pm[2]] = $pm[1]; // containerPort => hostPort
            }
        }
    }

    $folderContainerNames = [];
    $folders = [];

    foreach ($folderData as $id => $f) {
        $containers = [];
        foreach ($f['containers'] ?? [] as $cname) {
            $folderContainerNames[] = $cname;
            $status  = $statusMap[$cname] ?? '';
            $running = str_starts_with($status, 'Up');
            $containers[] = [
                'name'    => $cname,
                'running' => $running,
                'status'  => $status,
                'webui'   => vv_container_webui($cname, $portMap),
            ];
        }
        usort($containers, fn($a, $b) => $b['running'] <=> $a['running'] ?: strcmp($a['name'], $b['name']));

        $folders[] = [
            'id'         => $id,
            'name'       => $f['name'] ?? 'Unnamed',
            'icon'       => $f['icon'] ?? '',
            'containers' => $containers,
        ];
    }
    usort($folders, fn($a, $b) => strcmp($a['name'], $b['name']));

    $ungrouped = [];
    foreach ($statusMap as $cname => $status) {
        if (in_array($cname, $folderContainerNames, true)) continue;
        $running     = str_starts_with($status, 'Up');
        $ungrouped[] = [
            'name'    => $cname,
            'running' => $running,
            'status'  => $status,
            'webui'   => vv_container_webui($cname, $portMap),
        ];
    }
    usort($ungrouped, fn($a, $b) => strcmp($a['name'], $b['name']));

    return [
        'available' => true,
        'folders'   => $folders,
        'ungrouped' => $ungrouped,
    ];
}
