import { useState, useEffect } from 'react';
import api from '../../store/auth';
import { useAuthStore } from '../../store/auth';
import { 
  Users, Mail, Phone, Eye, TrendingUp, Award, 
  AlertCircle, CheckCircle2, XCircle, X,
  ChevronRight, Search, Filter, Download,
  Sparkles, GraduationCap, BarChart3, Clock,
  MoreHorizontal, MessageSquare, Info, FileText
} from 'lucide-react';
import { StudentSocialDoc } from '../../components/StudentSocialDoc';

interface Student {
  id: number;
  first_name: string;
  last_name: string;
  email: string;
  phone: string;
  course: string;
  score: number;
  progress: number;
  status: 'excellent' | 'good' | 'attention';
  attendance: number;
  assignmentsCompleted: number;
  totalAssignments: number;
}

interface GradeItem {
  assignment: string;
  score: number;
  maxScore: number;
  date: string;
}

interface AttendanceItem {
  date: string;
  status: 'present' | 'absent' | 'excused';
}

export default function Students() {
  const { user } = useAuthStore();
  const [students, setStudents] = useState<Student[]>([]);
  const [selectedStudent, setSelectedStudent] = useState<Student | null>(null);
  const [showDetailsModal, setShowDetailsModal] = useState(false);
  const [activeTab, setActiveTab] = useState<'overview' | 'grades' | 'attendance' | 'assignments'>('overview');
  const [filterStatus, setFilterStatus] = useState<'all' | 'excellent' | 'good' | 'attention'>('all');
  const [searchQuery, setSearchQuery] = useState('');
  const [loading, setLoading] = useState(true);

  useEffect(() => {
    loadStudents();
  }, []);

  const loadStudents = async () => {
    setLoading(true);
    try {
      // In a real app, we'd fetch from an endpoint like /teacher/students
      // For now, using mock data but simulating an API call
      await new Promise(resolve => setTimeout(resolve, 800));
      setStudents(mockStudents);
    } catch (err) {
      console.error('Failed to load student directory');
    } finally {
      setLoading(false);
    }
  };

  const filteredStudents = students.filter(student => {
    const matchesSearch = 
      student.first_name.toLowerCase().includes(searchQuery.toLowerCase()) ||
      student.last_name.toLowerCase().includes(searchQuery.toLowerCase()) ||
      student.course.toLowerCase().includes(searchQuery.toLowerCase());
    const matchesStatus = filterStatus === 'all' || student.status === filterStatus;
    return matchesSearch && matchesStatus;
  });

  const openDetailsModal = (student: Student) => {
    setSelectedStudent(student);
    setShowDetailsModal(true);
    setActiveTab('overview');
  };

  const getStatusStyles = (status: Student['status']) => {
    switch (status) {
      case 'excellent': return { text: 'text-emerald-600 dark:text-emerald-400', bg: 'bg-emerald-50 dark:bg-emerald-900/30', border: 'border-emerald-100 dark:border-emerald-800', icon: Award };
      case 'good': return { text: 'text-blue-600 dark:text-blue-400', bg: 'bg-blue-50 dark:bg-blue-900/30', border: 'border-blue-100 dark:border-blue-800', icon: TrendingUp };
      case 'attention': return { text: 'text-amber-600 dark:text-amber-400', bg: 'bg-amber-50 dark:bg-amber-900/30', border: 'border-amber-100 dark:border-amber-800', icon: AlertCircle };
      default: return { text: 'text-text-muted', bg: 'bg-muted', border: 'border-border', icon: Info };
    }
  };

  const getStatColors = (color: string) => {
    switch (color) {
      case 'blue': return 'bg-blue-50 dark:bg-blue-900/20 text-blue-600 dark:text-blue-400 border-blue-100 dark:border-blue-800';
      case 'emerald': return 'bg-emerald-50 dark:bg-emerald-900/20 text-emerald-600 dark:text-emerald-400 border-emerald-100 dark:border-emerald-800';
      case 'purple': return 'bg-purple-50 dark:bg-purple-900/20 text-purple-600 dark:text-purple-400 border-purple-100 dark:border-purple-800';
      case 'orange': return 'bg-orange-50 dark:bg-orange-900/20 text-orange-600 dark:text-orange-400 border-orange-100 dark:border-orange-800';
      default: return 'bg-muted text-text-muted border-border';
    }
  };

  const stats = [
    { label: 'Managed Learners', value: students.length, icon: Users, color: 'blue', trend: 'Academic' },
    { label: 'Peak Performance', value: students.filter(s => s.status === 'excellent').length, icon: Award, color: 'emerald', trend: 'Excellent' },
    { label: 'Priority Support', value: students.filter(s => s.status === 'attention').length, icon: AlertCircle, color: 'orange', trend: 'Attention' },
    { label: 'Avg. Proficiency', value: `${students.length ? Math.round(students.reduce((acc, s) => acc + s.score, 0) / students.length) : 0}%`, icon: BarChart3, color: 'purple', trend: 'Optimal' },
  ];

  const [showSocialModal, setShowSocialModal] = useState(false);
  const [socialStudent, setSocialStudent] = useState<any>(null);

  const openSocialDoc = (student: any) => {
    setSocialStudent(student);
    setShowSocialModal(true);
  };

  return (
    <div className="space-y-10 animate-in fade-in duration-700 pb-10">
      {/* Header Section */}
      <div className="flex flex-col md:flex-row justify-between items-start md:items-center gap-6">
        <div>
          <h2 className="text-3xl font-black text-text tracking-tight text-capitalize dark:text-white uppercase">Student Directory</h2>
          <nav className="flex text-[0.65rem] font-black uppercase tracking-[2px] text-text-muted gap-2 items-center mt-1">
            <span>Faculty Portal</span>
            <ChevronRight size={12} className="text-text-muted/30" />
            <span className="text-primary">Learner Insights</span>
          </nav>
        </div>
        <div className="flex gap-3">
          <button className="flex items-center gap-2 px-5 py-2.5 bg-card border border-border text-text rounded-xl font-bold text-sm hover:bg-muted/50 transition-all shadow-sm active:scale-95">
            <Download size={18} />
            Export Directory
          </button>
          <button className="flex items-center gap-2 px-6 py-2.5 bg-primary text-white rounded-xl font-bold text-sm hover:bg-primary/90 transition-all shadow-lg shadow-blue-900/10 active:scale-95">
            <MessageSquare size={18} />
            Broadcast Notice
          </button>
        </div>
      </div>

      {/* Stats Bento Grid */}
      <div className="grid grid-cols-1 sm:grid-cols-2 lg:grid-cols-4 gap-4">
        {stats.map((stat) => (
          <div key={stat.label} className="bg-card border border-border p-5 rounded-2xl flex items-center gap-4 hover:shadow-md transition-all group cursor-default">
            <div className={`w-12 h-12 rounded-2xl flex items-center justify-center ${getStatColors(stat.color)} group-hover:scale-110 transition-transform`}>
              <stat.icon size={22} />
            </div>
            <div>
              <p className="text-[0.65rem] font-black uppercase tracking-wider text-text-muted">{stat.label}</p>
              <div className="flex items-baseline gap-2">
                <p className="text-2xl font-black text-text">{stat.value}</p>
                <span className="text-[0.6rem] font-bold text-text-muted">{stat.trend}</span>
              </div>
            </div>
          </div>
        ))}
      </div>

      {/* Directory Control Bar */}
      <div className="bg-card border border-border rounded-[2rem] p-2 shadow-sm flex flex-col lg:flex-row items-stretch lg:items-center gap-4">
        {/* Filter Tabs */}
        <div className="flex p-1 bg-muted/50 rounded-2xl flex-grow lg:flex-grow-0 min-w-0">
          {(['all', 'excellent', 'good', 'attention'] as const).map((status) => (
            <button
              key={status}
              onClick={() => setFilterStatus(status)}
              className={`
                flex-1 px-4 py-2.5 rounded-xl text-[0.7rem] font-black uppercase tracking-widest transition-all
                ${filterStatus === status 
                  ? 'bg-card text-primary shadow-sm ring-1 ring-border' 
                  : 'text-text-muted hover:text-text'}
              `}
            >
              {status}
            </button>
          ))}
        </div>

        {/* Search Bar */}
        <div className="relative group flex-1">
          <Search size={18} className="absolute left-4 top-1/2 -translate-y-1/2 text-text-muted group-focus-within:text-primary transition-colors" />
          <input 
            type="text" 
            className="w-full bg-muted/50 border border-border rounded-2xl pl-11 pr-4 py-3.5 text-sm font-medium focus:outline-none focus:ring-4 focus:ring-blue-500/5 focus:bg-card focus:border-primary transition-all text-text"
            placeholder="Search by learner name, email or assigned subject..."
            value={searchQuery}
            onChange={e => setSearchQuery(e.target.value)}
          />
        </div>

        <button className="px-5 py-3 bg-muted/50 hover:bg-muted text-text-muted rounded-2xl border border-border transition-all flex items-center justify-center gap-2">
          <Filter size={18} />
          <span className="text-[0.7rem] font-black uppercase tracking-widest text-text-muted">Class Section</span>
        </button>
      </div>

      {/* Student Grid */}
      {loading ? (
        <div className="py-20 text-center">
          <div className="w-12 h-12 border-4 border-primary border-t-transparent rounded-full animate-spin mx-auto mb-4"></div>
          <p className="text-text-muted font-bold uppercase tracking-widest text-xs italic">Accessing Learner Intelligence...</p>
        </div>
      ) : filteredStudents.length === 0 ? (
        <div className="py-24 text-center bg-card border-2 border-dashed border-border rounded-[3rem]">
          <Users size={64} className="mx-auto text-text-muted/30 mb-6" />
          <h3 className="text-2xl font-black text-text mb-2">No Matching Records</h3>
          <p className="text-text-muted font-medium mb-8 max-w-sm mx-auto">Try adjusting your filters or search query to identify specific learners.</p>
        </div>
      ) : (
        <div className="grid grid-cols-1 lg:grid-cols-2 gap-8">
          {filteredStudents.map((student) => {
            const styles = getStatusStyles(student.status);
            return (
              <div key={student.id} className="bg-card rounded-[2.5rem] border border-border overflow-hidden hover:shadow-xl hover:shadow-blue-900/5 transition-all group flex flex-col">
                <div className="p-8 flex flex-col sm:flex-row gap-8">
                  {/* Profile & Identification */}
                  <div className="flex flex-col items-center sm:items-start gap-4">
                    <div className="relative group/avatar">
                        <div className="w-24 h-24 rounded-3xl bg-muted border border-border flex items-center justify-center text-3xl font-black text-primary shadow-inner overflow-hidden group-hover:scale-105 transition-transform">
                            <img 
                                src={`https://i.pravatar.cc/150?u=${student.id}`} 
                                alt={student.first_name} 
                                className="w-full h-full object-cover"
                            />
                        </div>
                        <div className={`absolute -bottom-2 -right-2 w-8 h-8 rounded-xl border-4 border-card flex items-center justify-center shadow-lg ${styles.bg} ${styles.text}`}>
                            <styles.icon size={14} strokeWidth={3} />
                        </div>
                    </div>
                    <div className="text-center sm:text-left">
                        <span className={`inline-flex px-2 py-0.5 rounded-lg text-[0.55rem] font-black uppercase tracking-widest border mb-1 ${styles.bg} ${styles.text} ${styles.border}`}>
                            {student.status}
                        </span>
                        <h3 className="text-xl font-black text-text tracking-tight truncate max-w-[180px]">{student.first_name} {student.last_name}</h3>
                        <p className="text-[0.65rem] font-black text-text-muted uppercase tracking-widest mt-0.5">UID-2024-{student.id.toString().padStart(3, '0')}</p>
                    </div>
                  </div>

                  {/* Performance Snapshot */}
                  <div className="flex-grow space-y-6">
                    <div className="grid grid-cols-3 gap-3">
                        {[
                            { label: 'Score', val: `${student.score}%`, color: 'blue' },
                            { label: 'Progress', val: `${student.progress}%`, color: 'emerald' },
                            { label: 'Attend', val: `${student.attendance}%`, color: 'purple' },
                        ].map(m => (
                            <div key={m.label} className="bg-muted/50 rounded-2xl p-3 border border-border text-center">
                                <p className="text-[0.55rem] font-black uppercase tracking-widest text-text-muted mb-1">{m.label}</p>
                                <p className={`text-lg font-black ${m.color === 'blue' ? 'text-blue-600 dark:text-blue-400' : m.color === 'emerald' ? 'text-emerald-600 dark:text-emerald-400' : 'text-purple-600 dark:text-purple-400'}`}>{m.val}</p>
                            </div>
                        ))}
                    </div>

                    <div className="space-y-4">
                        <div className="flex justify-between items-end">
                            <p className="text-[0.6rem] font-black uppercase tracking-[2px] text-text-muted">Subject: <span className="text-primary">{student.course}</span></p>
                            <p className="text-[0.65rem] font-black text-text-muted uppercase tracking-tighter">
                                {student.assignmentsCompleted}/{student.totalAssignments} Tasks
                            </p>
                        </div>
                        <div className="w-full h-1.5 bg-muted rounded-full overflow-hidden p-0.5 shadow-inner">
                            <div 
                                className="h-full bg-primary rounded-full transition-all duration-1000"
                                style={{ width: `${student.progress}%` }}
                            />
                        </div>
                    </div>
                  </div>
                </div>

                {/* Footer Quick Actions */}
                <div className="px-8 py-6 bg-muted/50 border-t border-border flex items-center justify-between mt-auto shrink-0">
                    <div className="flex gap-2">
                        <a href={`mailto:${student.email}`} className="p-3 bg-card border border-border text-text-muted hover:text-primary hover:border-primary rounded-xl transition-all shadow-sm">
                            <Mail size={18} />
                        </a>
                        <a href={`tel:${student.phone}`} className="p-3 bg-card border border-border text-text-muted hover:text-primary hover:border-primary rounded-xl transition-all shadow-sm">
                            <Phone size={18} />
                        </a>
                        <button 
                            onClick={() => openSocialDoc(student)}
                            className="p-3 bg-card border border-border text-text-muted hover:text-primary hover:border-primary rounded-xl transition-all shadow-sm"
                            title="Social Document"
                        >
                            <Sparkles size={18} />
                        </button>
                    </div>
                    <button 
                        onClick={() => openDetailsModal(student)}
                        className="flex items-center gap-2 bg-[#002147] hover:bg-black text-white px-6 py-3 rounded-2xl font-black text-[0.65rem] uppercase tracking-widest transition-all shadow-lg shadow-blue-900/10 active:scale-95 group/btn"
                    >
                        Detailed Audit
                        <ChevronRight size={14} className="group-hover/btn:translate-x-1 transition-transform" />
                    </button>
                </div>
              </div>
            );
          })}
        </div>
      )}

      {/* Social Document Modal */}
      {showSocialModal && socialStudent && (
        <StudentSocialDoc 
          student={socialStudent} 
          onClose={() => setShowSocialModal(false)} 
        />
      )}

      {/* Details Modal */}
      {showDetailsModal && selectedStudent && (
        <div className="fixed inset-0 bg-[#002147]/80 backdrop-blur-md z-[150] flex items-center justify-center p-4">
          <div className="bg-card rounded-[3rem] w-full max-w-3xl shadow-2xl overflow-hidden animate-in zoom-in-95 duration-200 max-h-[90vh] flex flex-col border border-border">
            {/* Modal Header */}
            <div className="p-10 pb-8 bg-muted/50 border-b border-border flex items-center justify-between shrink-0 relative overflow-hidden">
                <div className="absolute right-[-20px] top-[-20px] opacity-[0.03] rotate-12">
                    <Sparkles size={180} />
                </div>
              <div className="relative z-10 flex items-center gap-6">
                <div className="w-20 h-20 rounded-[1.5rem] bg-card border border-border shadow-xl overflow-hidden flex-shrink-0">
                    <img src={`https://i.pravatar.cc/150?u=${selectedStudent.id}`} alt="Avatar" className="w-full h-full object-cover" />
                </div>
                <div>
                    <h2 className="text-2xl font-black text-text tracking-tight uppercase">{selectedStudent.first_name} {selectedStudent.last_name}</h2>
                    <div className="flex items-center gap-3 mt-1">
                        <span className="text-[0.65rem] font-black text-primary uppercase tracking-[3px]">{selectedStudent.course}</span>
                        <div className="w-1 h-1 rounded-full bg-border"></div>
                        <span className="text-[0.65rem] font-black text-text-muted uppercase tracking-widest">ID: EDU-{selectedStudent.id}</span>
                    </div>
                </div>
              </div>
              <button
                onClick={() => setShowDetailsModal(false)}
                className="relative z-10 w-12 h-12 flex items-center justify-center rounded-2xl bg-card border border-border text-text-muted hover:text-rose-500 transition-all shadow-sm"
              >
                <X size={24} />
              </button>
            </div>

            {/* Modal Navigation */}
            <div className="px-10 py-2 bg-card border-b border-border flex gap-8 shrink-0">
                {(['overview', 'grades', 'attendance', 'assignments'] as const).map(tab => (
                    <button 
                        key={tab}
                        onClick={() => setActiveTab(tab)}
                        className={`py-4 text-[0.65rem] font-black uppercase tracking-[3px] transition-all relative ${activeTab === tab ? 'text-primary' : 'text-text-muted hover:text-text'}`}
                    >
                        {tab}
                        {activeTab === tab && <div className="absolute bottom-0 left-0 right-0 h-1 bg-primary rounded-full"></div>}
                    </button>
                ))}
            </div>

            {/* Modal Body */}
            <div className="p-10 overflow-y-auto flex-1 custom-scrollbar">
                {activeTab === 'overview' && (
                    <div className="space-y-8 animate-in fade-in duration-500">
                        <div className="grid grid-cols-2 gap-6">
                            <div className="p-8 bg-blue-50/10 dark:bg-blue-900/10 border border-blue-100/20 rounded-3xl relative group overflow-hidden">
                                <div className="absolute right-0 top-0 w-24 h-24 bg-blue-500/5 rounded-bl-full"></div>
                                <p className="text-[0.6rem] font-black uppercase tracking-[2px] text-blue-600 dark:text-blue-400 mb-4">Academic Standing</p>
                                <div className="flex items-center gap-4">
                                    <div className="w-14 h-14 rounded-2xl bg-card flex items-center justify-center text-blue-600 dark:text-blue-400 shadow-sm border border-border text-2xl font-black">{selectedStudent.score}%</div>
                                    <p className="text-sm font-medium text-text-secondary leading-relaxed max-w-[200px]">Learner is currently in the top <span className="text-primary font-bold">12%</span> of this subject division.</p>
                                </div>
                            </div>
                            <div className="p-8 bg-emerald-50/10 dark:bg-emerald-900/10 border border-emerald-100/20 rounded-3xl relative group overflow-hidden">
                                <div className="absolute right-0 top-0 w-24 h-24 bg-emerald-500/5 rounded-bl-full"></div>
                                <p className="text-[0.6rem] font-black uppercase tracking-[2px] text-emerald-600 dark:text-emerald-400 mb-4">System Progress</p>
                                <div className="flex items-center gap-4">
                                    <div className="w-14 h-14 rounded-2xl bg-card flex items-center justify-center text-emerald-600 dark:text-emerald-400 shadow-sm border border-border text-2xl font-black">{selectedStudent.progress}%</div>
                                    <p className="text-sm font-medium text-text-secondary leading-relaxed max-w-[200px]">Curriculum completion is <span className="text-emerald-600 font-bold">Optimal</span> for this period.</p>
                                </div>
                            </div>
                        </div>

                        <div className="bg-muted/50 border border-border rounded-3xl p-8">
                            <h4 className="text-[0.65rem] font-black uppercase tracking-[3px] text-text-muted mb-6">Contact Channels</h4>
                            <div className="space-y-4">
                                <div className="flex items-center gap-4 p-4 bg-card rounded-2xl border border-border">
                                    <div className="w-10 h-10 rounded-xl bg-blue-50 dark:bg-blue-900/30 text-blue-600 dark:text-blue-400 flex items-center justify-center"><Mail size={18} /></div>
                                    <div>
                                        <p className="text-[0.6rem] font-black text-text-muted uppercase tracking-widest">Email Identity</p>
                                        <p className="text-sm font-bold text-text">{selectedStudent.email}</p>
                                    </div>
                                </div>
                                <div className="flex items-center gap-4 p-4 bg-card rounded-2xl border border-border">
                                    <div className="w-10 h-10 rounded-xl bg-emerald-50 dark:bg-emerald-900/30 text-emerald-600 dark:text-emerald-400 flex items-center justify-center"><Phone size={18} /></div>
                                    <div>
                                        <p className="text-[0.6rem] font-black text-text-muted uppercase tracking-widest">Direct Line</p>
                                        <p className="text-sm font-bold text-text">{selectedStudent.phone}</p>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                )}

                {activeTab === 'grades' && (
                    <div className="animate-in fade-in duration-500">
                        <div className="bg-card border border-border rounded-[2rem] overflow-hidden shadow-sm">
                            <table className="w-full text-left">
                                <thead>
                                    <tr className="bg-muted/80 border-b border-border">
                                        <th className="px-6 py-4 text-[0.6rem] font-black text-primary uppercase tracking-widest">Assessment Module</th>
                                        <th className="px-6 py-4 text-[0.6rem] font-black text-primary uppercase tracking-widest text-center">Score</th>
                                        <th className="px-6 py-4 text-[0.6rem] font-black text-primary uppercase tracking-widest text-right">Date Recorded</th>
                                    </tr>
                                </thead>
                                <tbody className="divide-y divide-border">
                                    {mockGrades.map((grade, i) => (
                                        <tr key={i} className="hover:bg-muted/50 transition-colors">
                                            <td className="px-6 py-4">
                                                <p className="text-sm font-bold text-text">{grade.assignment}</p>
                                                <p className="text-[0.6rem] font-black text-text-muted uppercase tracking-tighter">Academic Year 2024</p>
                                            </td>
                                            <td className="px-6 py-4 text-center">
                                                <span className={`px-3 py-1 rounded-full text-[0.6rem] font-black uppercase tracking-widest border ${
                                                    grade.score >= grade.maxScore * 0.8 ? 'bg-emerald-50 dark:bg-emerald-900/30 text-emerald-600 dark:text-emerald-400 border-emerald-100 dark:border-emerald-800' : 'bg-blue-50 dark:bg-blue-900/30 text-blue-600 dark:text-blue-400 border-blue-100 dark:border-blue-800'
                                                }`}>
                                                    {grade.score}/{grade.maxScore}
                                                </span>
                                            </td>
                                            <td className="px-6 py-4 text-right">
                                                <p className="text-[0.7rem] font-bold text-text-muted font-mono">{grade.date}</p>
                                            </td>
                                        </tr>
                                    ))}
                                </tbody>
                            </table>
                        </div>
                    </div>
                )}

                {activeTab === 'attendance' && (
                    <div className="space-y-8 animate-in fade-in duration-500">
                        <div className="grid grid-cols-7 gap-3 text-text">
                            {mockAttendance.map((record, i) => (
                                <div key={i} className={`p-5 text-center rounded-[1.5rem] border transition-all ${
                                    record.status === 'present' ? 'bg-emerald-50/10 dark:bg-emerald-900/10 border-emerald-100/20 dark:border-emerald-800/20' : 'bg-rose-50/10 dark:bg-rose-900/10 border-rose-100/20 dark:border-rose-800/20'
                                }`}>
                                    <p className="text-[0.6rem] font-black text-text-muted uppercase tracking-widest mb-3">{record.date.split('-')[2]}</p>
                                    <div className="flex justify-center">
                                        {record.status === 'present' ? <CheckCircle2 size={24} className="text-emerald-500" /> : <XCircle size={24} className="text-rose-500" />}
                                    </div>
                                </div>
                            ))}
                        </div>
                        <div className="bg-muted/50 p-6 rounded-3xl border border-border flex items-center justify-center gap-8">
                             {[
                                { icon: CheckCircle2, label: 'Present', color: 'text-emerald-500' },
                                { icon: XCircle, label: 'Absent', color: 'text-rose-500' },
                                { icon: AlertCircle, label: 'Excused', color: 'text-amber-500' },
                             ].map(l => (
                                <div key={l.label} className="flex items-center gap-2">
                                    <l.icon size={16} className={l.color} />
                                    <span className="text-[0.65rem] font-black uppercase tracking-[2px] text-text-muted">{l.label}</span>
                                </div>
                             ))}
                        </div>
                    </div>
                )}

                {activeTab === 'assignments' && (
                    <div className="animate-in fade-in duration-500">
                        <div className="space-y-3">
                            {mockAssignments_details.map((assignment, i) => (
                                <div key={i} className="flex items-center justify-between p-6 bg-card border border-border rounded-[2rem] hover:border-primary transition-all group">
                                    <div className="flex items-center gap-4">
                                        <div className="w-12 h-12 rounded-2xl bg-muted flex items-center justify-center text-primary border border-border shadow-inner group-hover:scale-110 transition-transform">
                                            <FileText size={22} />
                                        </div>
                                        <div>
                                            <p className="text-[0.95rem] font-black text-text tracking-tight">{assignment.title}</p>
                                            <p className="text-[0.6rem] font-black text-text-muted uppercase tracking-[2px] mt-0.5">Target: {assignment.dueDate}</p>
                                        </div>
                                    </div>
                                    <div className="flex items-center gap-4">
                                        {assignment.grade && (
                                            <span className="text-sm font-black text-primary bg-primary/5 px-3 py-1 rounded-xl border border-primary/10">{assignment.grade}</span>
                                        )}
                                        <span className={`px-3 py-1.5 rounded-xl text-[0.6rem] font-black uppercase tracking-widest border ${
                                            assignment.status === 'submitted' ? 'bg-emerald-50 dark:bg-emerald-900/30 text-emerald-600 dark:text-emerald-400 border-emerald-100 dark:border-emerald-800' : 'bg-amber-50 dark:bg-amber-900/30 text-amber-600 dark:text-amber-400 border-amber-100 dark:border-amber-800'
                                        }`}>
                                            {assignment.status}
                                        </span>
                                    </div>
                                </div>
                            ))}
                        </div>
                    </div>
                )}
            </div>

            {/* Modal Footer */}
            <div className="p-10 border-t border-border bg-muted/50 shrink-0 flex items-center justify-between">
                <div className="flex gap-4">
                   <button className="px-6 py-4 bg-card border border-border text-text-muted rounded-2xl font-black text-[0.65rem] uppercase tracking-widest hover:bg-muted/50 transition-all flex items-center gap-2">
                        <Printer size={16} /> Export Transcript
                   </button>
                </div>
                <button
                    onClick={() => setShowDetailsModal(false)}
                    className="px-12 py-4 bg-primary hover:bg-primary-dark text-white rounded-2xl font-black text-[0.7rem] uppercase tracking-widest transition-all shadow-xl active:scale-95"
                >
                    Close Profile
                </button>
            </div>
          </div>
        </div>
      )}

      <style>{`
        .custom-scrollbar::-webkit-scrollbar { width: 4px; }
        .custom-scrollbar::-webkit-scrollbar-track { background: transparent; }
        .custom-scrollbar::-webkit-scrollbar-thumb { background: rgba(0, 71, 171, 0.1); border-radius: 10px; }
        .custom-scrollbar::-webkit-scrollbar-thumb:hover { background: rgba(0, 71, 171, 0.2); }
        .dark .custom-scrollbar::-webkit-scrollbar-thumb { background: rgba(255, 255, 255, 0.1); }
      `}</style>
    </div>
  );
}

const mockStudents: Student[] = [
    { id: 1, first_name: 'Amara', last_name: 'Okonkwo', email: 'amara.okonkwo@school.edu', phone: '+234 803 456 7890', course: 'Mathematics', score: 92, progress: 85, status: 'excellent', attendance: 94, assignmentsCompleted: 12, totalAssignments: 14 },
    { id: 2, first_name: 'Kofi', last_name: 'Mensah', email: 'kofi.mensah@school.edu', phone: '+233 244 567 8901', course: 'English Literature', score: 78, progress: 72, status: 'good', attendance: 88, assignmentsCompleted: 10, totalAssignments: 14 },
    { id: 3, first_name: 'Fatima', last_name: 'Ibrahim', email: 'fatima.ibrahim@school.edu', phone: '+234 905 678 9012', course: 'Science', score: 65, progress: 58, status: 'attention', attendance: 76, assignmentsCompleted: 7, totalAssignments: 14 },
    { id: 4, first_name: 'Chidi', last_name: 'Okafor', email: 'chidi.okafor@school.edu', phone: '+234 816 789 0123', course: 'Mathematics', score: 88, progress: 82, status: 'excellent', attendance: 96, assignmentsCompleted: 13, totalAssignments: 14 },
    { id: 5, first_name: 'Grace', last_name: 'Mwangi', email: 'grace.mwangi@school.edu', phone: '+254 712 345 6789', course: 'History', score: 74, progress: 68, status: 'good', attendance: 90, assignmentsCompleted: 9, totalAssignments: 14 },
    { id: 6, first_name: 'Samuel', last_name: 'Tadesse', email: 'samuel.tadesse@school.edu', phone: '+251 911 234 5678', course: 'Physics', score: 82, progress: 78, status: 'good', attendance: 92, assignmentsCompleted: 11, totalAssignments: 14 },
    { id: 7, first_name: 'Zainab', last_name: 'Hassan', email: 'zainab.hassan@school.edu', phone: '+234 701 234 5678', course: 'Chemistry', score: 56, progress: 48, status: 'attention', attendance: 70, assignmentsCompleted: 5, totalAssignments: 14 },
    { id: 8, first_name: 'Emmanuel', last_name: 'Ndlovu', email: 'emmanuel.ndlovu@school.edu', phone: '+263 771 234 567', course: 'Biology', score: 90, progress: 88, status: 'excellent', attendance: 98, assignmentsCompleted: 14, totalAssignments: 14 },
];

const mockGrades: GradeItem[] = [
    { assignment: 'Algebra Quiz 1', score: 18, maxScore: 20, date: '2026-05-15' },
    { assignment: 'Geometry Test', score: 45, maxScore: 50, date: '2026-05-10' },
    { assignment: 'Math Homework 1', score: 19, maxScore: 20, date: '2026-05-08' },
    { assignment: 'Linear Equations', score: 24, maxScore: 25, date: '2026-05-01' },
    { assignment: 'Probability Quiz', score: 17, maxScore: 20, date: '2026-04-28' },
];

const mockAttendance: AttendanceItem[] = [
    { date: '2026-06-01', status: 'present' },
    { date: '2026-05-31', status: 'present' },
    { date: '2026-05-29', status: 'present' },
    { date: '2026-05-28', status: 'absent' },
    { date: '2026-05-27', status: 'present' },
    { date: '2026-05-26', status: 'present' },
];

const mockAssignments_details = [
    { title: 'Algebra Problem Set', dueDate: '2026-06-05', status: 'submitted', grade: 18 },
    { title: 'Geometry Project', dueDate: '2026-06-08', status: 'submitted', grade: null },
    { title: 'Math Quiz Preparation', dueDate: '2026-06-10', status: 'pending', grade: null },
];

// Helper icon not in main list
const Printer = ({ size, className }: any) => <svg width={size} height={size} viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round" className={className}><polyline points="6 9 6 2 18 2 18 9"></polyline><path d="M6 18H4a2 2 0 0 1-2-2v-5a2 2 0 0 1 2-2h16a2 2 0 0 1 2 2v5a2 2 0 0 1-2 2h-2"></path><rect x="6" y="14" width="12" height="8"></rect></svg>;
