File size: 22,361 Bytes
5463318
 
 
 
 
 
 
 
 
38ee1b6
5463318
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
38ee1b6
5463318
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
import React, { useState } from 'react';
import { motion } from 'framer-motion';
import { useGenerate } from '../GenerateContext';
import { FileEdit, GraduationCap, Clock, Hash, CheckCircle2, Sparkles, Loader2, Book } from 'lucide-react';
import { Button } from '../../../components/ui/Button';
import { Input } from '../../../components/ui/Input';
import { useNavigate } from 'react-router-dom';
import { cn } from '../../../../src/utils/cn';
import { supabase } from '../../../../src/utils/supabaseClient';
import { API_BASE_URL } from '../../../../src/utils/api';

export const FinalDetailsStep = () => {
    const { finalDetails, setFinalDetails, configure, materials, setStep } = useGenerate();
    const navigate = useNavigate();
    const [isGenerating, setIsGenerating] = useState(false);
    const [progress, setProgress] = useState(0);
    const [loadingText, setLoadingText] = useState("Balancing cognitive complexity...");

    const updateField = (field, value) => {
        setFinalDetails(prev => ({ ...prev, [field]: value }));
    };

    // Auto-calculate Total Marks from Configuration
    React.useEffect(() => {
        const calculatedTotal = (configure.questionTypes || [])
            .filter(q => q.selected)
            .reduce((sum, q) => sum + (q.count * q.marks), 0);
        
        const totalStr = calculatedTotal.toString();
        if (finalDetails.totalMarks !== totalStr) {
            setFinalDetails(prev => ({ ...prev, totalMarks: totalStr }));
        }
    }, [configure.questionTypes, finalDetails.totalMarks, setFinalDetails]);

    const [errors, setErrors] = useState({});

    const validate = () => {
        const newErrors = {};
        if (!finalDetails.title) newErrors.title = "Paper title is required";
        if (!finalDetails.subject) newErrors.subject = "Subject is required";
        setErrors(newErrors);
        return Object.keys(newErrors).length === 0;
    };

    const handleGenerate = async () => {
        if (!validate()) return;
        setIsGenerating(true);
        // ... rest of the code

        setProgress(0);

        const messages = [
            "Structuring evaluation framework...",
            "Applying Bloom's Taxonomy rules...",
            "Validating question difficulty...",
            "Finalizing layout...",
        ];

        let currProgress = 0;

        const interval = setInterval(() => {
            currProgress += Math.random() * 20;
            if (currProgress > 95) currProgress = 95;
            setProgress(currProgress);

            if (currProgress > 25 && currProgress < 50) setLoadingText(messages[0]);
            if (currProgress > 50 && currProgress < 75) setLoadingText(messages[1]);
            if (currProgress > 75 && currProgress < 90) setLoadingText(messages[2]);
            if (currProgress >= 90) setLoadingText(messages[3]);
        }, 600);

        try {
            const fileId = materials[0]; // Take first material

            // Attempt to get user JWT session
            const { data: { session } } = await supabase.auth.getSession();
            const token = session?.access_token;

            const headers = { 'Content-Type': 'application/json' };
            if (token) {
                headers['Authorization'] = `Bearer ${token}`;
            }

            const res = await fetch(`${API_BASE_URL}/generate`, {
                method: 'POST',
                headers: headers,
                body: JSON.stringify({
                    file_id: fileId,
                    ...finalDetails,
                    configure: {
                        ...configure,
                        marks: finalDetails.totalMarks,
                        numberOfQuestions: configure.numberOfQuestions || 10
                    }
                })
            });
            const data = await res.json();

            clearInterval(interval);
            setProgress(100);

            setTimeout(() => {
                navigate("/app/preview", {
                    state: {
                        formData: { ...finalDetails, content_preview: data.content_preview },
                        sectionsData: configure,
                        materials: materials,
                        generatedResult: data
                    }
                });
            }, 1000);
        } catch (error) {
            console.error("Generation failed", error);
            clearInterval(interval);
            setIsGenerating(false);
            alert("Generation failed due to server error");
        }
    };

    return (
        <motion.div
            initial={{ opacity: 0, x: 20 }} animate={{ opacity: 1, x: 0 }} exit={{ opacity: 0, x: -20 }}
            className="w-full bg-white dark:bg-[#111827] shadow-[0_8px_30px_rgb(0,0,0,0.04)] dark:shadow-[0_8px_30px_rgba(0,0,0,0.4)] rounded-2xl border border-slate-100 dark:border-slate-800 p-6 sm:p-8 flex flex-col"
        >
            <div className="mb-6">
                <h2 className="text-2xl font-bold text-slate-900 dark:text-white mb-2">Final Details</h2>
                <p className="text-slate-500 font-medium">Add the identifying header information for your paper.</p>
            </div>

            <div className="space-y-6 pb-8">
                <div className="space-y-4">
                    <div className="relative">
                        <label className="text-xs font-bold text-slate-500 uppercase tracking-widest block mb-2">Paper Title</label>
                        <div className="relative flex items-center">
                            <FileEdit className="absolute left-3 w-5 h-5 text-slate-400" />
                            <input
                                className={cn(
                                    "w-full pl-10 pr-4 py-3 rounded-xl border bg-white dark:bg-[#0F172A] text-slate-900 dark:text-white focus:ring-2 focus:ring-[#4F7CFF] focus:border-transparent outline-none transition-all font-medium placeholder:font-normal placeholder:text-slate-400",
                                    errors.title ? "border-red-500" : "border-slate-200 dark:border-slate-700"
                                )}
                                placeholder="e.g. Mid-Term Physics Assessment 2026"
                                value={finalDetails.title || ''}
                                onChange={e => {
                                    updateField('title', e.target.value);
                                    if (errors.title) setErrors(prev => ({ ...prev, title: '' }));
                                }}
                            />
                        </div>
                        {errors.title && <p className="text-red-500 text-[10px] mt-1 font-bold uppercase tracking-wider">{errors.title}</p>}
                    </div>

                    <div className="grid grid-cols-1 md:grid-cols-2 gap-4">
                        <div className="relative">
                            <label className="text-xs font-bold text-slate-500 uppercase tracking-widest block mb-2">Subject</label>
                            <div className="relative flex items-center">
                                <Book className="absolute left-3 w-4 h-4 text-slate-400" />
                                <input
                                    className={cn(
                                        "w-full pl-9 pr-3 py-2.5 rounded-xl border bg-white dark:bg-[#0F172A] text-slate-900 dark:text-white focus:ring-2 focus:ring-[#4F7CFF] outline-none text-sm",
                                        errors.subject ? "border-red-500" : "border-slate-200 dark:border-slate-700"
                                    )}
                                    placeholder="e.g. Physics"
                                    value={finalDetails.subject || ''}
                                    onChange={e => {
                                        updateField('subject', e.target.value);
                                        if (errors.subject) setErrors(prev => ({ ...prev, subject: '' }));
                                    }}
                                />
                            </div>
                            {errors.subject && <p className="text-red-500 text-[10px] mt-1 font-bold uppercase tracking-wider">{errors.subject}</p>}
                        </div>
                        <div className="relative">
                            <label className="text-xs font-bold text-slate-500 uppercase tracking-widest block mb-2">Grade / Class</label>
                            <div className="relative flex items-center">
                                <GraduationCap className="absolute left-3 w-4 h-4 text-slate-400" />
                                <input
                                    className="w-full pl-9 pr-3 py-2.5 rounded-xl border border-slate-200 dark:border-slate-700 bg-white dark:bg-[#0F172A] text-slate-900 dark:text-white focus:ring-2 focus:ring-[#4F7CFF] outline-none text-sm"
                                    placeholder="e.g. Grade 11"
                                    value={finalDetails.classGrade || ''}
                                    onChange={e => updateField('classGrade', e.target.value)}
                                />
                            </div>
                        </div>
                    </div>

                    <div className="grid grid-cols-1 md:grid-cols-2 gap-4">
                        <div className="relative">
                            <label className="text-xs font-bold text-slate-500 uppercase tracking-widest block mb-2">Duration (Minutes)</label>
                            <div className="relative flex items-center">
                                <Clock className="absolute left-3 w-4 h-4 text-slate-400" />
                                <input
                                    type="number"
                                    className="w-full pl-9 pr-3 py-2.5 rounded-xl border border-slate-200 dark:border-slate-700 bg-white dark:bg-[#0F172A] text-slate-900 dark:text-white focus:ring-2 focus:ring-[#4F7CFF] outline-none text-sm"
                                    placeholder="60"
                                    value={finalDetails.duration}
                                    onChange={e => updateField('duration', e.target.value)}
                                />
                            </div>
                        </div>
                        <div className="relative">
                            <label className="text-xs font-bold text-slate-500 uppercase tracking-widest block mb-2">Total Marks (Auto-set)</label>
                            <div className="relative flex items-center group/marks">
                                <Hash className="absolute left-3 w-4 h-4 text-slate-400 group-hover/marks:text-[#4F7CFF] transition-colors" />
                                <input
                                    readOnly
                                    className="w-full pl-9 pr-3 py-2.5 rounded-xl border border-slate-200 dark:border-slate-700 bg-slate-100 dark:bg-slate-800/50 text-slate-500 dark:text-slate-400 cursor-not-allowed outline-none text-sm font-bold"
                                    placeholder="Calculated from sections"
                                    value={finalDetails.totalMarks}
                                />
                            </div>
                        </div>
                    </div>

                    <div className="relative">
                        <label className="text-xs font-bold text-slate-500 uppercase tracking-widest block mb-2">General Instructions</label>
                        <textarea
                            rows={4}
                            className="w-full p-4 rounded-xl border border-slate-200 dark:border-slate-700 bg-white dark:bg-[#0F172A] text-slate-900 dark:text-white focus:ring-2 focus:ring-[#4F7CFF] focus:border-transparent outline-none transition-all font-medium placeholder:font-normal placeholder:text-slate-400 resize-none text-sm"
                            placeholder="All questions are compulsory..."
                            value={finalDetails.instructions}
                            onChange={e => updateField('instructions', e.target.value)}
                        />
                    </div>
                </div>
            </div>

            <div className="mt-auto pt-6 border-t border-slate-200 dark:border-slate-800 flex justify-between gap-4">
                <Button
                    variant="outline"
                    onClick={() => setStep(2)}
                    disabled={isGenerating || progress === 100}
                    className="px-6 py-2.5 rounded-xl font-bold border-slate-200 text-slate-600 hover:bg-slate-50 dark:border-slate-700 dark:text-slate-300 dark:hover:bg-slate-800"
                >
                    Back
                </Button>
                <Button
                    onClick={handleGenerate}
                    onMouseMove={(e) => {
                        const rect = e.currentTarget.getBoundingClientRect();
                        e.currentTarget.style.setProperty('--mouse-x', `${e.clientX - rect.left}px`);
                        e.currentTarget.style.setProperty('--mouse-y', `${e.clientY - rect.top}px`);
                    }}
                    disabled={isGenerating || progress === 100 || !finalDetails.title}
                    className={cn(
                        "relative w-full sm:w-auto overflow-hidden px-10 py-5 text-white font-bold rounded-2xl transition-all shadow-2xl hover:scale-[1.02] active:scale-[0.98] disabled:opacity-100 disabled:scale-100 group border-none",
                        !isGenerating && "bg-gradient-to-br from-[#6366f1] via-[#8b5cf6] to-[#ec4899] hover:shadow-[0_0_30px_rgba(139,92,246,0.5)]",
                        isGenerating && "bg-[#1e293b]"
                    )}
                >
                    {/* Background Animated Gradient - Dynamic depth */}
                    {!isGenerating && (
                        <motion.div 
                            className="absolute inset-0 opacity-50 bg-[radial-gradient(circle_at_50%_40%,_rgba(255,255,255,0.2),transparent)]"
                            animate={{ 
                                scale: [1, 1.3, 1],
                                opacity: [0.3, 0.6, 0.3]
                            }}
                            transition={{ duration: 6, repeat: Infinity, ease: "easeInOut" }}
                        />
                    )}

                    {/* Active State Overlay */}
                    {isGenerating && (
                        <motion.div 
                            className="absolute inset-0 bg-gradient-to-r from-[#6366f1] to-[#8b5cf6] opacity-10"
                            initial={{ opacity: 0 }}
                            animate={{ opacity: 0.1 }}
                        />
                    )}

                    <div className="relative z-10 flex flex-col items-center justify-center min-w-[280px]">
                        {isGenerating || progress === 100 ? (
                            <>
                                <div className="flex items-center gap-4 mb-3">
                                    <div className="relative h-10 w-10 flex items-center justify-center">
                                        {progress === 100 ? (
                                            <motion.div
                                                initial={{ scale: 0, rotate: -45 }}
                                                animate={{ scale: 1, rotate: 0 }}
                                                className="bg-green-500 rounded-lg p-1.5 shadow-[0_0_15px_rgba(34,197,94,0.5)]"
                                            >
                                                <CheckCircle2 size={24} className="text-white" />
                                            </motion.div>
                                        ) : (
                                            <div className="relative">
                                                <Loader2 size={28} className="animate-spin text-[#8b5cf6]" />
                                                <motion.div 
                                                    className="absolute inset-0 blur-md text-[#8b5cf6]/60"
                                                    animate={{ opacity: [0, 1, 0] }}
                                                    transition={{ duration: 1.5, repeat: Infinity }}
                                                >
                                                    <Loader2 size={28} className="animate-spin" />
                                                </motion.div>
                                            </div>
                                        )}
                                    </div>
                                    <div className="flex flex-col items-start">
                                        <span className="text-sm font-black uppercase tracking-[0.1em] text-white/95">
                                            {progress === 100 ? "Ready to Review" : "AI Crafting Pipeline"}
                                        </span>
                                        <span className="text-[11px] font-bold text-[#8b5cf6] flex items-center gap-1.5">
                                            {progress === 100 ? "Paper Finalized" : `Compiling... ${Math.round(progress)}%`}
                                            {!(progress === 100) && (
                                                <div className="flex gap-0.5">
                                                    {[0, 1, 2].map(i => (
                                                        <motion.div
                                                            key={i}
                                                            className="w-1 h-1 bg-[#8b5cf6] rounded-full"
                                                            animate={{ opacity: [0.3, 1, 0.3] }}
                                                            transition={{ delay: i * 0.2, duration: 1, repeat: Infinity }}
                                                        />
                                                    ))}
                                                </div>
                                            )}
                                        </span>
                                    </div>
                                </div>
                                
                                <div className="w-full relative h-1.5 bg-white/10 rounded-full overflow-hidden mb-3">
                                    <motion.div 
                                        className="absolute top-0 left-0 h-full bg-gradient-to-r from-[#6366f1] via-[#8b5cf6] to-[#ec4899] transition-all" 
                                        animate={{ width: `${progress}%` }}
                                        transition={{ type: "spring", stiffness: 50, damping: 20 }}
                                    >
                                        {/* Progressive shadow for depth */}
                                        <div className="absolute inset-0 shadow-[inset_0_1px_1px_rgba(255,255,255,0.3)]" />
                                        
                                        {/* Trailing beam effect */}
                                        <motion.div 
                                            className="absolute top-0 bottom-0 w-20 bg-gradient-to-r from-transparent via-white/30 to-transparent"
                                            animate={{ left: ['-100%', '100%'] }}
                                            transition={{ duration: 1.5, repeat: Infinity, ease: "linear" }}
                                        />

                                        {/* Lead edge glow */}
                                        <div className="absolute right-0 top-0 bottom-0 w-1 bg-white shadow-[0_0_12px_2px_#fff]" />
                                    </motion.div>
                                </div>
                                
                                <motion.span 
                                    className="text-[10px] uppercase font-bold text-white/60 tracking-[0.25em] h-3 flex items-center"
                                    animate={{ opacity: [0.6, 1, 0.6] }}
                                    transition={{ duration: 2, repeat: Infinity }}
                                >
                                    {loadingText}
                                </motion.span>
                            </>
                        ) : (
                            <div className="flex items-center gap-4 py-1">
                                <div className="relative">
                                    <div className="bg-white/10 p-2 rounded-xl border border-white/10 group-hover:bg-white/20 transition-colors">
                                        <Sparkles size={24} className="text-white fill-white/20 group-hover:scale-110 transition-transform duration-300" />
                                    </div>
                                    <motion.div 
                                        className="absolute -inset-2 bg-gradient-to-r from-[#6366f1] to-[#ec4899] rounded-full blur-xl opacity-0 group-hover:opacity-40 transition-opacity"
                                    />
                                </div>
                                <div className="flex flex-col items-start">
                                    <span className="text-lg leading-tight tracking-tight">Generate Question Paper</span>
                                    <span className="text-[10px] uppercase tracking-[0.3em] font-black text-white/70">With Cognitive Intelligence</span>
                                </div>
                            </div>
                        )}
                    </div>

                    {/* Interactive Surface Glow */}
                    {!isGenerating && (
                        <div className="absolute inset-0 opacity-0 group-hover:opacity-100 transition-opacity duration-700 pointer-events-none bg-[radial-gradient(circle_at_var(--mouse-x,50%)_var(--mouse-y,50%),rgba(255,255,255,0.25)_0%,transparent_60%)]" />
                    )}
                </Button>
            </div>
        </motion.div>
    );
};