import type {
  EcommerceVideoStage,
  EcommerceVideoSceneTask,
  EcommerceVideoPlanProgress,
  EcommerceVideoPlanResult,
  PlanStep,
} from "./ecommerceVideoTypes";

const KEEPALIVE_KEY = "omniai:ecommerce-video-workspace";

interface EcommerceVideoKeepalive {
  inputFingerprint: string;
  stage: EcommerceVideoStage;
  completedSteps: PlanStep[];
  planResult: EcommerceVideoPlanResult | null;
  planProgress?: EcommerceVideoPlanProgress | null;
  scenes: EcommerceVideoSceneTask[];
  sourceImageUrls: string[];
  savedAt: number;
}

export function saveEcommerceVideoState(state: {
  inputFingerprint: string;
  stage: EcommerceVideoStage;
  completedSteps: PlanStep[];
  planResult: EcommerceVideoPlanResult | null;
  planProgress?: EcommerceVideoPlanProgress | null;
  scenes: EcommerceVideoSceneTask[];
  sourceImageUrls?: string[];
}): void {
  try {
    const entry: EcommerceVideoKeepalive = {
      ...state,
      sourceImageUrls: state.sourceImageUrls || [],
      savedAt: Date.now(),
    };
    window.localStorage.setItem(KEEPALIVE_KEY, JSON.stringify(entry));
  } catch {
    // quota exceeded — silently drop
  }
}

export function loadEcommerceVideoState(inputFingerprint: string): EcommerceVideoKeepalive | null {
  try {
    const raw = window.localStorage.getItem(KEEPALIVE_KEY);
    if (!raw) return null;
    const parsed = JSON.parse(raw) as EcommerceVideoKeepalive;
    // Discard entries older than 2 hours
    if (Date.now() - (parsed.savedAt || 0) > 2 * 60 * 60 * 1000) {
      clearEcommerceVideoState();
      return null;
    }
    if (parsed.inputFingerprint !== inputFingerprint) return null;
    return parsed;
  } catch {
    return null;
  }
}

export function clearEcommerceVideoState(): void {
  try {
    window.localStorage.removeItem(KEEPALIVE_KEY);
  } catch {
    // ignore
  }
}
