"use client"; import { useState } from "react"; import { Button } from "@/components/ui/button"; import { Dialog, DialogContent, DialogHeader, DialogTitle, DialogFooter } from "@/components/ui/dialog"; import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from "@/components/ui/select"; import { Input } from "@/components/ui/input"; import { Checkbox } from "@/components/ui/checkbox"; import { useChannels, useUpdateChannel } from "@/hooks/use-channels"; import type { LibraryItemFull, ShowSummary, Weekday, ProgrammingBlock, ScheduleConfig } from "@/lib/types"; import { WEEKDAYS, WEEKDAY_LABELS } from "@/lib/types"; interface Props { selectedItems: LibraryItemFull[]; selectedShows?: ShowSummary[]; } export function ScheduleFromLibraryDialog({ selectedItems, selectedShows }: Props) { const [open, setOpen] = useState(false); const [channelId, setChannelId] = useState(""); const [selectedDays, setSelectedDays] = useState>(new Set()); const [startTime, setStartTime] = useState("20:00"); const [durationMins, setDurationMins] = useState(() => { if (selectedItems.length === 1) return Math.ceil(selectedItems[0].duration_secs / 60); return 60; }); const [strategy, setStrategy] = useState<"sequential" | "random" | "best_fit">("sequential"); const { data: channels } = useChannels(); const updateChannel = useUpdateChannel(); const selectedChannel = channels?.find(c => c.id === channelId); const isEpisodic = selectedItems.every(i => i.content_type === "episode"); const allSameSeries = isEpisodic && selectedItems.length > 0 && new Set(selectedItems.map(i => i.series_name)).size === 1; function toggleDay(day: Weekday) { setSelectedDays(prev => { const next = new Set(prev); if (next.has(day)) next.delete(day); else next.add(day); return next; }); } async function handleConfirm() { if (!selectedChannel || selectedDays.size === 0) return; const startTimeFull = startTime.length === 5 ? `${startTime}:00` : startTime; const hasShows = selectedShows && selectedShows.length > 0; const newBlock: ProgrammingBlock = hasShows ? { id: globalThis.crypto.randomUUID(), name: selectedShows!.length === 1 ? `${selectedShows![0].series_name} — ${startTime}` : `${selectedShows!.length} shows — ${startTime}`, start_time: startTimeFull, duration_mins: durationMins, content: { type: "algorithmic", filter: { content_type: "episode", series_names: selectedShows!.map(s => s.series_name), genres: [], tags: [], collections: [], }, strategy, }, } : allSameSeries ? { id: globalThis.crypto.randomUUID(), name: `${selectedItems[0].series_name ?? "Series"} — ${startTime}`, start_time: startTimeFull, duration_mins: durationMins, content: { type: "algorithmic", filter: { content_type: "episode", series_names: [selectedItems[0].series_name!], genres: [], tags: [], collections: [], }, strategy, provider_id: selectedItems[0].id.split("::")[0], }, } : { id: globalThis.crypto.randomUUID(), name: `${selectedItems.length} items — ${startTime}`, start_time: startTimeFull, duration_mins: durationMins, content: { type: "manual", items: selectedItems.map(i => i.id) }, }; const updatedDayBlocks = { ...selectedChannel.schedule_config.day_blocks }; for (const day of selectedDays) { updatedDayBlocks[day] = [...(updatedDayBlocks[day] ?? []), newBlock]; } const scheduleConfig: ScheduleConfig = { day_blocks: updatedDayBlocks }; await updateChannel.mutateAsync({ id: channelId, data: { schedule_config: scheduleConfig }, }); setOpen(false); } const canConfirm = !!channelId && selectedDays.size > 0; const daysLabel = [...selectedDays].map(d => WEEKDAY_LABELS[d]).join(", "); const preview = canConfirm ? `${selectedDays.size} block(s) will be created on ${selectedChannel?.name} — ${daysLabel} at ${startTime}, ${strategy}` : null; return ( <> Schedule on channel

Channel

Days

{WEEKDAYS.map(day => ( ))}

Start time{selectedChannel?.timezone ? ` (${selectedChannel.timezone})` : ""}

setStartTime(e.target.value)} disabled={!channelId} />

Duration (mins)

setDurationMins(Number(e.target.value))} disabled={!channelId} />

Fill strategy

{preview && (

{preview}

)}
); }