File size: 13,157 Bytes
ae14296
 
 
 
 
 
 
 
 
 
 
40d16d9
ae14296
 
 
 
 
 
 
 
 
 
 
 
 
 
40d16d9
ae14296
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
40d16d9
ae14296
 
 
 
 
 
40d16d9
 
1974805
 
 
40d16d9
 
 
ae14296
 
 
 
 
 
40d16d9
ae14296
 
 
 
 
40d16d9
ae14296
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
40d16d9
ae14296
 
 
40d16d9
ae14296
 
 
 
 
 
 
 
 
 
 
40d16d9
ae14296
 
 
 
 
 
 
 
 
 
 
 
 
40d16d9
 
 
 
ae14296
40d16d9
 
 
1974805
 
 
40d16d9
 
 
ae14296
 
 
 
 
 
40d16d9
ae14296
 
 
 
 
40d16d9
ae14296
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
40d16d9
ae14296
 
 
 
 
 
 
 
 
 
 
 
 
 
40d16d9
ae14296
40d16d9
 
 
 
ae14296
 
40d16d9
 
 
 
 
 
 
 
 
 
 
ae14296
 
 
 
 
40d16d9
 
ae14296
 
 
40d16d9
ae14296
40d16d9
ae14296
40d16d9
 
 
 
 
 
 
 
ae14296
 
40d16d9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ae14296
 
 
 
 
40d16d9
 
 
 
 
 
 
 
ae14296
40d16d9
 
ae14296
 
 
 
 
40d16d9
 
 
 
 
 
 
 
ae14296
 
40d16d9
 
 
 
 
 
 
 
 
ae14296
40d16d9
 
ae14296
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
import { useState } from "react";
import { Dialog, DialogContent, DialogHeader, DialogTitle } from "@/components/ui/dialog";
import { Tabs, TabsContent, TabsList, TabsTrigger } from "@/components/ui/tabs";
import { Button } from "@/components/ui/button";
import { Input } from "@/components/ui/input";
import { Label } from "@/components/ui/label";
import { Textarea } from "@/components/ui/textarea";
import { useToast } from "@/hooks/use-toast";
import { useAuth } from "@/hooks/useAuth";
import { supabase } from "@/integrations/supabase/client";
import { useNavigate } from "react-router-dom";
import { Loader2, Upload, FileText, Youtube, Sparkles } from "lucide-react";
import { useDropzone } from "react-dropzone";
import { triggerIngest } from "@/lib/pipeline";
import { extractFileText } from "@/lib/extract";

const MAX_FILE_BYTES = 50 * 1024 * 1024; // 50MB
const AUDIO_EXTS = ["mp3", "wav", "m4a", "ogg", "flac", "webm"];
const VIDEO_EXTS = ["mp4", "mov", "mkv"];
const IMAGE_EXTS = ["png", "jpg", "jpeg", "webp", "bmp"];

export default function UploadDialog({ open, onOpenChange }: { open: boolean; onOpenChange: (v: boolean) => void }) {
  const { user } = useAuth();
  const { toast } = useToast();
  const navigate = useNavigate();
  const [submitting, setSubmitting] = useState(false);
  const [extractionProgress, setExtractionProgress] = useState(0);

  // Text mode
  const [textTitle, setTextTitle] = useState("");
  const [textContent, setTextContent] = useState("");
  // YouTube mode
  const [ytUrl, setYtUrl] = useState("");
  // File mode
  const [file, setFile] = useState<File | null>(null);

  const { getRootProps, getInputProps, isDragActive } = useDropzone({
    multiple: false,
    onDrop: (files) => setFile(files[0] ?? null),
  });

  const reset = () => {
    setTextTitle(""); setTextContent(""); setYtUrl(""); setFile(null);
    setExtractionProgress(0);
  };

  const createTextDoc = async () => {
    if (!user || !textContent.trim()) return;
    setSubmitting(true);
    try {
      // EXPERT OVERRIDE
      const EXPERT_PREFIX = `[EXPERT ADVISORY: ACT AS AN EXPERT ACADEMIC DATA EXTRACTION AGENT. 
STYLE: PROFESSIONAL, CLEAN MARKDOWN. NO EMOJIS ALLOWED.
DIRECTIVE: LOSSLESS EXTRACTION. ZERO OMISSION.
TEMPLATE: Overview -> Key Points -> Tables for Frameworks -> Sub-headings for Detailed Terms.]\n\n`;
      
      const finalContent = EXPERT_PREFIX + textContent;

      const { data, error } = await supabase
        .from("documents")
        .insert({
          user_id: user.id,
          title: textTitle.trim() || "Untitled note",
          source_type: "text",
          raw_text: finalContent,
          status: "ready",
        })
        .select("id")
        .single();
      if (error) throw error;
      toast({ title: "Document created", description: "Expert agent has saved your notes with lossless directives." });
      reset(); onOpenChange(false);
      navigate(`/app/doc/${data!.id}`);
    } catch (e: any) {
      toast({ title: "Failed", description: e.message, variant: "destructive" });
    } finally {
      setSubmitting(false);
    }
  };

  const createYoutubeDoc = async () => {
    if (!user || !ytUrl.trim()) return;
    setSubmitting(true);
    try {
      const { data, error } = await supabase
        .from("documents")
        .insert({
          user_id: user.id,
          title: ytUrl,
          source_type: "youtube",
          source_url: ytUrl,
          status: "pending",
        })
        .select("id")
        .single();
      if (error) throw error;
      toast({ title: "Source Queued", description: "Expert Agent is fetching the transcript..." });
      reset(); onOpenChange(false);
      navigate(`/app/doc/${data!.id}`);
      triggerIngest(data!.id).catch((e) =>
        toast({ title: "Extraction failed", description: e.message, variant: "destructive" }),
      );
    } catch (e: any) {
      toast({ title: "Failed", description: e.message, variant: "destructive" });
    } finally {
      setSubmitting(false);
    }
  };

  const createFileDoc = async () => {
    if (!user || !file) return;
    if (file.size > MAX_FILE_BYTES) {
      toast({ title: "File too large", description: "Expert Agent requires files under 50MB.", variant: "destructive" });
      return;
    }
    setSubmitting(true);
    try {
      const ext = file.name.split(".").pop()?.toLowerCase() ?? "";
      const isPdf = ext === "pdf";
      const isDocx = ext === "docx" || ext === "doc";
      const isImage = IMAGE_EXTS.includes(ext);
      const isAudio = AUDIO_EXTS.includes(ext);
      const isVideo = VIDEO_EXTS.includes(ext);

      // PDF/DOCX/Image: extract text in the browser, no file upload needed
      if (isPdf || isDocx || isImage) {
        toast({ title: "Expert Extraction Initialized", description: "Running parallel OCR and hierarchy mapping..." });
        const { text: extractedText, sourceType } = await extractFileText(file, (p) => setExtractionProgress(p));
        if (!extractedText || extractedText.trim().length < 5) {
          throw new Error("Expert Agent could not extract readable text from this file.");
        }

        // EXPERT OVERRIDE: Prepend instructions to ensure lossless extraction even if backend is old
        const EXPERT_PREFIX = `[EXPERT ADVISORY: ACT AS AN EXPERT ACADEMIC DATA EXTRACTION AGENT. 
STYLE: PROFESSIONAL, CLEAN MARKDOWN. NO EMOJIS ALLOWED.
DIRECTIVE: LOSSLESS EXTRACTION. ZERO OMISSION.
TEMPLATE: Overview -> Key Points -> Tables for Frameworks -> Sub-headings for Detailed Terms.]\n\n`;
        
        const finalRawText = EXPERT_PREFIX + extractedText;

        const { data, error } = await supabase
          .from("documents")
          .insert({
            user_id: user.id,
            title: file.name,
            source_type: sourceType,
            raw_text: finalRawText,
            status: "pending",
          })
          .select("id")
          .single();
        if (error) throw error;
        toast({ title: "Extraction Complete", description: "Expert Agent is finalizing the document..." });
        reset(); onOpenChange(false);
        navigate(`/app/doc/${data!.id}`);
        triggerIngest(data!.id).catch((e) =>
          toast({ title: "Ingest failed", description: e.message, variant: "destructive" }),
        );
        return;
      }

      // Audio/Video: upload to storage; ingest function transcribes via Groq Whisper
      if (!isAudio && !isVideo) {
        throw new Error("Unsupported file type. Use PDF, DOCX, Image, audio or video.");
      }
      const sourceType = isAudio ? "audio" : "video";
      const path = `${user.id}/${crypto.randomUUID()}-${file.name}`;
      const { error: upErr } = await supabase.storage.from("uploads").upload(path, file);
      if (upErr) throw upErr;

      const { data, error } = await supabase
        .from("documents")
        .insert({
          user_id: user.id,
          title: file.name,
          source_type: sourceType,
          source_url: path,
          status: "pending",
        })
        .select("id")
        .single();
      if (error) throw error;
      toast({ title: "Source Uploaded", description: "Expert Agent is initiating transcription..." });
      reset(); onOpenChange(false);
      navigate(`/app/doc/${data!.id}`);
      triggerIngest(data!.id).catch((e) =>
        toast({ title: "Ingest failed", description: e.message, variant: "destructive" }),
      );
    } catch (e: any) {
      toast({ title: "Failed", description: e.message, variant: "destructive" });
    } finally {
      setSubmitting(false);
    }
  };

  return (
    <Dialog open={open} onOpenChange={onOpenChange}>
      <DialogContent className="sm:max-w-lg bg-slate-950 border-slate-800 text-white">
        <DialogHeader>
          <DialogTitle className="flex items-center gap-2">
            <Sparkles className="h-5 w-5 text-primary" />
            Add Academic Source
          </DialogTitle>
        </DialogHeader>

        <Tabs defaultValue="file" className="w-full">
          <TabsList className="grid grid-cols-3 w-full bg-slate-900">
            <TabsTrigger value="file" className="data-[state=active]:bg-slate-800 text-xs">
              <Upload className="h-3.5 w-3.5 mr-1.5" /> File
            </TabsTrigger>
            <TabsTrigger value="youtube" className="data-[state=active]:bg-slate-800 text-xs">
              <Youtube className="h-3.5 w-3.5 mr-1.5" /> YouTube
            </TabsTrigger>
            <TabsTrigger value="text" className="data-[state=active]:bg-slate-800 text-xs">
              <FileText className="h-3.5 w-3.5 mr-1.5" /> Text
            </TabsTrigger>
          </TabsList>

          <TabsContent value="file" className="space-y-4 pt-4">
            <div
              {...getRootProps()}
              className={`border-2 border-dashed rounded-lg p-8 text-center cursor-pointer transition-all duration-300 ${
                isDragActive ? "border-primary bg-primary/10" : "border-slate-800 hover:border-primary/50 hover:bg-slate-900/50"
              }`}
            >
              <input {...getInputProps()} />
              <Upload className="h-8 w-8 mx-auto mb-3 text-slate-500" />
              {file ? (
                <p className="text-sm font-medium">{file.name}</p>
              ) : (
                <div className="space-y-1">
                  <p className="text-sm text-slate-300">
                    {isDragActive ? "Drop here…" : "Drag your academic document here"}
                  </p>
                  <p className="text-[11px] text-slate-500 uppercase tracking-tighter">
                    PDF, DOCX, Images, MP3, MP4 Supported
                  </p>
                </div>
              )}
            </div>

            {submitting && extractionProgress > 0 && (
              <div className="space-y-2">
                <div className="flex justify-between text-[10px] uppercase tracking-widest text-slate-500 font-bold">
                  <span>Extraction Progress</span>
                  <span>{extractionProgress}%</span>
                </div>
                <div className="h-1.5 w-full bg-slate-900 rounded-full overflow-hidden">
                  <div 
                    className="h-full bg-primary transition-all duration-300"
                    style={{ width: `${extractionProgress}%` }}
                  />
                </div>
              </div>
            )}

            <Button 
              onClick={createFileDoc} 
              disabled={!file || submitting} 
              className="w-full bg-primary hover:bg-primary/90 text-white font-semibold"
            >
              {submitting ? (
                <>
                  <Loader2 className="h-4 w-4 mr-2 animate-spin" />
                  {extractionProgress > 0 ? "Expert Scanning..." : "Processing..."}
                </>
              ) : (
                "Initialize Extraction"
              )}
            </Button>
          </TabsContent>

          <TabsContent value="youtube" className="space-y-4 pt-4">
            <div className="space-y-1.5">
              <Label htmlFor="yt" className="text-xs text-slate-400">YouTube URL</Label>
              <Input 
                id="yt" 
                value={ytUrl} 
                onChange={(e) => setYtUrl(e.target.value)} 
                placeholder="https://youtube.com/watch?v=..." 
                className="bg-slate-900 border-slate-800 text-white placeholder:text-slate-600"
              />
            </div>
            <Button onClick={createYoutubeDoc} disabled={!ytUrl.trim() || submitting} className="w-full bg-primary hover:bg-primary/90">
              {submitting && <Loader2 className="h-4 w-4 mr-2 animate-spin" />} Analyze Link
            </Button>
          </TabsContent>

          <TabsContent value="text" className="space-y-4 pt-4">
            <div className="space-y-1.5">
              <Label htmlFor="title" className="text-xs text-slate-400">Note Title</Label>
              <Input 
                id="title" 
                value={textTitle} 
                onChange={(e) => setTextTitle(e.target.value)} 
                placeholder="Untitled Lecture Notes" 
                className="bg-slate-900 border-slate-800 text-white placeholder:text-slate-600"
              />
            </div>
            <div className="space-y-1.5">
              <Label htmlFor="content" className="text-xs text-slate-400">Content</Label>
              <Textarea 
                id="content" 
                value={textContent} 
                onChange={(e) => setTextContent(e.target.value)} 
                rows={8} 
                placeholder="Paste your text here…" 
                className="bg-slate-900 border-slate-800 text-white placeholder:text-slate-600 resize-none"
              />
            </div>
            <Button onClick={createTextDoc} disabled={!textContent.trim() || submitting} className="w-full bg-primary hover:bg-primary/90">
              {submitting && <Loader2 className="h-4 w-4 mr-2 animate-spin" />} Create Expert Note
            </Button>
          </TabsContent>
        </Tabs>
      </DialogContent>
    </Dialog>
  );
}