import React, { useState, useEffect } from 'react'; import { Plus, Settings, Users, BarChart, Trash2, Edit2, X, MessageSquare, Send, Image as ImageIcon, Video, Loader2, FileText, FileSpreadsheet, FileArchive, Link as LinkIcon, Paperclip, Lightbulb, ListVideo, Check, Lock, Unlock, Globe, Download } from 'lucide-react'; import { Course, Comment, Attachment, Lesson, AttachmentType } from '../types'; import { useToast } from '../context/ToastContext'; import { getManagedCourses, saveCourse, deleteCourse, getComments, saveComment } from '../services/db'; import { CATEGORIES } from '../constants'; import { getAttachmentIcon, getAttachmentBadge } from '../components/MaterialCard'; // 1. Create/Edit Course Modal const CourseModal: React.FC<{ isOpen: boolean; onClose: () => void; onSave: (course: Course) => Promise; initialData?: Course; }> = ({ isOpen, onClose, onSave, initialData }) => { const [title, setTitle] = useState(''); const [description, setDescription] = useState(''); const [category, setCategory] = useState(''); const [thumbnailPreview, setThumbnailPreview] = useState(''); const [instructor, setInstructor] = useState('Mário Morgado'); const [instructorRole, setInstructorRole] = useState('Especialista em Performance'); // Lesson Management const [lessons, setLessons] = useState([]); // New Lesson State const [newLessonTitle, setNewLessonTitle] = useState(''); const [newLessonDuration, setNewLessonDuration] = useState(''); const [newLessonVideoUrl, setNewLessonVideoUrl] = useState(''); const [newLessonProvider, setNewLessonProvider] = useState('external'); const [newLessonIsFree, setNewLessonIsFree] = useState(false); // Tips & Global Attachments const [tips, setTips] = useState([]); const [newTip, setNewTip] = useState(''); const [attachments, setAttachments] = useState([]); // New Attachment State const [newAttName, setNewAttName] = useState(''); const [newAttType, setNewAttType] = useState('pdf'); const [newAttUrl, setNewAttUrl] = useState(''); const [newAttSize, setNewAttSize] = useState(''); const [newAttDesc, setNewAttDesc] = useState(''); const [isSubmitting, setIsSubmitting] = useState(false); useEffect(() => { if (isOpen) { if (initialData) { setTitle(initialData.title); setDescription(initialData.description); setCategory(initialData.category); setThumbnailPreview(initialData.thumbnail); setInstructor(initialData.instructor || 'Mário Morgado'); setInstructorRole(initialData.instructorRole || 'Especialista em Performance'); setTips(initialData.tips || []); setAttachments(initialData.attachments || []); setLessons(initialData.lessons || []); } else { setTitle(''); setDescription(''); setCategory(CATEGORIES[0]); setThumbnailPreview(''); setInstructor('Mário Morgado'); setInstructorRole('Especialista em Performance'); setTips([]); setAttachments([]); setLessons([]); } } }, [isOpen, initialData]); const handleAddLesson = () => { if (!newLessonTitle.trim()) { alert("Digite um título para a aula."); return; } if (!newLessonVideoUrl.trim()) { alert("Enter a hosted video URL."); return; } const newLesson: Lesson = { id: `lesson-${Date.now()}`, title: newLessonTitle, duration: newLessonDuration || '15m', videoUrl: newLessonVideoUrl, mediaProvider: newLessonProvider || 'external', isFree: newLessonIsFree, description: '' }; setLessons([...lessons, newLesson]); setNewLessonTitle(''); setNewLessonDuration(''); setNewLessonVideoUrl(''); setNewLessonProvider('external'); setNewLessonIsFree(false); }; const removeLesson = (id: string) => { setLessons(lessons.filter(l => l.id !== id)); }; const toggleLessonAccess = (id: string) => { setLessons(lessons.map(l => l.id === id ? { ...l, isFree: !l.isFree } : l)); }; const handleAddTip = () => { if (newTip.trim()) { setTips([...tips, newTip.trim()]); setNewTip(''); } }; const removeTip = (idx: number) => { setTips(tips.filter((_, i) => i !== idx)); }; const handleAddAttachment = () => { if (!newAttName.trim()) { alert("Informe o nome do arquivo para download."); return; } const att: Attachment = { id: `att-${Date.now()}`, name: newAttName.trim(), type: newAttType, url: newAttUrl.trim() || '#', size: newAttSize.trim() || '1.5 MB', description: newAttDesc.trim() }; setAttachments([...attachments, att]); setNewAttName(''); setNewAttSize(''); setNewAttUrl(''); setNewAttDesc(''); }; const removeAttachment = (id: string) => { setAttachments(attachments.filter(a => a.id !== id)); }; const handleSubmit = async (e: React.FormEvent) => { e.preventDefault(); if (lessons.length === 0) { alert("Adicione pelo menos uma aula à trilha."); return; } setIsSubmitting(true); const durationStr = `${lessons.length} Aulas`; try { await onSave({ id: initialData?.id || `course-${Date.now()}`, title, description, category: category || CATEGORIES[0], thumbnail: thumbnailPreview || 'https://images.unsplash.com/photo-1460925895917-afdab827c52f?auto=format&fit=crop&w=800&q=80', duration: durationStr, instructor, instructorRole, views: initialData?.views || 0, progress: initialData?.progress || 0, lessons: lessons, tips: tips, attachments: attachments }); onClose(); } catch { alert('We could not save this course. Confirm that you are still signed in and that all asset URLs are valid.'); } finally { setIsSubmitting(false); } }; if (!isOpen) return null; return (
{/* Left Column: Course Details, Tips & Downloads */}

{initialData ? 'Editar Curso' : 'Criar Curso'}

{/* Thumbnail */}
{thumbnailPreview ? ( <> Preview
Alterar Capa
) : (

Add a hosted cover image URL below

)}
setThumbnailPreview(event.target.value)} placeholder="Hosted cover image URL" className="w-full mt-2 bg-black/50 text-white text-xs border border-white/10 rounded-xl px-4 py-3 focus:outline-none focus:border-orange-500/50" />
setTitle(e.target.value)} placeholder="Ex: Masterclass: Escala de Tráfego Pago" className="w-full bg-black/50 text-white text-base font-bold border border-white/10 rounded-xl px-4 py-3 focus:outline-none focus:border-orange-500/50" required />
setInstructor(e.target.value)} placeholder="Nome do Instrutor" className="w-full bg-black/50 text-white border border-white/10 rounded-xl px-3 py-2.5 text-xs focus:outline-none focus:border-orange-500/50" />