#!/bin/bash

tagsProcessorAppUrl()
{
    local full_file_path="$1"
    local app_name="$2"
    local is_public="$3"
    local host_setup="$4"
    local public_ip_v4="$5"

    local traefik_installed=""
    if [[ -f "$docker_dir/$db_file" ]] && command -v sqlite3 >/dev/null 2>&1; then
        traefik_installed=$(runInstallOp sqlite3 "$docker_dir/$db_file" "SELECT name FROM apps WHERE name = 'traefik' AND status = 1;" 2>/dev/null)
    fi

    local app_url=""
    if [[ "$is_public" == "true" && -n "$traefik_installed" && -n "$host_setup" ]]; then
        app_url="https://$host_setup"
    else
        local external_port=""
        if [[ -f "$docker_dir/$db_file" ]] && command -v sqlite3 >/dev/null 2>&1; then
            external_port=$(runInstallOp sqlite3 "$docker_dir/$db_file" "SELECT resource_value FROM network_resources WHERE app_name = '$app_name' AND resource_type = 'port' AND service_name LIKE '%webui%' AND status = 'active' ORDER BY service_name LIMIT 1;" 2>/dev/null | cut -d':' -f1)
        fi
        local host="${public_ip_v4:-localhost}"
        if [[ -n "$external_port" ]]; then
            app_url="http://$host:$external_port"
        else
            app_url="http://$host"
        fi
    fi

    tagsManagerUpdateUniversalTag "$full_file_path" "APP_URL_TAG" "$app_url"
}
