import type { PipelineAdapters, ThemeBrief } from "./types";

// Phase 0 mock implementation: no external APIs are called. Every stage
// waits briefly (to simulate real work / let the UI show live progress)
// and returns deterministic placeholder content so the full workflow —
// intake -> analysis -> script -> assembly -> metadata -> review — can be
// exercised end-to-end before any paid provider is wired in.

function delay(ms: number) {
  return new Promise((resolve) => setTimeout(resolve, ms));
}

function extractVideoId(url: string): string {
  try {
    const u = new URL(url);
    return u.searchParams.get("v") ?? u.pathname.split("/").filter(Boolean).pop() ?? "unknown";
  } catch {
    return "unknown";
  }
}

export const mockAdapters: PipelineAdapters = {
  async fetchSourceMetadata(url) {
    await delay(500);
    const id = extractVideoId(url);
    return { title: `Reference video (${id})`, description: "", tags: [] };
  },

  async extractThemeBrief(sourceMeta): Promise<ThemeBrief> {
    await delay(700);
    return {
      topic: `Same general topic as "${sourceMeta.title}"`,
      format: "Hook → problem → payoff → CTA",
      beats: [
        "Open on a strong visual hook in the first 2 seconds",
        "State the problem/curiosity gap",
        "Deliver the payoff / demonstration",
        "Close with a clear call to action",
      ],
      tone: "Energetic, fast-paced, upbeat",
    };
  },

  async generateScript(brief) {
    await delay(900);
    const script = [
      `HOOK: You won't believe what happens next...`,
      `SETUP: Here's the situation about ${brief.topic}.`,
      `PAYOFF: And that's exactly how it plays out.`,
      `CTA: Follow for more like this.`,
    ].join("\n");
    return { script, durationSeconds: 42 };
  },

  async generateVisuals() {
    await delay(1200);
    return { assetUrls: ["mock://visual/scene-1", "mock://visual/scene-2", "mock://visual/scene-3"] };
  },

  async synthesizeVoice() {
    await delay(900);
    return { url: "mock://voice/narration.mp3" };
  },

  async generateMusic() {
    await delay(600);
    return { url: "mock://music/track.mp3" };
  },

  async assembleVideo() {
    await delay(1000);
    return { url: "mock://video/short.mp4", durationSeconds: 42 };
  },

  async generateMetadata(brief) {
    await delay(600);
    return {
      title: `This ${brief.topic.replace(/^Same general topic as /, "")} Hack Actually Works`,
      description:
        "Original content generated from a theme brief — new script, voice, visuals, and music, not reused footage.\n\n#shorts",
      tags: ["shorts", "viral", "trending"],
    };
  },
};
