File size: 13,642 Bytes
542c765
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"use client";
import { useCallback, useState, useEffect } from "react";
import { useDropzone } from "react-dropzone";
import { useRouter } from "next/navigation";
import { motion, AnimatePresence } from "framer-motion";
import { colors, motionPresets } from "@/lib/tokens";
import { useGUCStore, type ParsedReport } from "@/lib/store";
import { getNextMock } from "@/lib/mockData";

const LOADING_STEPS = [
  "रिपोर्ट पढ़ रहे हैं... (Reading report...)",
  "मेडिकल शब्द समझ रहे हैं... (Understanding jargon...)",
  "हिंदी में अनुवाद हो रहा है... (Translating to Hindi...)",
  "लगभग हो गया! (Almost done!)",
];

const FEATURES = [
  { icon: "🔒", label: "100% Private" },
  { icon: "⚡", label: "Instant Results" },
  { icon: "🇮🇳", label: "Made for India" },
];

export default function Home() {
  const router = useRouter();
  const setLatestReport = useGUCStore((s) => s.setLatestReport);
  const language = useGUCStore((s) => s.profile.language);

  const [file, setFile] = useState<File | null>(null);
  const [loading, setLoading] = useState(false);
  const [loadingStep, setLoadingStep] = useState(0);
  const [progress, setProgress] = useState(0);
  const [dots, setDots] = useState<{ x: number; y: number; size: number; opacity: number }[]>([]);
  const [error, setError] = useState<string | null>(null);

  useEffect(() => {
    setDots(Array.from({ length: 50 }, () => ({
      x: Math.random() * 100,
      y: Math.random() * 100,
      size: Math.random() * 2 + 0.5,
      opacity: Math.random() * 0.25 + 0.04,
    })));
  }, []);

  useEffect(() => {
    if (!loading) return;
    const id = setInterval(() => setLoadingStep((s) => (s + 1) % LOADING_STEPS.length), 800);
    return () => clearInterval(id);
  }, [loading]);

  useEffect(() => {
    if (!loading) { setProgress(0); return; }
    const start = Date.now();
    const total = 15000; // Match the API timeout
    const id = setInterval(() => {
      const elapsed = Date.now() - start;
      // Progress moves slower toward end to feel more natural
      const pct = Math.min((elapsed / total) * 85, 85);
      setProgress(pct);
      if (elapsed >= total) clearInterval(id);
    }, 50);
    return () => clearInterval(id);
  }, [loading]);

  const onDrop = useCallback((accepted: File[]) => {
    setFile(accepted[0]);
    setError(null);
  }, []);

  const { getRootProps, getInputProps, isDragActive } = useDropzone({
    onDrop,
    accept: { "image/*": [], "application/pdf": [] },
    maxFiles: 1,
  });

  const handleAnalyze = async () => {
    if (!file) return;
    setError(null);
    setLoading(true);

    try {
      const formData = new FormData();
      formData.append("file", file);
      formData.append("language", language);

      const res = await fetch("/api/analyze-report", {
        method: "POST",
        body: formData,
      });

      const data = await res.json();

      if (res.ok && data.is_readable !== undefined) {
        // Success — save to store
        setLatestReport(data as ParsedReport);
        setProgress(100);
        await new Promise((r) => setTimeout(r, 400));
        router.push("/dashboard");
      } else if (data.useMock || !res.ok) {
        // Backend failed or timed out — use mock data
        console.warn("Using mock fallback:", data.error);
        const mock = getNextMock();
        setLatestReport(mock);
        setProgress(100);
        await new Promise((r) => setTimeout(r, 400));
        router.push("/dashboard");
      }
    } catch {
      // Network error — use mock data
      console.warn("Network error, using mock fallback");
      const mock = getNextMock();
      setLatestReport(mock);
      setProgress(100);
      await new Promise((r) => setTimeout(r, 400));
      router.push("/dashboard");
    } finally {
      setLoading(false);
    }
  };

  return (
    <main
      className="min-h-screen flex flex-col items-center justify-center px-4 relative overflow-hidden"
      style={{ background: colors.bg }}
    >
      {/* Enhanced ambient glow */}
      <div className="fixed inset-0 pointer-events-none" style={{
        backgroundImage:
          "radial-gradient(ellipse at 20% 10%, rgba(255,153,51,0.07) 0%, transparent 55%), " +
          "radial-gradient(ellipse at 80% 80%, rgba(34,197,94,0.06) 0%, transparent 55%), " +
          "radial-gradient(ellipse at 55% 50%, rgba(99,102,241,0.03) 0%, transparent 60%)",
      }} />

      {/* Starfield */}
      {dots.map((dot, i) => (
        <div key={i} className="absolute rounded-full pointer-events-none"
          style={{
            left: `${dot.x}%`, top: `${dot.y}%`,
            width: dot.size, height: dot.size,
            background: "white", opacity: dot.opacity,
          }}
        />
      ))}

      {/* Loading overlay */}
      <AnimatePresence>
        {loading && (
          <motion.div
            className="fixed inset-0 flex flex-col items-center justify-center z-50"
            style={{ background: "rgba(13,13,26,0.97)" }}
            initial={{ opacity: 0 }}
            animate={{ opacity: 1 }}
          >
            {/* Animated pipeline */}
            <svg width="320" height="90" viewBox="0 0 320 90" className="mb-8">
              <rect x="10" y="20" width="70" height="50" rx="10"
                fill={colors.bgCard} stroke={colors.accent} strokeWidth="1.5" />
              <text x="45" y="42" textAnchor="middle" fontSize="18">📄</text>
              <text x="45" y="60" textAnchor="middle" fill={colors.textMuted} fontSize="9">Report</text>

              <line x1="80" y1="45" x2="240" y2="45"
                stroke={colors.border} strokeWidth="1.5" strokeDasharray="6 4" />
              {[0, 1, 2].map((i) => (
                <motion.circle key={i} r="5" fill={colors.accent} cy="45"
                  animate={{ cx: [80, 240], opacity: [0, 1, 1, 0] }}
                  transition={{ duration: 1.4, delay: i * 0.45, repeat: Infinity }} />
              ))}

              <circle cx="275" cy="45" r="32"
                fill={colors.bgCard} stroke={colors.accent} strokeWidth="1.5" />
              <text x="275" y="40" textAnchor="middle" fontSize="18">🧠</text>
              <text x="275" y="58" textAnchor="middle" fill={colors.textMuted} fontSize="9">AI Engine</text>
            </svg>

            <AnimatePresence mode="wait">
              <motion.p
                key={loadingStep}
                className="text-white text-sm font-medium text-center mb-6"
                initial={{ opacity: 0, y: 8 }}
                animate={{ opacity: 1, y: 0 }}
                exit={{ opacity: 0, y: -8 }}
                transition={{ duration: 0.3 }}
              >
                {LOADING_STEPS[loadingStep]}
              </motion.p>
            </AnimatePresence>

            {/* Progress bar */}
            <div className="w-64 h-1 rounded-full overflow-hidden" style={{ background: colors.border }}>
              <motion.div
                className="h-full rounded-full"
                style={{ background: `linear-gradient(90deg, #FF9933, #FFCC80)` }}
                animate={{ width: `${progress}%` }}
                transition={{ ease: "linear", duration: 0.05 }}
              />
            </div>
            <p className="text-xs mt-2" style={{ color: colors.textFaint }}>
              {Math.round(progress)}%
            </p>
          </motion.div>
        )}
      </AnimatePresence>

      {/* Main card */}
      <motion.div
        className="relative z-10 w-full max-w-md flex flex-col items-center"
        {...motionPresets.fadeUp}
        transition={{ duration: 0.5 }}
      >
        {/* Logo */}
        <div className="flex items-center gap-3 mb-4">
          <div
            className="w-12 h-12 rounded-2xl flex items-center justify-center text-2xl"
            style={{ background: "rgba(255,153,51,0.15)", border: "1px solid rgba(255,153,51,0.25)" }}
          >
            🏥
          </div>
          <div className="leading-none">
            <div className="text-xs font-semibold tracking-widest uppercase mb-0.5" style={{ color: colors.textMuted }}>
              Report
            </div>
            <h1 className="text-3xl font-black tracking-tight" style={{ color: "#FF9933" }}>
              Raahat
            </h1>
          </div>
        </div>

        <p className="text-base mb-1 text-center font-devanagari" style={{ color: colors.textSecondary }}>
          अपनी मेडिकल रिपोर्ट समझें — आसान हिंदी में
        </p>
        <p className="text-sm mb-6 text-center" style={{ color: colors.textMuted }}>
          Upload your report and understand it instantly
        </p>

        {/* Language toggle */}
        <div className="flex gap-1 mb-5 p-1 rounded-2xl w-full"
          style={{ background: colors.bgSubtle, border: `1px solid ${colors.border}` }}>
          {(["HI", "EN"] as const).map((lang) => (
            <button key={lang} onClick={() => useGUCStore.getState().setLanguage(lang)}
              className="flex-1 py-2.5 rounded-xl text-sm font-semibold transition-all duration-200"
              style={{
                background: language === lang ? colors.accent : "transparent",
                color: language === lang ? "#0d0d1a" : colors.textMuted,
                boxShadow: language === lang ? "0 2px 12px rgba(255,153,51,0.3)" : "none",
              }}>
              {lang === "HI" ? "🇮🇳 हिंदी" : "🌐 English"}
            </button>
          ))}
        </div>

        {/* Drop zone */}
        <div
          {...getRootProps()}
          className="w-full cursor-pointer rounded-2xl mb-4 transition-all duration-300"
          style={{
            border: `2px dashed ${isDragActive || file ? colors.accent : colors.accentBorder}`,
            background: isDragActive ? "rgba(255,153,51,0.05)" : colors.bgCard,
            padding: "36px 20px",
            boxShadow: isDragActive || file ? "0 0 24px rgba(255,153,51,0.15)" : "none",
          }}
        >
          <input {...getInputProps()} />
          <AnimatePresence mode="wait">
            {file ? (
              <motion.div key="file" className="flex flex-col items-center"
                initial={{ opacity: 0, scale: 0.92 }} animate={{ opacity: 1, scale: 1 }}>
                <div className="text-4xl mb-2"></div>
                <p className="text-white font-semibold">{file.name}</p>
                <p className="text-xs mt-1" style={{ color: colors.ok }}>
                  Ready! Click Samjho below ↓
                </p>
              </motion.div>
            ) : (
              <motion.div key="empty" className="flex flex-col items-center"
                initial={{ opacity: 0 }} animate={{ opacity: 1 }}>
                <motion.div className="mb-3 text-5xl"
                  animate={{ y: [0, -6, 0] }}
                  transition={{ duration: 2.5, repeat: Infinity }}>
                  📋
                </motion.div>
                <p className="text-white font-bold text-lg mb-1">
                  {isDragActive ? "Drop it here! 🎯" : "Drag & drop your report"}
                </p>
                <p className="text-sm" style={{ color: colors.textMuted }}>
                  or click to browse — PDF or Image
                </p>
                <p className="text-xs mt-2" style={{ color: colors.textFaint }}>
                  Supports: JPG, PNG, PDF
                </p>
              </motion.div>
            )}
          </AnimatePresence>
        </div>

        {/* Error message */}
        {error && (
          <p className="text-xs mb-3 text-center" style={{ color: "#EF4444" }}>
            {error}
          </p>
        )}

        {/* CTA button */}
        <motion.button
          onClick={handleAnalyze}
          disabled={loading || !file}
          className="w-full py-4 rounded-2xl text-base font-black disabled:opacity-50 transition-all"
          style={{
            background: "linear-gradient(135deg, #FF9933 0%, #FFAA55 100%)",
            color: "#0d0d1a",
            boxShadow: "0 4px 20px rgba(255,153,51,0.4)",
          }}
          whileHover={{ scale: 1.01, boxShadow: "0 6px 28px rgba(255,153,51,0.5)" }}
          whileTap={{ scale: 0.98 }}
        >
          ✨ Samjho! — समझो
        </motion.button>

        {/* Feature chips */}
        <div className="flex items-center gap-3 mt-5">
          {FEATURES.map((f) => (
            <div
              key={f.label}
              className="flex items-center gap-1.5 px-3 py-1.5 rounded-full text-xs font-medium"
              style={{ background: colors.bgSubtle, border: `1px solid ${colors.border}`, color: colors.textMuted }}
            >
              <span>{f.icon}</span>
              <span>{f.label}</span>
            </div>
          ))}
        </div>
      </motion.div>

      {/* Floating chat pill */}
      <motion.div
        className="fixed bottom-6 right-6 flex items-center gap-2 px-4 py-2.5 rounded-full cursor-pointer text-sm font-bold"
        style={{
          background: "rgba(255,153,51,0.15)",
          border: "1px solid rgba(255,153,51,0.3)",
          color: "#FF9933",
          backdropFilter: "blur(12px)",
        }}
        animate={{ y: [0, -4, 0] }}
        transition={{ duration: 2.5, repeat: Infinity }}
        whileHover={{ scale: 1.05, background: "rgba(255,153,51,0.25)" }}
      >
        <span>🤖</span>
        <span>Dr. Raahat</span>
      </motion.div>
    </main>
  );
}