'use client'; /* eslint-disable @typescript-eslint/no-explicit-any */ import { useState, useEffect, useTransition } from 'react'; import { useSession } from 'next-auth/react'; import { Card, CardContent, CardHeader, CardTitle } from "@workspace/ui/components/card" import { Button } from "@workspace/ui/components/button" import { Badge } from "@workspace/ui/components/badge" import { Separator } from "@workspace/ui/components/separator" import { Input } from "@workspace/ui/components/input" import { Label } from "@workspace/ui/components/label" import { Textarea } from "@workspace/ui/components/textarea" import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from "@workspace/ui/components/select" import { User, Mail, Phone, MapPin, Calendar, GraduationCap, BookOpen, Award, Edit, Save, Camera, Linkedin, Github, FileText, Download, Upload, CheckCircle, AlertCircle, Briefcase, Plus, Trash2, ExternalLink, X } from "lucide-react" import { getStudentProfile, updateStudentProfile } from "../actions" // Optional fallback student object to avoid UI crashes during development const mockStudent = null; export default function ProfilePage() { const [student, setStudent] = useState(mockStudent); const [isLoading, setIsLoading] = useState(true); const [error, setError] = useState(null); const [editingSection, setEditingSection] = useState(null); const [editData, setEditData] = useState({}); const [isSaving, setIsSaving] = useState(false); const [editingSkills, setEditingSkills] = useState([]); const [newSkill, setNewSkill] = useState(''); const [editingGrades, setEditingGrades] = useState([]); const [editingInternships, setEditingInternships] = useState([]); const [editingResumes, setEditingResumes] = useState([]); const [successMessage, setSuccessMessage] = useState(null); const [isPending, startTransition] = useTransition(); const [newInternship, setNewInternship] = useState({ title: '', company: '', location: '', startDate: '', endDate: '', description: '' }); const [newResume, setNewResume] = useState({ title: '', link: '' }); const [isDragOver, setIsDragOver] = useState(false); const { data: session, status } = useSession(); useEffect(() => { if (status === 'authenticated' && session?.user?.studentId) { loadStudentProfile(session.user.studentId); } }, [status, session]); const loadStudentProfile = async (id: number) => { try { const result = await getStudentProfile(id); if (result.success && result.student) { setStudent(result.student as any); } else { setError(result.error || 'Failed to load profile'); } } catch (err) { setError('Failed to load profile data'); } finally { setIsLoading(false); } }; const handleEdit = (section: string) => { setEditingSection(section); setEditData({}); setError(null); // Clear any previous errors }; const handleCancel = () => { setEditingSection(null); setEditData({}); }; const handleSave = async (section: string) => { startTransition(async () => { try { const result = await updateStudentProfile(student.id, editData); if (result.success) { setStudent((prev: Record | null) => ({ ...(prev || {}), ...editData })); setEditingSection(null); setEditData({}); setSuccessMessage('Profile updated successfully!'); setTimeout(() => setSuccessMessage(null), 3000); } else { setError(result.error || 'Failed to update profile'); } } catch (err) { setError('Failed to update profile'); } }); }; const handleInputChange = (field: string, value: string) => { setEditData((prev: Record) => ({ ...prev, [field]: value })); }; const handleInputChangeWithType = (field: string, value: string, type: 'string' | 'number' | 'boolean' = 'string') => { let processedValue: any = value; if (type === 'number') { processedValue = parseFloat(value) || 0; } else if (type === 'boolean') { processedValue = value === 'true'; } setEditData((prev: Record) => ({ ...prev, [field]: processedValue })); }; const addSkill = () => { if (newSkill.trim() && !editingSkills.includes(newSkill.trim())) { setEditingSkills([...editingSkills, newSkill.trim()]); setNewSkill(''); } }; const removeSkill = (skillToRemove: string) => { setEditingSkills(editingSkills.filter(skill => skill !== skillToRemove)); }; const saveSkills = async () => { startTransition(async () => { try { const result = await updateStudentProfile(student.id, { skills: editingSkills }); if (result.success) { setStudent((prev: Record | null) => ({ ...(prev || {}), skills: editingSkills })); setEditingSection(null); setSuccessMessage('Skills updated successfully!'); setTimeout(() => setSuccessMessage(null), 3000); } else { setError(result.error || 'Failed to update skills'); } } catch (err) { setError('Failed to update skills'); } }); }; const handleEditSkills = () => { setEditingSkills([...(student.skills || [])]); setEditingSection('skills'); setError(null); // Clear any previous errors }; const handleEditGrades = () => { // Initialize with existing grades or create default grades for 8 semesters const existingGrades = student.grades || []; const defaultGrades = []; for (let sem = 1; sem <= 8; sem++) { const existingGrade = existingGrades.find((g: any) => g.sem === sem); defaultGrades.push({ sem, sgpi: existingGrade?.sgpi || 0, isKT: existingGrade?.isKT || false, deadKT: existingGrade?.deadKT || false, }); } setEditingGrades(defaultGrades); setEditingSection('grades'); setError(null); // Clear any previous errors }; const handleEditInternships = () => { setEditingInternships([...(student.internships || [])]); setEditingSection('internships'); setError(null); // Clear any previous errors }; const handleEditResumes = () => { setEditingResumes([...(student.resumes || [])]); setEditingSection('resumes'); setError(null); // Clear any previous errors }; const updateGrade = (sem: number, field: string, value: any) => { setEditingGrades(prev => prev.map(grade => grade.sem === sem ? { ...grade, [field]: value } : grade ) ); }; const addInternship = () => { if (newInternship.title && newInternship.company) { setEditingInternships([...editingInternships, { ...newInternship, id: Date.now() }]); setNewInternship({ title: '', company: '', location: '', startDate: '', endDate: '', description: '' }); } }; const removeInternship = (id: number) => { setEditingInternships(editingInternships.filter(intern => intern.id !== id)); }; const addResume = () => { if (newResume.title && newResume.link) { setEditingResumes([...editingResumes, { ...newResume, id: Date.now() }]); setNewResume({ title: '', link: '' }); } }; const removeResume = (id: number) => { setEditingResumes(editingResumes.filter(resume => resume.id !== id)); }; const saveGrades = async () => { startTransition(async () => { try { const result = await updateStudentProfile(student.id, { section: 'grades', grades: editingGrades }); if (result.success) { setStudent((prev: Record | null) => ({ ...(prev || {}), grades: editingGrades })); setEditingSection(null); setSuccessMessage('Grades updated successfully!'); setTimeout(() => setSuccessMessage(null), 3000); } else { setError(result.error || 'Failed to update grades'); } } catch (err) { setError('Failed to update grades'); } }); }; const saveInternships = async () => { startTransition(async () => { try { const result = await updateStudentProfile(student.id, { section: 'internships', internships: editingInternships }); if (result.success) { setStudent((prev: Record | null) => ({ ...(prev || {}), internships: editingInternships })); setEditingSection(null); setSuccessMessage('Internships updated successfully!'); setTimeout(() => setSuccessMessage(null), 3000); } else { setError(result.error || 'Failed to update internships'); } } catch (err) { setError('Failed to update internships'); } }); }; const saveResumes = async () => { startTransition(async () => { try { const result = await updateStudentProfile(student.id, { section: 'resumes', resumes: editingResumes }); if (result.success) { setStudent((prev: Record | null) => ({ ...(prev || {}), resumes: editingResumes })); setEditingSection(null); setSuccessMessage('Resumes updated successfully!'); setTimeout(() => setSuccessMessage(null), 3000); } else { setError(result.error || 'Failed to update resumes'); } } catch (err) { setError('Failed to update resumes'); } }); }; const handleDragOver = (e: React.DragEvent) => { e.preventDefault(); setIsDragOver(true); }; const handleDragLeave = (e: React.DragEvent) => { e.preventDefault(); setIsDragOver(false); }; const handleDrop = (e: React.DragEvent) => { e.preventDefault(); setIsDragOver(false); const files = Array.from(e.dataTransfer.files); files.forEach(file => { if (file.type === 'application/pdf' || file.name.endsWith('.pdf')) { // In a real app, you'd upload to cloud storage and get a URL // For now, we'll create a mock URL const mockUrl = URL.createObjectURL(file); const newResumeItem = { id: Date.now(), title: file.name, link: mockUrl }; setEditingResumes(prev => [...prev, newResumeItem]); } }); }; const handleFileSelect = (e: React.ChangeEvent) => { const files = Array.from(e.target.files || []); files.forEach(file => { if (file.type === 'application/pdf' || file.name.endsWith('.pdf')) { const mockUrl = URL.createObjectURL(file); const newResumeItem = { id: Date.now(), title: file.name, link: mockUrl }; setEditingResumes(prev => [...prev, newResumeItem]); } }); }; if (isLoading || status === 'loading') { return (

Loading profile...

); } if (!student) { return (

No profile data found.

); } return (
{/* Header */}

My Profile

Manage your personal information and academic details

{error && (
{error}
)} {successMessage && (
{successMessage}
)}
{/* Profile Overview */}
{student.firstName?.[0] || 'S'}{student.lastName?.[0] || 'T'}

{student.firstName} {student.middleName} {student.lastName}

{student.email}

{student.verified ? ( <> Verified ) : ( <> Pending Verification )} {student.markedOut && ( Marked Out )}
Roll Number: {student.rollNumber}
{/* Quick Stats */}
Academic Year {student.year}
Branch {student.branch}
SSC Score {student.ssc}/100
HSC Score {student.hsc}/100
{/* Social Links */}

Social Links

{student.linkedin && ( )} {student.github && ( )} {(!student.linkedin && !student.github) && (

No social links added yet

)}
{/* Main Content */}
{/* Personal Information */}
Personal Information {editingSection !== 'personal' ? ( ) : (
)}
{editingSection === 'personal' ? ( handleInputChange('firstName', e.target.value)} /> ) : ( )}
{editingSection === 'personal' ? ( handleInputChange('middleName', e.target.value)} /> ) : ( )}
{editingSection === 'personal' ? ( handleInputChange('lastName', e.target.value)} /> ) : ( )}
{editingSection === 'personal' ? ( handleInputChange('mothersName', e.target.value)} /> ) : ( )}
{editingSection === 'personal' ? ( handleInputChange('mothersEmail', e.target.value)} placeholder="mother@example.com" /> ) : ( )}
{editingSection === 'personal' ? ( handleInputChange('mothersPhone', e.target.value)} placeholder="10-digit number" /> ) : ( )}
{editingSection === 'personal' ? ( handleInputChange('fathersName', e.target.value)} /> ) : ( )}
{editingSection === 'personal' ? ( handleInputChange('fathersEmail', e.target.value)} placeholder="father@example.com" /> ) : ( )}
{editingSection === 'personal' ? ( handleInputChange('fathersPhone', e.target.value)} placeholder="10-digit number" /> ) : ( )}
{editingSection === 'personal' ? ( handleInputChange('personalGmail', e.target.value)} /> ) : ( )}
{editingSection === 'personal' ? ( handleInputChange('phoneNumber', e.target.value)} /> ) : ( )}
{editingSection === 'personal' ? ( handleInputChange('gender', e.target.value)} /> ) : ( )}
{editingSection === 'personal' ? ( handleInputChange('dob', e.target.value)} /> ) : ( )}
{editingSection === 'personal' ? (