import type { BrandProfile, Project } from "@/types/workflow";

/** Env-driven config for self-hosted OSS video pipeline (Ollama, FLUX, Wan, Piper, FFmpeg). */
export function getOssVideoPipelineConfig() {
  return {
    enabled: process.env.OSS_VIDEO_PIPELINE?.trim().toLowerCase() === "true",
    llmProvider: process.env.OSS_LLM_PROVIDER?.trim() || "ollama",
    llmModel: process.env.OSS_LLM_MODEL?.trim() || "qwen3:8b",
    ollamaHost: process.env.OLLAMA_HOST?.trim() || "http://127.0.0.1:11434",
    imageModel: process.env.OSS_IMAGE_MODEL?.trim() || "flux-dev",
    imageToVideoModel: process.env.OSS_I2V_MODEL?.trim() || "wan2.1",
    ttsEngine: process.env.OSS_TTS_ENGINE?.trim() || "piper",
    sceneCount: Number(process.env.OSS_VIDEO_SCENE_COUNT ?? 10),
    sceneDurationSeconds: Number(process.env.OSS_SCENE_DURATION_SECONDS ?? 3),
    outputDir: process.env.OSS_VIDEO_OUTPUT_DIR?.trim() || "data/video-projects",
    ffmpegPath: process.env.FFMPEG_PATH?.trim() || "ffmpeg",
  };
}

/** System prompt for LLM storyboard JSON (Ollama / local models). */
export function buildOssStoryboardSystemPrompt(): string {
  return [
    "You are a professional video storyboard writer for YouTube Shorts and social reels.",
    "Return ONLY valid JSON — no markdown, no explanation.",
    "Create 8–12 unique scenes. Each scene must have a different visual — not a slideshow of one image.",
    "Adapt visuals to the company's industry from the brand profile. Do not assume software/SaaS unless the brand is tech.",
    "Fields per scene: scene, duration (seconds), visual_prompt, voiceover, text (max 10 words), camera, motion, transition, animation, sound_effect.",
    "Top-level: title, creative_concept, total_duration_seconds, visual_direction, music_style, scenes[], cta{text, website?, phone?, email?}.",
    "Do not invent phone or email — only include if provided in context.",
  ].join(" ");
}

export function buildOssStoryboardUserPrompt(input: {
  brand: BrandProfile;
  project: Project;
  caption: string;
  marketingContext: string;
  sceneCount?: number;
}): string {
  const cfg = getOssVideoPipelineConfig();
  const scenes = input.sceneCount ?? cfg.sceneCount;

  return [
    `Create a ${scenes}-scene YouTube Shorts storyboard JSON (~${scenes * cfg.sceneDurationSeconds} seconds total).`,
    "",
    `Company: ${input.brand.companyName || input.project.name}`,
    `Industry: ${input.brand.industry || input.project.industry}`,
    `Website: ${input.project.website || "not provided"}`,
    `Tagline: ${input.brand.tagline || ""}`,
    `Brand voice: ${input.brand.brandVoice || "Professional"}`,
    "",
    "## Marketing post",
    input.caption.slice(0, 2000),
    "",
    "## Marketing context",
    input.marketingContext.slice(0, 1500),
    "",
    "JSON only:",
  ].join("\n");
}
