#!/usr/bin/env bash
# Pipeline: clone_project_subrepos — Clona todos los sub-repos Gitea de un project
# (apps + analysis, NO vaults) en el PC actual. Util tras llegar a un PC nuevo.
set -euo pipefail

clone_project_subrepos() {
    local project_id=""
    local owner="dataforge"
    local dry_run=0

    # --- Parseo de argumentos ---
    while [[ $# -gt 0 ]]; do
        case "$1" in
            --owner)
                owner="$2"
                shift 2
                ;;
            --dry-run)
                dry_run=1
                shift
                ;;
            -*)
                echo "[error] Opcion desconocida: $1" >&2
                echo "Uso: clone_project_subrepos <project_id> [--owner OWNER] [--dry-run]" >&2
                return 1
                ;;
            *)
                project_id="$1"
                shift
                ;;
        esac
    done

    if [[ -z "$project_id" ]]; then
        echo "[error] project_id requerido." >&2
        echo "Uso: clone_project_subrepos <project_id> [--owner OWNER] [--dry-run]" >&2
        return 1
    fi

    # --- Resolver paths ---
    local registry_root="${FN_REGISTRY_ROOT:-/home/lucas/fn_registry}"
    local db="$registry_root/registry.db"
    local gitea_url="${GITEA_URL:-https://gitea-dgg044oo04woo4ggcsws4gk0.organic-machine.com}"

    # --- Validar registry.db ---
    if [[ ! -f "$db" ]]; then
        echo "[error] registry.db no encontrado en $db" >&2
        echo "  Asegurate de que FN_REGISTRY_ROOT apunta a la raiz del registry." >&2
        return 1
    fi

    if ! command -v sqlite3 >/dev/null 2>&1; then
        echo "[error] sqlite3 no esta en PATH." >&2
        return 1
    fi

    # --- Validar que el project existe ---
    local project_check
    project_check=$(sqlite3 "$db" "SELECT COUNT(*) FROM projects WHERE id = '$project_id';" 2>/dev/null || echo "0")
    if [[ "$project_check" == "0" ]]; then
        echo "[error] project_id '$project_id' no encontrado en tabla projects." >&2
        echo "  Proyectos disponibles:" >&2
        sqlite3 "$db" "SELECT '  ' || id || ' — ' || COALESCE(description,'') FROM projects;" 2>/dev/null >&2 || true
        return 1
    fi

    echo "=== clone_project_subrepos: project=$project_id ===" >&2
    [[ "$dry_run" -eq 1 ]] && echo "  [DRY-RUN: ninguna accion se ejecutara]" >&2
    echo "  owner=$owner  gitea=$gitea_url" >&2
    echo "" >&2

    # --- Query: apps + analysis del project (NO vaults) ---
    local rows
    rows=$(sqlite3 "$db" \
        "SELECT 'app', id, name, COALESCE(dir_path,''), COALESCE(repo_url,'') FROM apps WHERE project_id = '$project_id'
         UNION ALL
         SELECT 'analysis', id, name, COALESCE(dir_path,''), COALESCE(repo_url,'') FROM analysis WHERE project_id = '$project_id';" \
        2>/dev/null || true)

    if [[ -z "$rows" ]]; then
        echo "[warn] No se encontraron apps ni analysis para project '$project_id'." >&2
        echo "  Verifica que los artefactos tienen project_id='$project_id' en sus .md y que se ha ejecutado fn index." >&2
        return 0
    fi

    # --- Procesar cada fila ---
    local summary_lines=()
    local had_failures=0

    while IFS='|' read -r kind id name dir_path repo_url; do
        [[ -z "$dir_path" ]] && {
            summary_lines+=("$(printf '%-10s %-35s %s' "$kind" "$name" "[skip: dir_path vacio en BD]")")
            continue
        }

        local target_dir="$registry_root/$dir_path"
        local basename_dir
        basename_dir="$(basename "$dir_path")"

        # Construir repo_url si no esta en BD
        if [[ -z "$repo_url" ]]; then
            repo_url="${gitea_url}/${owner}/${basename_dir}"
        fi

        # Comprobar si ya esta clonado
        if [[ -d "$target_dir/.git" ]]; then
            summary_lines+=("$(printf '%-10s %-35s %s' "$kind" "$name" "[skip] ya clonado")")
            continue
        fi

        # Dry-run: solo imprimir
        if [[ "$dry_run" -eq 1 ]]; then
            summary_lines+=("$(printf '%-10s %-35s %s' "$kind" "$name" "[dry-run] git clone $repo_url $target_dir")")
            continue
        fi

        # Crear directorio padre si no existe
        mkdir -p "$(dirname "$target_dir")"

        # Clonar
        local clone_out
        if clone_out=$(git clone --branch master "$repo_url" "$target_dir" 2>&1); then
            summary_lines+=("$(printf '%-10s %-35s %s' "$kind" "$name" "[cloned]")")
        else
            # Detectar error de auth
            local status_msg
            if echo "$clone_out" | grep -qi "authentication\|permission denied\|could not read\|403\|401"; then
                status_msg="[failed] auth (configura SSH o credential helper)"
            else
                status_msg="[failed] $(echo "$clone_out" | tail -1)"
            fi
            summary_lines+=("$(printf '%-10s %-35s %s' "$kind" "$name" "$status_msg")")
            had_failures=1
            echo "  [error] $name: $clone_out" >&2
        fi

    done <<< "$rows"

    # --- Tabla resumen ---
    echo ""
    printf '%-10s %-35s %s\n' "KIND" "NAME" "STATUS"
    printf '%-10s %-35s %s\n' "----------" "-----------------------------------" "-------"
    for line in "${summary_lines[@]}"; do
        echo "$line"
    done

    echo ""
    if [[ "$dry_run" -eq 0 ]]; then
        echo "Siguiente paso sugerido:"
        echo "  cd $registry_root && CGO_ENABLED=1 ./fn index && ./fn sync"
        echo ""
        echo "  fn index  — actualiza registry.db con los nuevos artefactos"
        echo "  fn sync   — sincroniza pc_locations para este PC"
    fi

    if [[ "$had_failures" -eq 1 ]]; then
        return 1
    fi
    return 0
}

clone_project_subrepos "$@"
