Skip to content

Self-hosted, Self-Improving, Federated Everything App — dev management, agent orchestration, persistent mind, digital twin, second brain, and creative pipeline from a single dashboard over Tailscale to port the user into the OS

48.9 KB
Raw Blame
import PageLayout from '../components/PageLayout';
import { useCallback, useEffect, useMemo, useRef, useState } from 'react';
import { Copy, FileCode2, GitFork, Film, Globe, Images, ImagePlus, LoaderCircle, Music2, PenLine, Sparkles, Wand2, X } from 'lucide-react';
import { Navigate, useLocation, useNavigate, useParams, useSearchParams } from 'react-router';
import PageHeader from '../components/PageHeader';
import RouteTabsHeader from '../components/ui/RouteTabsHeader';
import ProviderModelSelector from '../components/ProviderModelSelector';
import AlbumTrackPicker from '../components/music/AlbumTrackPicker';
import CodeAnimationPreview from '../components/codeAnimation/CodeAnimationPreview';
import CodeAnimationSoundtrack from '../components/codeAnimation/CodeAnimationSoundtrack';
import FilmStylePicker from '../components/codeAnimation/FilmStylePicker';
import GalleryVideoPicker from '../components/videoGen/GalleryVideoPicker';
import ProductionProjects from '../components/codeAnimation/ProductionProjects';
import CodeAnimationGallery from '../components/codeAnimation/CodeAnimationGallery';
import UniverseMoodBoardPicker, { BOARD_FOLLOW_UNIVERSE, BOARD_NONE, useStyleSourceLists } from '../components/media/UniverseMoodBoardPicker';
import useProviderModels from '../hooks/useProviderModels';
import { useSocketResource } from '../hooks/useSocketResource';
import toast from '../components/ui/Toast';
import {
  buildCodeAnimationPrompt,
  generateCodeAnimationBrief,
  getCodeAnimationJob,
  getCodeAnimationOptions,
  listTracks,
  startCodeAnimationGeneration,
  uploadFile,
} from '../services/api';
import { uuidv4 } from '../lib/uuid';
import { copyToClipboard } from '../lib/clipboard';
import { pluralize } from '../lib/textUtils';
import { safeReadJsonStorage, safeRemoveStorage, safeWriteJsonStorage } from '../lib/safeStorage';
import { UPLOAD_IMAGE_ACCEPT, validateImageFile } from '../utils/fileUpload';

const DRAFT_KEY = 'portos.codeAnimation.draft';
const JOB_EVENTS = ['code-animation:changed'];

const DEFAULT_DRAFT = {
  title: '',
  seedIdea: '',
  concept: '',
  cast: '',
  onScreenText: '',
  styleNotes: '',
  styleGrammarId: '',
  universeId: '',
  moodBoardChoice: BOARD_FOLLOW_UNIVERSE,
  includeMoodBoardImages: true,
  referenceImages: [],
  referenceVideo: null,
  audio: null,
  soundtrack: 'none',
  format: { durationSeconds: 20, aspectRatio: '16:9', resolution: '1080p', fps: 30 },
  renderer: 'auto',
  interactive: false,
  // The finished animation this draft forks and extends: { jobId, title, durationSeconds }.
  base: null,
};

// Brief fields where a blank from the brief writer means "nothing to add"
// rather than "clear it" (on-screen text, by contrast, can be deliberately none).
const KEEP_ON_BLANK = new Set(['styleNotes', 'cast']);

const isRecord = (value) => value !== null && typeof value === 'object' && !Array.isArray(value);

const readBase = (value) => (isRecord(value) && typeof value.jobId === 'string' && value.jobId
  ? { jobId: value.jobId, title: typeof value.title === 'string' ? value.title : '', durationSeconds: Number.isFinite(value.durationSeconds) ? value.durationSeconds : null }
  : null);

const loadDraft = (key = DRAFT_KEY) => {
  const stored = safeReadJsonStorage(key, null);
  if (!isRecord(stored)) return DEFAULT_DRAFT;
  const format = isRecord(stored.format) ? stored.format : {};
  const referenceImages = Array.isArray(stored.referenceImages)
    ? stored.referenceImages
      .filter((image) => isRecord(image) && typeof image.filename === 'string' && image.filename.trim())
      .map((image) => ({
        filename: image.filename,
        label: typeof image.label === 'string' ? image.label : image.filename,
        note: typeof image.note === 'string' ? image.note : '',
        url: `/api/uploads/${encodeURIComponent(image.filename)}`,
      }))
    : [];
  let audio = null;
  if (isRecord(stored.audio)) {
    if (stored.audio.source === 'track' && typeof stored.audio.trackId === 'string' && stored.audio.trackId.trim()) {
      audio = {
        source: 'track',
        trackId: stored.audio.trackId.trim(),
        label: typeof stored.audio.label === 'string' ? stored.audio.label : 'Music track',
        durationSeconds: Number.isFinite(stored.audio.durationSeconds) ? stored.audio.durationSeconds : null,
        notes: typeof stored.audio.notes === 'string' ? stored.audio.notes : '',
        url: typeof stored.audio.url === 'string' ? stored.audio.url : '',
      };
    } else if (typeof stored.audio.filename === 'string' && stored.audio.filename.trim()) {
      audio = {
        source: 'upload',
        filename: stored.audio.filename.trim(),
        label: typeof stored.audio.label === 'string' ? stored.audio.label : stored.audio.filename,
        durationSeconds: Number.isFinite(stored.audio.durationSeconds) ? stored.audio.durationSeconds : null,
        notes: typeof stored.audio.notes === 'string' ? stored.audio.notes : '',
        url: `/api/uploads/${encodeURIComponent(stored.audio.filename)}`,
      };
    }
  }
  const stringField = (key) => typeof stored[key] === 'string' ? stored[key] : DEFAULT_DRAFT[key];
  return {
    ...DEFAULT_DRAFT,
    title: stringField('title'),
    seedIdea: stringField('seedIdea'),
    concept: stringField('concept'),
    cast: stringField('cast'),
    onScreenText: stringField('onScreenText'),
    styleNotes: stringField('styleNotes'),
    styleGrammarId: stringField('styleGrammarId'),
    universeId: stringField('universeId'),
    moodBoardChoice: stringField('moodBoardChoice'),
    includeMoodBoardImages: typeof stored.includeMoodBoardImages === 'boolean' ? stored.includeMoodBoardImages : DEFAULT_DRAFT.includeMoodBoardImages,
    referenceImages,
    audio,
    soundtrack: ['none', 'procedural'].includes(stored.soundtrack) ? stored.soundtrack : DEFAULT_DRAFT.soundtrack,
    format: {
      durationSeconds: Number.isFinite(format.durationSeconds) ? format.durationSeconds : DEFAULT_DRAFT.format.durationSeconds,
      aspectRatio: typeof format.aspectRatio === 'string' ? format.aspectRatio : DEFAULT_DRAFT.format.aspectRatio,
      resolution: typeof format.resolution === 'string' ? format.resolution : DEFAULT_DRAFT.format.resolution,
      fps: Number.isFinite(format.fps) ? format.fps : DEFAULT_DRAFT.format.fps,
    },
    renderer: stringField('renderer'),
    interactive: typeof stored.interactive === 'boolean' ? stored.interactive : DEFAULT_DRAFT.interactive,
    base: readBase(stored.base),
  };
};

// The mood-board choice as the server reads it: an ABSENT moodBoardId follows
// the universe's linked board, '' means none.
function moodBoardSelection(draft) {
  if (draft.moodBoardChoice === BOARD_NONE) return { moodBoardId: '' };
  if (draft.moodBoardChoice !== BOARD_FOLLOW_UNIVERSE) return { moodBoardId: draft.moodBoardChoice };
  return {};
}

// The draft → the server's brief shape.
function toBrief(draft) {
  return {
    title: draft.title,
    concept: draft.concept,
    cast: draft.cast,
    onScreenText: draft.onScreenText,
    styleNotes: draft.styleNotes,
    styleGrammarId: draft.styleGrammarId || null,
    format: draft.format,
    renderer: draft.renderer,
    interactive: draft.interactive,
    soundtrack: draft.audio ? 'none' : draft.soundtrack,
    universeId: draft.universeId || null,
    includeMoodBoardImages: draft.includeMoodBoardImages,
    referenceImages: draft.referenceImages.map(({ filename, label, note }) => ({ filename, label, note })),
    referenceVideo: draft.referenceVideo
      ? { filename: draft.referenceVideo.filename, label: draft.referenceVideo.label, note: draft.referenceVideo.note }
      : null,
    audio: draft.audio
      ? draft.audio.source === 'track'
        ? {
          source: 'track',
          trackId: draft.audio.trackId,
          ...(draft.audio.label ? { label: draft.audio.label } : {}),
          ...(draft.audio.durationSeconds != null ? { durationSeconds: draft.audio.durationSeconds } : {}),
          notes: draft.audio.notes || '',
        }
        : {
          filename: draft.audio.filename,
          label: draft.audio.label,
          durationSeconds: draft.audio.durationSeconds ?? null,
          notes: draft.audio.notes,
        }
      : null,
    ...(draft.base ? { baseJobId: draft.base.jobId } : {}),
    ...moodBoardSelection(draft),
  };
}
// The draft → the brief-writer's input: art direction plus whatever the artist
// has typed so far, which the model builds on rather than discards.
function toBriefIdeaInput(draft) {
  return {
    universeId: draft.universeId || null,
    ...moodBoardSelection(draft),
    styleGrammarId: draft.styleGrammarId || null,
    seedIdea: draft.seedIdea,
    current: {
      title: draft.title,
      concept: draft.concept,
      cast: draft.cast,
      onScreenText: draft.onScreenText,
      styleNotes: draft.styleNotes,
    },
    // The writer's prompt reads only these two — the rest of the format
    // conditions the picture, not the story.
    format: { durationSeconds: draft.format.durationSeconds, aspectRatio: draft.format.aspectRatio },
  };
}

function draftFromJob(job) {
  const input = job?.input || {};
  const moodBoardChoice = input.moodBoardId === undefined
    ? BOARD_FOLLOW_UNIVERSE
    : input.moodBoardId
      ? input.moodBoardId
      : BOARD_NONE;
  return {
    ...DEFAULT_DRAFT,
    title: input.title || '',
    seedIdea: input.seedIdea || '',
    concept: input.concept || '',
    cast: input.cast || '',
    onScreenText: input.onScreenText || '',
    styleNotes: input.styleNotes || '',
    styleGrammarId: input.styleGrammarId || '',
    universeId: input.universeId || '',
    moodBoardChoice,
    includeMoodBoardImages: input.includeMoodBoardImages !== false,
    referenceImages: (Array.isArray(input.referenceImages) ? input.referenceImages : []).map((image) => ({
      ...image,
      url: `/api/uploads/${encodeURIComponent(image.filename)}`,
    })),
    referenceVideo: isRecord(input.referenceVideo) ? { ...input.referenceVideo, previewUrl: null } : null,
    audio: input.audio
      ? input.audio.source === 'track'
        ? {
          source: 'track',
          trackId: input.audio.trackId,
          label: input.audio.label || 'Music track',
          durationSeconds: input.audio.durationSeconds ?? null,
          notes: input.audio.notes || '',
          url: job.audioUrl || '',
        }
        : {
          source: 'upload',
          ...input.audio,
          url: `/api/uploads/${encodeURIComponent(input.audio.filename)}`,
        }
      : null,
    soundtrack: input.soundtrack || 'none',
    format: { ...DEFAULT_DRAFT.format, ...(input.format || {}) },
    renderer: input.renderer || 'auto',
    interactive: input.interactive === true,
    base: readBase(job.base),
  };
}

const readAudioDuration = (file) => new Promise((resolve) => {
  const url = URL.createObjectURL(file);
  const audio = new Audio();
  const done = (value) => { URL.revokeObjectURL(url); resolve(value); };
  audio.onloadedmetadata = () => done(Number.isFinite(audio.duration) ? audio.duration : null);
  audio.onerror = () => done(null);
  audio.src = url;
});

const inputClass = 'w-full rounded-lg border border-port-border bg-port-bg px-3 py-2 text-sm text-white placeholder:text-gray-600 focus:border-port-accent focus:outline-none';
const labelClass = 'mb-1 block text-xs text-gray-400';
const fileInputClass = 'block w-full text-xs text-gray-400 file:mr-3 file:rounded file:border-0 file:bg-port-border file:px-3 file:py-1.5 file:text-gray-200';
const buttonPrimary = 'inline-flex items-center gap-2 rounded-lg bg-port-accent px-3 py-1.5 text-sm text-white hover:opacity-90 disabled:cursor-not-allowed disabled:opacity-40';
const buttonSecondary = 'inline-flex items-center gap-2 rounded-lg border border-port-border px-3 py-1.5 text-sm text-gray-200 hover:border-port-accent disabled:cursor-not-allowed disabled:opacity-40';
const providerFilter = (provider) => provider.enabled !== false;

// Read + upload one file; toasts and resolves null on failure.
async function uploadOrToast(file) {
  return uploadFile(file, file.name, { silent: true }).catch((error) => {
    toast.error(error.message || `Failed to upload ${file.name}`);
    return null;
  });
}

function FastCodeAnimation() {
  const [searchParams] = useSearchParams();
  const routeParams = useParams();
  const navigate = useNavigate();
  const jobId = routeParams.jobId || searchParams.get('job') || '';
  const [options, setOptions] = useState(null);
  const styleLists = useStyleSourceLists();
  const { boards } = styleLists;
  const [libraryTracks, setLibraryTracks] = useState([]);
  const [trackPickerOpen, setTrackPickerOpen] = useState(false);
  const draftKey = routeParams.draftId ? `${DRAFT_KEY}.${routeParams.draftId}` : DRAFT_KEY;
  const [loadedDraftKey, setLoadedDraftKey] = useState(draftKey);
  const [draft, setDraft] = useState(() => loadDraft(draftKey));
  // Restore a different draft before rendering or persisting its form.
  if (loadedDraftKey !== draftKey) {
    setLoadedDraftKey(draftKey);
    if (routeParams.draftId) setDraft(loadDraft(draftKey));
  }
  const [uploading, setUploading] = useState(false);
  const [videoPickerOpen, setVideoPickerOpen] = useState(false);
  const [building, setBuilding] = useState(false);
  const [writingBrief, setWritingBrief] = useState(false);
  // The last built prompt, tagged with the brief it was built from.
  const [built, setBuilt] = useState(null);
  const [starting, setStarting] = useState(false);
  const [job, setJob] = useState(null);
  const [effort, setEffort] = useState('');
  const [briefEffort, setBriefEffort] = useState('');
  const [pastedHtml, setPastedHtml] = useState('');
  const [preview, setPreview] = useState(null);
  const hydratedJobIdRef = useRef('');
  const locallyStartedJobIdRef = useRef('');
  const {
    providers,
    selectedProviderId,
    selectedModel,
    availableModels,
    setSelectedProviderId,
    setSelectedModel,
    loading: providersLoading,
  } = useProviderModels({ filter: providerFilter, silent: true, withEffort: true });
  const {
    providers: briefProviders,
    selectedProviderId: briefProviderId,
    selectedModel: briefModel,
    availableModels: briefModels,
    setSelectedProviderId: setBriefProviderId,
    setSelectedModel: setBriefModel,
    loading: briefProvidersLoading,
  } = useProviderModels({ filter: providerFilter, silent: true, withEffort: true });

  const update = (patch) => setDraft((prev) => ({ ...prev, ...patch }));
  const onStyleGrammarChange = useCallback((styleGrammarId) => setDraft((prev) => ({ ...prev, styleGrammarId })), []);
  const updateFormat = (patch) => setDraft((prev) => ({ ...prev, format: { ...prev.format, ...patch } }));

  useEffect(() => {
    if (routeParams.draftId) safeWriteJsonStorage(draftKey, draft);
  }, [draft, draftKey, routeParams.draftId]);

  useEffect(() => {
    getCodeAnimationOptions({ silent: true }).then(setOptions).catch(() => toast.error('Failed to load Code Animation options'));
    listTracks({ silent: true })
      .then((rows) => setLibraryTracks(Array.isArray(rows) ? rows : rows?.tracks || []))
      .catch(() => {});
  }, []);

  const brief = useMemo(() => toBrief(draft), [draft]);
  const briefKey = useMemo(() => JSON.stringify(brief), [brief]);
  const promptStale = !!built && built.briefKey !== briefKey;
  const limits = options?.limits;
  // A reference video is studied as a contact sheet plus keyframes, which
  // occupy reference-image slots.
  const maxRefs = (limits?.referenceImagesMax ?? 8) - (draft.referenceVideo ? limits?.referenceVideoImageSlots ?? 3 : 0);
  const videoRoom = (limits?.referenceImagesMax ?? 8) - draft.referenceImages.length >= (limits?.referenceVideoImageSlots ?? 3);
  const audioAccept = (options?.audioExtensions || ['mp3', 'wav', 'ogg', 'm4a']).map((ext) => `.${ext}`).join(',');
  const generating = job?.id === jobId && job.status === 'running';
  const canBuild = draft.concept.trim().length > 0 && !building && !uploading;
  // The writer needs a world or some words to be faithful to — it is not a
  // blank-slate idea generator.
  const briefSeeds = !!(draft.universeId || draft.seedIdea.trim() || draft.concept.trim() || draft.title.trim());
  const canWriteBrief = briefSeeds && !writingBrief;

  // Clear route-specific output before applying the newly selected resource.
  useEffect(() => {
    hydratedJobIdRef.current = '';
    setJob(null);
    setPreview(null);
    setBuilt(null);
  }, [jobId, routeParams.draftId]);
  const jobResource = useSocketResource(async () => {
    if (!jobId) return null;
    // Only a 404 means the job is gone. Transient failures retain the current
    // output and offer retry, with recovery on events, reconnect or tab re-show.
    return getCodeAnimationJob(jobId, { silent: true }).catch((error) => {
      if (error.status === 404) return { id: jobId, status: 'missing', error: error.message };
      throw error;
    });
  }, {
    namespace: 'code-animation',
    events: JOB_EVENTS,
    resourceKey: jobId,
    // A poster event changes only the gallery; re-reading the job would hand the
    // preview a new frame object and restart a session the user is watching.
    matchesEvent: (payload) => !!jobId && payload?.id === jobId && payload.kind !== 'poster',
  });

  useEffect(() => {
    const next = jobResource.data;
    if (!next) return;
    const requested = jobId;
    setJob(next);
    // Keep the same preview object when only other job fields changed (a
    // soundtrack request), so the running preview session is not restarted.
    if (next.status === 'completed' && next.html) {
      setPreview((current) => (current?.jobId === next.id && current.html === next.html && current.audioUrl === next.audioUrl
        ? current
        : { html: next.html, audioUrl: next.audioUrl, frame: next.frame, jobId: next.id }));
    } else setPreview(null);
    if (locallyStartedJobIdRef.current !== requested
      && hydratedJobIdRef.current !== requested
      && next.input) {
      hydratedJobIdRef.current = requested;
      const restoredDraft = draftFromJob(next);
      setDraft(restoredDraft);
      setSelectedProviderId(next.input.providerId || '');
      setSelectedModel(next.input.model || '');
      setEffort(next.input.effort || '');
      setBuilt(next.prompt ? {
        prompt: next.prompt,
        attachments: next.attachments || [],
        frame: next.frame,
        audioUrl: next.audioUrl,
        moodBoardId: next.moodBoardId,
        briefKey: JSON.stringify(toBrief(restoredDraft)),
      } : null);
    }
  }, [jobId, jobResource.data]);

  const handleImages = async (event) => {
    const files = [...(event.target.files || [])];
    event.target.value = '';
    if (!files.length) return;
    const room = Math.max(0, maxRefs - draft.referenceImages.length);
    if (files.length > room) toast.error(`Only ${pluralize(room, 'more reference image')} fit`);
    const valid = files.slice(0, room).filter((file) => {
      const invalid = validateImageFile(file, Infinity);
      if (invalid) toast.error(invalid);
      return !invalid;
    });
    setUploading(true);
    const uploaded = await Promise.all(valid.map(async (file) => {
      const saved = await uploadOrToast(file);
      return saved ? { filename: saved.filename, label: file.name, note: '', url: saved.path } : null;
    }));
    const added = uploaded.filter(Boolean);
    setUploading(false);
    if (added.length) setDraft((prev) => ({ ...prev, referenceImages: [...prev.referenceImages, ...added] }));
  };

  const updateReference = (filename, patch) => setDraft((prev) => ({
    ...prev,
    referenceImages: prev.referenceImages.map((ref) => (ref.filename === filename ? { ...ref, ...patch } : ref)),
  }));

  const playableTracks = useMemo(() => libraryTracks.filter((t) => Boolean(t.audioFilename)), [libraryTracks]);

  const handlePickTrack = (track) => {
    if (!track) return;
    update({
      audio: {
        source: 'track',
        trackId: track.id,
        label: track.title || 'Untitled track',
        durationSeconds: track.durationSec ?? null,
        notes: '',
        url: track.audioFilename ? `/data/music/${encodeURIComponent(track.audioFilename)}` : '',
      },
    });
  };

  const handleAudio = async (event) => {
    const file = event.target.files?.[0];
    event.target.value = '';
    if (!file) return;
    setUploading(true);
    const [durationSeconds, saved] = await Promise.all([readAudioDuration(file), uploadOrToast(file)]);
    setUploading(false);
    if (saved) update({ audio: { source: 'upload', filename: saved.filename, label: file.name, durationSeconds, notes: '', url: saved.path } });
  };

  // Ask a model to write the brief from the universe's bible and canon cast,
  // the way a series or story is generated from a universe. The result lands in
  // the form as an editable draft — nothing is generated from it until the user
  // builds the prompt.
  const handleWriteBrief = async () => {
    if (!canWriteBrief) return;
    const ideaInput = toBriefIdeaInput(draft);
    const startingBrief = ideaInput.current;
    setWritingBrief(true);
    const result = await generateCodeAnimationBrief({
      ...ideaInput,
      providerId: briefProviderId || undefined,
      model: briefModel || undefined,
      effort: briefEffort || undefined,
    }, { silent: true }).catch((error) => {
      toast.error(error.message || 'Failed to write the brief');
      return null;
    });
    setWritingBrief(false);
    if (!result?.brief) return;
    // Fill only the fields the artist left untouched while the writer ran.
    setDraft((previous) => {
      const next = { ...previous };
      for (const [key, before] of Object.entries(startingBrief)) {
        const written = result.brief[key];
        if (typeof written !== 'string' || previous[key] !== before) continue;
        // A blank refinement or character bible means the writer had nothing to
        // add — never let it wipe what the artist already wrote there.
        if (KEEP_ON_BLANK.has(key) && !written) continue;
        next[key] = written;
      }
      return next;
    });
    toast.success('Brief written — edit it before building the prompt');
  };

  const handleBuild = async () => {
    if (!canBuild) return;
    setBuilding(true);
    const result = await buildCodeAnimationPrompt(brief, { silent: true }).catch((error) => {
      toast.error(error.message || 'Failed to build the prompt');
      return null;
    });
    setBuilding(false);
    if (result) setBuilt({ ...result, briefKey });
  };

  const handleGenerate = async () => {
    if (!canBuild || !selectedProviderId || starting) return;
    setStarting(true);
    const started = await startCodeAnimationGeneration({
      ...brief,
      seedIdea: draft.seedIdea,
      providerId: selectedProviderId,
      model: selectedModel || undefined,
      effort: effort || undefined,
    }, { silent: true }).catch((error) => {
      toast.error(error.message || 'Failed to start generation');
      return null;
    });
    setStarting(false);
    if (!started) return;
    locallyStartedJobIdRef.current = started.id;
    setBuilt({ prompt: started.prompt, attachments: started.attachments, frame: started.frame, audioUrl: started.audioUrl, moodBoardId: started.moodBoardId, briefKey });
    setJob(started);
    setPreview(null);
    navigate(`/code-animation/${encodeURIComponent(started.id)}`);
  };

  // Fork a finished animation: keep its brief, extend its code. The new run is
  // a separate job, so leave the job route; the draft carries everything.
  const handleFork = () => {
    if (!job || job.status !== 'completed') return;
    const forked = draftFromJob(job);
    // A soundtrack generated at the old length would not fit the longer cut.
    const audio = forked.audio?.trackId && forked.audio.trackId === job.soundtrack?.trackId ? null : forked.audio;
    const id = uuidv4();
    safeWriteJsonStorage(`${DRAFT_KEY}.${id}`, { ...forked, audio, base: { jobId: job.id, title: job.title || '', durationSeconds: job.frame?.durationSeconds ?? null } });
    navigate(`/code-animation/drafts/${id}`);
    toast.success('Forked. Set the new duration and write what happens next, then Generate animation.');
  };

  const handlePreviewPasted = () => {
    if (!pastedHtml.trim()) return;
    setPreview({ html: pastedHtml, audioUrl: built?.audioUrl ?? draft.audio?.url ?? null, frame: built?.frame ?? null });
  };

  const boardName = (id) => boards.find((board) => board.id === id)?.name || 'linked board';

  return (
    <div className="mx-auto max-w-7xl space-y-4">
      <div className="grid gap-4 lg:grid-cols-[minmax(0,1fr)_minmax(0,1fr)]">
        <div className="space-y-4">
          <section className="space-y-3 rounded-xl border border-port-border bg-port-card p-4" aria-labelledby="ca-style-heading">
            <h2 id="ca-style-heading" className="flex items-center gap-2 text-sm font-semibold text-white"><Globe className="h-4 w-4 text-port-accent" /> Style</h2>
            <UniverseMoodBoardPicker
              lists={styleLists}
              idPrefix="ca"
              universeLabel="Universe (sets the art style)"
              labelClass={labelClass}
              inputClass={inputClass}
              universeId={draft.universeId}
              moodBoardChoice={draft.moodBoardChoice}
              onUniverseChange={(universeId) => update({ universeId })}
              onBoardChoiceChange={(moodBoardChoice) => update({ moodBoardChoice })}
            />
            <FilmStylePicker id="ca-film-style" value={draft.styleGrammarId} onChange={onStyleGrammarChange} labelClass={labelClass} inputClass={inputClass} />
            <div>
              <label htmlFor="ca-style-notes" className={labelClass}>Style refinements <span className="text-gray-600">(optional, applied on top of the universe style)</span></label>
              <textarea id="ca-style-notes" rows={2} value={draft.styleNotes} maxLength={limits?.styleNotesMax} onChange={(event) => update({ styleNotes: event.target.value })} placeholder="Heavier film grain, slower camera, dusk palette" className={`${inputClass} resize-y`} />
            </div>
            <label className="flex items-center gap-2 text-xs text-gray-300">
              <input type="checkbox" checked={draft.includeMoodBoardImages} onChange={(event) => update({ includeMoodBoardImages: event.target.checked })} />
              Attach mood board images as references
            </label>
          </section>

          <section className="rounded-xl border border-port-border bg-port-card p-4" aria-labelledby="ca-format-heading">
            <h2 id="ca-format-heading" className="mb-3 text-sm font-semibold text-white">Format</h2>
            <div className="grid grid-cols-2 gap-3 sm:grid-cols-3">
              <div>
                <label htmlFor="ca-duration" className={labelClass}>Duration (s)</label>
                <input id="ca-duration" type="number" min={limits?.durationMin ?? 3} max={limits?.durationMax ?? 180} value={draft.format.durationSeconds} onChange={(event) => updateFormat({ durationSeconds: Math.round(Number(event.target.value) || 0) })} className={inputClass} />
              </div>
              <div>
                <label htmlFor="ca-aspect" className={labelClass}>Aspect</label>
                <select id="ca-aspect" value={draft.format.aspectRatio} onChange={(event) => updateFormat({ aspectRatio: event.target.value })} className={inputClass}>
                  {(options?.aspectRatios || ['16:9']).map((ratio) => <option key={ratio} value={ratio}>{ratio}</option>)}
                </select>
              </div>
              <div>
                <label htmlFor="ca-resolution" className={labelClass}>Resolution</label>
                <select id="ca-resolution" value={draft.format.resolution} onChange={(event) => updateFormat({ resolution: event.target.value })} className={inputClass}>
                  {(options?.resolutions || ['1080p']).map((res) => <option key={res} value={res}>{res}</option>)}
                </select>
              </div>
              <div>
                <label htmlFor="ca-fps" className={labelClass}>FPS</label>
                <select id="ca-fps" value={draft.format.fps} onChange={(event) => updateFormat({ fps: Number(event.target.value) })} className={inputClass}>
                  {(limits?.fpsOptions || [30]).map((fps) => <option key={fps} value={fps}>{fps}</option>)}
                </select>
              </div>
              <div>
                <label htmlFor="ca-renderer" className={labelClass}>Renderer</label>
                <select id="ca-renderer" value={draft.renderer} onChange={(event) => update({ renderer: event.target.value })} className={inputClass}>
                  {(options?.renderers || ['auto']).map((renderer) => <option key={renderer} value={renderer}>{renderer}</option>)}
                </select>
              </div>
              <label className="flex items-end gap-2 pb-2 text-xs text-gray-300">
                <input type="checkbox" checked={draft.interactive} onChange={(event) => update({ interactive: event.target.checked })} />
                Interactive
              </label>
            </div>
          </section>

          <section className="space-y-3 rounded-xl border border-port-border bg-port-card p-4" aria-labelledby="ca-brief-heading">
            <div className="flex flex-wrap items-center justify-between gap-2">
              <h2 id="ca-brief-heading" className="flex items-center gap-2 text-sm font-semibold text-white"><Sparkles className="h-4 w-4 text-port-accent" /> Brief</h2>
              <button type="button" onClick={handleWriteBrief} disabled={!canWriteBrief} className={buttonSecondary} title={briefSeeds ? undefined : 'Pick a universe or write a starting idea first'}>
                {writingBrief ? <LoaderCircle className="h-4 w-4 animate-spin" /> : <PenLine className="h-4 w-4" />}
                Write brief
              </button>
            </div>
            {draft.base && (
              <div role="status" className="flex flex-wrap items-center gap-2 rounded-lg border border-port-accent/40 bg-port-accent/10 p-2 text-xs text-gray-200">
                <GitFork className="h-4 w-4 shrink-0 text-port-accent" aria-hidden="true" />
                <span className="min-w-0 flex-1">
                  Extending <span className="font-semibold">{draft.base.title || 'an animation'}</span>
                  {draft.base.durationSeconds ? ` (${draft.base.durationSeconds}s)` : ''}. Its code is sent with the prompt; set the new duration in Format.
                  {draft.base.durationSeconds && draft.format.durationSeconds <= draft.base.durationSeconds ? ' The duration is not longer than the original yet.' : ''}
                </span>
                <button type="button" onClick={() => update({ base: null })} className="text-gray-400 hover:text-white">Stop extending</button>
              </div>
            )}
            <ProviderModelSelector
              providers={briefProviders}
              selectedProviderId={briefProviderId}
              selectedModel={briefModel}
              availableModels={briefModels}
              onProviderChange={(id) => { setBriefProviderId(id); setBriefEffort(''); }}
              onModelChange={setBriefModel}
              effort={briefEffort}
              onEffortChange={setBriefEffort}
              disabled={briefProvidersLoading || writingBrief}
              alwaysShowModel
              emptyModelOption="Provider default"
              label="Brief writing provider"
            />
            <div>
              <label htmlFor="ca-seed" className={labelClass}>
                Starting idea <span className="text-gray-600">(optional; what the brief writer starts from)</span>
              </label>
              <textarea id="ca-seed" rows={2} value={draft.seedIdea} maxLength={limits?.seedIdeaMax} onChange={(event) => update({ seedIdea: event.target.value })} placeholder="A chase through the lower market that ends in silence" className={`${inputClass} resize-y`} />
              <p className="mt-1 text-xs text-gray-500">
                {draft.universeId
                  ? 'Write brief casts the film from this universe\u2019s characters, places, and tone.'
                  : 'Pick a universe above to have the brief cast from its characters and places.'}
              </p>
            </div>
            <div>
              <label htmlFor="ca-title" className={labelClass}>Title <span className="text-gray-600">(optional)</span></label>
              <input id="ca-title" value={draft.title} maxLength={200} onChange={(event) => update({ title: event.target.value })} placeholder="The Lantern Keeper" className={inputClass} />
            </div>
            <div>
              <label htmlFor="ca-concept" className={labelClass}>What happens</label>
              <textarea id="ca-concept" rows={6} value={draft.concept} maxLength={limits?.conceptMax} onChange={(event) => update({ concept: event.target.value })} placeholder={'A paper lantern drifts over a sleeping harbor town, gathers fireflies, and bursts into a constellation.\n0:00–0:04 Low tracking shot: the lantern bobs past rooftops, curious…'} className={`${inputClass} resize-y`} />
            </div>
            <div>
              <label htmlFor="ca-cast" className={labelClass}>Characters <span className="text-gray-600">(optional; design bible the animation rigs)</span></label>
              <textarea id="ca-cast" rows={4} value={draft.cast} maxLength={limits?.castMax} onChange={(event) => update({ cast: event.target.value })} placeholder="Wick — a palm-sized paper lantern: round body, bent-wire handle that droops when sad; palette cream #F3E6C4, ember #E8763A; face: two ink-dot eyes (curious, sleepy, startled, delighted, determined). Identity lock: silhouette and handle never change." className={`${inputClass} resize-y`} />
            </div>
            <div>
              <label htmlFor="ca-text" className={labelClass}>On-screen text / narration <span className="text-gray-600">(optional)</span></label>
              <textarea id="ca-text" rows={2} value={draft.onScreenText} maxLength={limits?.textMax} onChange={(event) => update({ onScreenText: event.target.value })} placeholder={'0:02 "Every night, one light remains"\n0:15 Title card'} className={`${inputClass} resize-y`} />
            </div>
          </section>

          <section className="space-y-3 rounded-xl border border-port-border bg-port-card p-4" aria-labelledby="ca-refs-heading">
            <h2 id="ca-refs-heading" className="flex items-center gap-2 text-sm font-semibold text-white"><ImagePlus className="h-4 w-4 text-port-accent" /> References &amp; audio</h2>
            <div>
              <label htmlFor="ca-images" className={labelClass}>Reference images ({draft.referenceImages.length}/{maxRefs})</label>
              <input id="ca-images" type="file" multiple accept={UPLOAD_IMAGE_ACCEPT} onChange={handleImages} disabled={uploading} className={fileInputClass} />
              {draft.referenceImages.length > 0 && (
                <ul className="mt-2 space-y-2">
                  {draft.referenceImages.map((ref) => (
                    <li key={ref.filename} className="flex items-center gap-2">
                      <img src={ref.url} alt={ref.label} className="h-12 w-12 shrink-0 rounded object-cover" />
                      <input aria-label={`Note for ${ref.label}`} value={ref.note} maxLength={limits?.referenceNoteMax} onChange={(event) => updateReference(ref.filename, { note: event.target.value })} placeholder="What to take from it (palette, silhouette…)" className={`${inputClass} min-w-0`} />
                      <button type="button" aria-label={`Remove ${ref.label}`} onClick={() => update({ referenceImages: draft.referenceImages.filter((item) => item.filename !== ref.filename) })} className="shrink-0 text-gray-500 hover:text-port-error"><X className="h-4 w-4" /></button>
                    </li>
                  ))}
                </ul>
              )}
            </div>
            <div>
              <span className={labelClass}>Reference video <span className="text-gray-600">(optional; the quality bar — its pacing, camera and finish, never its content)</span></span>
              {draft.referenceVideo ? (
                <div className="flex items-center gap-2">
                  {draft.referenceVideo.previewUrl
                    ? <img src={draft.referenceVideo.previewUrl} alt="" className="h-12 w-20 shrink-0 rounded object-cover" />
                    : <Film className="h-5 w-5 shrink-0 text-gray-500" aria-hidden="true" />}
                  <input aria-label={`Note for ${draft.referenceVideo.label}`} value={draft.referenceVideo.note} maxLength={limits?.referenceNoteMax} onChange={(event) => update({ referenceVideo: { ...draft.referenceVideo, note: event.target.value } })} placeholder={`${draft.referenceVideo.label}: what to match (cut rhythm, depth of field, grade…)`} className={`${inputClass} min-w-0`} />
                  <button type="button" aria-label="Remove reference video" onClick={() => update({ referenceVideo: null })} className="shrink-0 text-gray-500 hover:text-port-error"><X className="h-4 w-4" /></button>
                </div>
              ) : (
                <button type="button" onClick={() => setVideoPickerOpen(true)} disabled={!videoRoom} title={videoRoom ? undefined : `A reference video needs ${limits?.referenceVideoImageSlots ?? 3} free reference slots`} className="inline-flex items-center gap-1.5 rounded border border-port-border px-2.5 py-1.5 text-xs text-gray-200 hover:border-port-accent disabled:opacity-50">
                  <Film className="h-3.5 w-3.5" aria-hidden="true" /> Pick or upload a video…
                </button>
              )}
              <GalleryVideoPicker open={videoPickerOpen} onClose={() => setVideoPickerOpen(false)} allowUpload uploadToGallery
                onSelect={(item) => update({ referenceVideo: { filename: item.filename, label: item.prompt && item.prompt !== '(no prompt)' ? item.prompt.slice(0, 120) : item.filename, note: '', previewUrl: item.previewUrl } })} />
            </div>
            <div>
              <label htmlFor="ca-audio" className={`${labelClass} flex items-center gap-1`}><Music2 className="h-3 w-3" /> Audio track <span className="text-gray-600">(optional; the animation syncs to it)</span></label>
              {draft.audio ? (
                <div className="space-y-2">
                  <div className="flex items-center gap-2 text-sm text-gray-200">
                    <span className="min-w-0 truncate">{draft.audio.label}</span>
                    {draft.audio.durationSeconds ? <span className="text-xs text-gray-500">{draft.audio.durationSeconds.toFixed(1)}s</span> : null}
                    <span className="rounded bg-port-card-hover px-1.5 py-0.5 text-[10px] uppercase tracking-wider text-gray-400">
                      {draft.audio.source === 'track' ? 'Library track' : 'Upload'}
                    </span>
                    <button type="button" aria-label="Remove audio track" onClick={() => update({ audio: null })} className="ml-auto text-gray-500 hover:text-port-error"><X className="h-4 w-4" /></button>
                  </div>
                  <textarea aria-label="Audio notes" rows={2} value={draft.audio.notes} maxLength={limits?.audioNotesMax} onChange={(event) => update({ audio: { ...draft.audio, notes: event.target.value } })} placeholder="120 BPM; soft intro, drop at 0:16, fade at 0:40" className={`${inputClass} resize-y`} />
                </div>
              ) : (
                <div className="space-y-2">
                  <div className="flex flex-wrap items-center gap-2">
                    <input id="ca-audio" type="file" accept={audioAccept} onChange={handleAudio} disabled={uploading} className={`${fileInputClass} flex-1 min-w-[200px]`} />
                    <button
                      type="button"
                      onClick={() => setTrackPickerOpen(true)}
                      className={buttonSecondary}
                    >
                      <Music2 className="h-3.5 w-3.5" /> Pick from music library
                    </button>
                  </div>
                  <label className="flex items-center gap-2 text-xs text-gray-300">
                    <input type="checkbox" checked={draft.soundtrack === 'procedural'} onChange={(event) => update({ soundtrack: event.target.checked ? 'procedural' : 'none' })} />
                    No track? Have the code compose a procedural soundtrack
                  </label>
                  <p className="text-xs text-gray-500">Or generate one with a music model once the preview is ready, under Soundtrack below it.</p>
                </div>
              )}
            </div>
            {uploading && <p className="flex items-center gap-2 text-xs text-gray-400"><LoaderCircle className="h-3 w-3 animate-spin" /> Uploading…</p>}
          </section>

        </div>

        <div className="space-y-4">
          <section className="space-y-3 rounded-xl border border-port-border bg-port-card p-4" aria-labelledby="ca-prompt-heading">
            <div className="flex flex-wrap items-center justify-between gap-2">
              <h2 id="ca-prompt-heading" className="text-sm font-semibold text-white">Prompt</h2>
              <div className="flex shrink-0 flex-wrap gap-2 border-b border-port-border px-4 py-2">
                <button type="button" onClick={handleBuild} disabled={!canBuild} className={buttonPrimary}>
                  {building ? <LoaderCircle className="h-4 w-4 animate-spin" /> : <Wand2 className="h-4 w-4" />}
                  {built ? 'Rebuild prompt' : 'Build prompt'}
                </button>
                <button type="button" onClick={() => built && copyToClipboard(built.prompt, 'Prompt copied')} disabled={!built} className={buttonSecondary}>
                  <Copy className="h-4 w-4" /> Copy
                </button>
              </div>
            </div>
            {!draft.concept.trim() && <p className="text-xs text-gray-500">Describe what happens in the brief to build a prompt.</p>}
            {built && (
              <>
                {promptStale && <p className="text-xs text-port-warning">The brief changed since this prompt was built. Rebuild to include your edits.</p>}
                {built.moodBoardId && draft.moodBoardChoice === BOARD_FOLLOW_UNIVERSE && (
                  <p className="text-xs text-gray-500">Using the universe&apos;s mood board: {boardName(built.moodBoardId)}</p>
                )}
                <textarea readOnly aria-label="Generated prompt" value={built.prompt} rows={14} className={`${inputClass} font-mono text-xs`} />
                {built.attachments?.length > 0 && (
                  <div>
                    <p className="mb-1 text-xs text-gray-400">Attach these images, in order, when you paste the prompt into another LLM:</p>
                    <div className="flex shrink-0 flex-wrap gap-2 border-b border-port-border px-4 py-2">
                      {built.attachments.map((attachment, index) => (
                        <a key={attachment.url} href={attachment.url} target="_blank" rel="noreferrer" title={`${index + 1}. ${attachment.label} (${attachment.origin})`} className="relative block h-14 w-14 overflow-hidden rounded border border-port-border">
                          <img src={attachment.url} alt={attachment.label} className="h-full w-full object-cover" />
                          <span className="port-media-overlay-strong absolute left-0 top-0 rounded-br px-1 text-[10px]">{index + 1}</span>
                        </a>
                      ))}
                    </div>
                  </div>
                )}
              </>
            )}
          </section>

          <section className="space-y-3 rounded-xl border border-port-border bg-port-card p-4" aria-labelledby="ca-generate-heading">
            <h2 id="ca-generate-heading" className="text-sm font-semibold text-white">Generate &amp; preview</h2>
            <ProviderModelSelector
              providers={providers}
              selectedProviderId={selectedProviderId}
              selectedModel={selectedModel}
              availableModels={availableModels}
              onProviderChange={(id) => { setSelectedProviderId(id); setEffort(''); }}
              onModelChange={setSelectedModel}
              effort={effort}
              onEffortChange={setEffort}
              disabled={providersLoading || starting}
              alwaysShowModel
              emptyModelOption="Provider default"
              label="Generation provider"
            />
            <div className="flex flex-wrap items-center gap-3">
              <button type="button" onClick={handleGenerate} disabled={!canBuild || !selectedProviderId || starting || generating} className={buttonPrimary}>
                {starting || generating ? <LoaderCircle className="h-4 w-4 animate-spin" /> : <Sparkles className="h-4 w-4" />}
                {generating ? 'Generating…' : 'Generate animation'}
              </button>
              {job?.status === 'failed' && <span className="text-xs text-port-error">Generation failed: {job.error}</span>}
              {job?.status === 'missing' && <span className="text-xs text-gray-500">That generation is no longer available.</span>}
            </div>
            {jobResource.error && (
              <div role="status" className="flex flex-wrap items-center gap-2 text-xs text-port-error">
                <span>Could not refresh this generation. Its displayed status may be out of date.</span>
                <button type="button" onClick={jobResource.refetch} className={buttonSecondary}>Retry job status</button>
              </div>
            )}
            <details className="text-xs text-gray-400">
              <summary className="cursor-pointer select-none">Preview HTML from another LLM</summary>
              <div className="mt-2 space-y-2">
                <textarea aria-label="Pasted animation HTML" rows={4} value={pastedHtml} onChange={(event) => setPastedHtml(event.target.value)} placeholder="Paste the <!DOCTYPE html> document the model returned" className={`${inputClass} font-mono text-xs`} />
                <button type="button" onClick={handlePreviewPasted} disabled={!pastedHtml.trim()} className={buttonSecondary}>Preview pasted HTML</button>
              </div>
            </details>
            {preview && options && (
              <CodeAnimationPreview
                html={preview.html}
                audioUrl={preview.audioUrl}
                frame={preview.frame}
                jobId={preview.jobId}
                messages={options.messages}
                audioGlobal={options.audioGlobal}
                title={draft.title || draft.concept.slice(0, 40)}
              />
            )}
            {preview?.jobId && job?.id === preview.jobId && job.status === 'completed' && (
              <button type="button" onClick={handleFork} className={buttonSecondary}>
                <GitFork className="h-4 w-4" /> Fork &amp; extend
              </button>
            )}
            {preview?.jobId && job?.id === preview.jobId && job.status === 'completed' && (
              <CodeAnimationSoundtrack
                job={job}
                jobId={job.id}
                soundtrack={job.soundtrack}
                audioUrl={job.audioUrl}
                audioTrackId={job.input?.audio?.trackId}
                durationSeconds={job.frame?.durationSeconds}
                onReady={jobResource.refetch}
              />
            )}
          </section>
        </div>
      </div>
      <AlbumTrackPicker
        open={trackPickerOpen}
        tracks={playableTracks}
        onClose={() => setTrackPickerOpen(false)}
        onAdd={([selected]) => {
          if (selected) handlePickTrack(selected);
        }}
        single
        title="Pick soundtrack track"
      />
    </div>
  );
}


// Route-mounted lanes keep inactive galleries and provider calls idle.
function NewCodeAnimation() {
  const [id] = useState(uuidv4);
  useEffect(() => {
    // Carry the pre-UUID draft forward without sharing it with future projects.
    const legacy = safeReadJsonStorage(DRAFT_KEY, null);
    if (legacy) {
      const key = `${DRAFT_KEY}.${id}`;
      safeWriteJsonStorage(key, legacy);
      if (safeReadJsonStorage(key, null)) safeRemoveStorage(DRAFT_KEY);
    }
  }, [id]);
  return <Navigate to={`/code-animation/drafts/${id}`} replace />;
}

export default function CodeAnimation() {
  const { pathname, search } = useLocation();
  const mode = pathname.startsWith('/code-animation/production')
    ? 'production'
    : pathname === '/code-animation/gallery' || pathname.startsWith('/code-animation/gallery/') ? 'gallery' : 'fast';
  return (
    <PageLayout
      header={<PageHeader
        icon={FileCode2}
        title="Code Animation"
        subtitle="Prompt an LLM to code an animated film, with no assets, in a universe's style, then preview it and record it to video."
      />}
      navigation={<RouteTabsHeader
        ariaLabel="Animation mode"
        activeTab={mode}
        tabs={[
          { id: 'fast', label: 'Fast HTML', to: '/code-animation', icon: FileCode2 },
          { id: 'gallery', label: 'Gallery', to: '/code-animation/gallery', icon: Images },
          { id: 'production', label: 'Production', to: '/code-animation/production', icon: Film },
        ]}
      />}
    >
      <div className="mx-auto max-w-7xl">
        {mode === 'production' ? <ProductionProjects /> : mode === 'gallery' ? <CodeAnimationGallery /> : pathname === '/code-animation' && !new URLSearchParams(search).get('job') ? <NewCodeAnimation /> : <FastCodeAnimation />}
      </div>
    </PageLayout>
  );
}