import {
  defineVQChronologicalScenario,
  defineVQScenarioAssetWorkflow,
  totalVQChronologicalScenarioDurationInFrames,
} from "../../lib/VQRemotionLib/scenario";
import {doesVQTimelineEventAdvanceTimeline} from "../../lib/VQRemotionLib/timeline";
import {timeline, type SpeechEvent, type TimelineEvent} from "./script";
import voicevoxManifest from "./voicevox-manifest.json";

type ManifestEntry = {
  id: string;
  character?: string;
  speakerName?: string;
  styleName?: string;
  speakerId?: number;
  file: string;
  durationSeconds: number;
};

const manifestEntries = voicevoxManifest as ManifestEntry[];
const manifestById = new Map(
  manifestEntries.map((entry) => [entry.id, entry])
);

export const ZUNDAMON_REWORK_STANDEE_DEMO_FPS = 30;
export const ZUNDAMON_REWORK_STANDEE_DEMO_GAP_FRAMES = 6;

export const hasAudioForSpeech = (speech: SpeechEvent) =>
  manifestById.has(speech.id);

export const audioFileForSpeech = (speech: SpeechEvent) =>
  manifestById.get(speech.id)?.file ??
  `audio/zundamon-rework-standee-demo/lines/${speech.id}.wav`;

export const durationForSpeech = (
  speech: SpeechEvent,
  fps = ZUNDAMON_REWORK_STANDEE_DEMO_FPS
) => {
  const entry = manifestById.get(speech.id);
  if (entry && Number.isFinite(entry.durationSeconds)) {
    return Math.max(1, Math.ceil(entry.durationSeconds * fps));
  }

  const textForEstimate = speech.readAs ?? speech.text;
  const estimatedSeconds = Math.max(1.2, textForEstimate.length * 0.11);
  return Math.ceil(estimatedSeconds * fps);
};

export const durationForTimelineEvent = (
  event: TimelineEvent,
  fps = ZUNDAMON_REWORK_STANDEE_DEMO_FPS
) => {
  if (event.type === "say") {
    return durationForSpeech(event, fps);
  }

  const durationSeconds =
    "durationSeconds" in event ? event.durationSeconds : undefined;
  if (typeof durationSeconds === "number" && Number.isFinite(durationSeconds)) {
    return Math.max(1, Math.ceil(durationSeconds * fps));
  }

  throw new Error(`Timeline event "${event.type}" needs a duration.`);
};

export const zundamonReworkStandeeDemoAssetWorkflow =
  defineVQScenarioAssetWorkflow({
    voicevox: {
      scriptPath: "src/data/zundamon-rework-standee-demo/script.ts",
      outputDir: "public/audio/zundamon-rework-standee-demo/lines",
      manifestPath:
        "src/data/zundamon-rework-standee-demo/voicevox-manifest.json",
    },
    rhubarb: {
      sourceManifestPath:
        "src/data/zundamon-rework-standee-demo/voicevox-manifest.json",
      manifestPath: "src/generated/lipsync/manifest.json",
      outputDir: "src/generated/lipsync",
      rawOutputDir: "public/lipsync/raw",
    },
  });

export const zundamonReworkStandeeDemoScenario =
  defineVQChronologicalScenario<TimelineEvent>({
    timeline,
    gapFrames: ZUNDAMON_REWORK_STANDEE_DEMO_GAP_FRAMES,
    durationForEvent: durationForTimelineEvent,
    doesEventAdvanceTimeline: doesVQTimelineEventAdvanceTimeline,
    assetWorkflow: zundamonReworkStandeeDemoAssetWorkflow,
  });

export const totalZundamonReworkStandeeDemoDurationInFrames = (
  fps = ZUNDAMON_REWORK_STANDEE_DEMO_FPS
) =>
  totalVQChronologicalScenarioDurationInFrames(
    zundamonReworkStandeeDemoScenario,
    fps
  );
