/**
 * Generic single-task keep-alive for tool pages.
 * Persists task state to localStorage so in-progress tasks survive page switches.
 */

import { waitForTask } from "../../api/taskSubscription";

const KEEPALIVE_PREFIX = "omniai:tool-task:";

interface ToolTaskKeepalive {
  taskId: string;
  resultUrl: string;
  resultPreview: string;
  status: string;
  progress: number;
  sourceName: string;
  sourceUrl: string;
  savedAt: number;
}

export function saveToolTaskState(key: string, state: {
  taskId: string;
  resultUrl?: string;
  resultPreview?: string;
  status?: string;
  progress?: number;
  sourceName?: string;
  sourceUrl?: string;
}): void {
  if (!state.taskId) return;
  try {
    const entry: ToolTaskKeepalive = {
      taskId: state.taskId,
      resultUrl: state.resultUrl || "",
      resultPreview: state.resultPreview || "",
      status: state.status || "",
      progress: state.progress || 0,
      sourceName: state.sourceName || "",
      sourceUrl: state.sourceUrl || "",
      savedAt: Date.now(),
    };
    window.localStorage.setItem(KEEPALIVE_PREFIX + key, JSON.stringify(entry));
  } catch { /* quota */ }
}

export function loadToolTaskState(key: string): ToolTaskKeepalive | null {
  try {
    const raw = window.localStorage.getItem(KEEPALIVE_PREFIX + key);
    if (!raw) return null;
    const parsed = JSON.parse(raw) as ToolTaskKeepalive;
    if (Date.now() - (parsed.savedAt || 0) > 2 * 60 * 60 * 1000) {
      clearToolTaskState(key);
      return null;
    }
    if (!parsed.taskId) return null;
    return parsed;
  } catch { return null; }
}

export function clearToolTaskState(key: string): void {
  try { window.localStorage.removeItem(KEEPALIVE_PREFIX + key); } catch { /* ignore */ }
}

export async function pollTaskUntilDone(
  taskId: string,
  onProgress?: (progress: number) => void,
  abortRef?: { current: boolean },
  kind: "image" | "video" = "video",
): Promise<string | null> {
  try {
    return await waitForTask(taskId, {
      kind,
      abortRef,
      onProgress: (event) => onProgress?.(Math.min(99, Number(event.progress || 0))),
    });
  } catch {
    return null;
  }
}
