import { supabase } from "@/auth/supabase" export const planKeys = { byId: (id: string) => ["plan", id] as const, } export const asignaturaKeys = { count: (planId: string) => ["asignaturas", "count", planId] as const, preview: (planId: string) => ["asignaturas", "preview", planId] as const, extra: (asigId: string) => ["asignatura", "extra", asigId] as const, } export type PlanFull = { id: string; nombre: string; nivel: string | null; objetivo_general: string | null; perfil_ingreso: string | null; perfil_egreso: string | null; duracion: string | null; total_creditos: number | null; competencias_genericas: string | null; competencias_especificas: string | null; sistema_evaluacion: string | null; indicadores_desempeno: string | null; pertinencia: string | null; prompt: string | null; estado: string | null; fecha_creacion: string | null; carreras: { id: string; nombre: string; facultades?: { id: string; nombre: string; color?: string | null; icon?: string | null } | null } | null } export type AsignaturaLite = { id: string; nombre: string; semestre: number | null; creditos: number | null } export function planByIdOptions(planId: string) { return { queryKey: planKeys.byId(planId), queryFn: async (): Promise => { const { data, error } = await supabase .from("plan_estudios") .select(` *, carreras ( id, nombre, facultades ( id, nombre, color, icon ) ) `) .eq("id", planId) .maybeSingle(); if (error || !data) throw error ?? new Error("Plan no encontrado") return data as unknown as PlanFull }, staleTime: 60_000, } as const } export function asignaturasCountOptions(planId: string) { return { queryKey: asignaturaKeys.count(planId), queryFn: async (): Promise => { const { count, error } = await supabase .from("asignaturas") .select("*", { count: "exact", head: true }) .eq("plan_id", planId) if (error) throw error return count ?? 0 }, staleTime: 30_000, } as const } export function asignaturasPreviewOptions(planId: string) { return { queryKey: asignaturaKeys.preview(planId), queryFn: async (): Promise => { const { data, error } = await supabase .from("asignaturas") .select("id, nombre, semestre, creditos") .eq("plan_id", planId) .order("semestre", { ascending: true }) .order("nombre", { ascending: true }) .limit(8) if (error) throw error return (data ?? []) as unknown as AsignaturaLite[] }, staleTime: 30_000, } as const } export function asignaturaExtraOptions(asigId: string) { return { queryKey: asignaturaKeys.extra(asigId), queryFn: async (): Promise<{ tipo: string | null horas_teoricas: number | null horas_practicas: number | null contenidos: Record> | null } | null> => { const { data, error } = await supabase .from("asignaturas") .select("tipo, horas_teoricas, horas_practicas, contenidos") .eq("id", asigId) .maybeSingle() if (error) throw error return (data as any) ?? null }, } as const }