"use client";

import { useState } from "react";
import Link from "next/link";

interface DeptConfig {
  id: string;
  category: 'all' | 'medicine' | 'nursing' | 'allied' | 'admin_volunteer';
  categoryLabel: string;
  name: string;
  icon: string;
  color: string;
  description: string;
  sections: string[];
  href: string;
}

const allDepartments: DeptConfig[] = [
  // أطباء الامتياز
  { 
    id: 'pediatrics', 
    category: 'medicine',
    categoryLabel: 'أطباء الامتياز',
    name: 'قسم الأطفال', 
    icon: '👶', 
    color: '#3498db',
    description: 'Clinic, PICU, NICU, PW - جميع عيادات وأقسام الأطفال',
    sections: ['Clinic', 'PICU', 'NICU', 'PW'],
    href: '/schedule/pediatrics'
  },
  { 
    id: 'surgery', 
    category: 'medicine',
    categoryLabel: 'أطباء الامتياز',
    name: 'قسم الجراحة العامة', 
    icon: '🏥', 
    color: '#e74c3c',
    description: 'OR, Clinic, ER, Ward, ICU - غرف العمليات والأقسام الجراحية',
    sections: ['OR', 'Clinic', 'ER', 'Ward', 'ICU'],
    href: '/schedule/surgery'
  },
  { 
    id: 'gynecology', 
    category: 'medicine',
    categoryLabel: 'أطباء الامتياز',
    name: 'قسم النسائية والتوليد', 
    icon: '👩‍⚕️', 
    color: '#9b59b6',
    description: 'Delivery, Clinic, OR, ER, Ward - الولادة والعيادات والعمليات',
    sections: ['Delivery', 'Clinic', 'OR', 'ER', 'Ward'],
    href: '/schedule/gynecology'
  },
  {
    id: 'contracted_doctors',
    category: 'medicine',
    categoryLabel: 'أطباء الامتياز',
    name: 'الأطباء المتعاقدون مع الجامعات',
    icon: '🩺',
    color: '#4338ca',
    description: 'جداول وتوزيع الأطباء المتعاقدين في مختلف الأقسام التخصصية',
    sections: ['عيادات تخصصية', 'طوارئ', 'أقسام منومة', 'مناوبات ليلية'],
    href: '/schedule/editor?dept=surgery'
  },

  // التمريض
  {
    id: 'nursing_general',
    category: 'nursing',
    categoryLabel: 'التمريض',
    name: 'قسم التمريض العام',
    icon: '💉',
    color: '#0d9488',
    description: 'شفتات ومناوبات كادر التمريض العام في الطوابق والأقسام',
    sections: ['شفت A (صباحي)', 'شفت B (مسائي)', 'شفت C (ليلي)', 'طوارئ'],
    href: '/schedule/editor?dept=pediatrics'
  },
  {
    id: 'nursing_uni',
    category: 'nursing',
    categoryLabel: 'التمريض',
    name: 'تدريب تمريض الجامعات',
    icon: '🎓',
    color: '#0284c7',
    description: 'توزيع جداول تدريب طلاب التمريض القادمين من الجامعات الشريكة',
    sections: ['أجنحة الجراحة', 'أجنحة الباطني', 'العناية المركزة', 'الطوارئ'],
    href: '/schedule/editor?dept=pediatrics'
  },
  {
    id: 'nursing_intern',
    category: 'nursing',
    categoryLabel: 'التمريض',
    name: 'تمريض امتياز وتدريب',
    icon: '🩺',
    color: '#7c3aed',
    description: 'مناوبات وجداول خريجي وطلاب تمريض الامتياز والتدريب الميداني',
    sections: ['شفت صباحي', 'شفت مسائي', 'شفت ليلي', 'عنايات خاصة'],
    href: '/schedule/editor?dept=pediatrics'
  },

  // المهن الطبية المساندة
  {
    id: 'pt',
    category: 'allied',
    categoryLabel: 'المهن الطبية المساندة',
    name: 'العلاج الطبيعي والتأهيل',
    icon: '🏃',
    color: '#27ae60',
    description: 'مواعيد وجلسات وتوزيع أخصائيي ومتدربي العلاج الطبيعي',
    sections: ['قسم الرجال', 'قسم النساء', 'تأهيل الأطفال', 'العناية الحثيثة'],
    href: '/schedule/editor?dept=surgery'
  },
  {
    id: 'radiology',
    category: 'allied',
    categoryLabel: 'المهن الطبية المساندة',
    name: 'الأشعة والتصوير الطبي',
    icon: '☢️',
    color: '#455a64',
    description: 'مناوبات فنيي ومتدربي الأشعة العادية، المقطعية، والسونار',
    sections: ['X-Ray سينية', 'CT مقطعية', 'طوارئ 24 ساعة'],
    href: '/schedule/editor?dept=surgery'
  },
  {
    id: 'lab',
    category: 'allied',
    categoryLabel: 'المهن الطبية المساندة',
    name: 'المختبرات الطبية وبنك الدم',
    icon: '🧪',
    color: '#0284c7',
    description: 'توزيع فنيي التحاليل الطبية وسحب الدم ومناوبات بنك الدم',
    sections: ['سحب الدم', 'فحوصات الروتين', 'بنك الدم', 'طوارئ المختبر'],
    href: '/schedule/editor?dept=pediatrics'
  },
  {
    id: 'ambulance',
    category: 'allied',
    categoryLabel: 'المهن الطبية المساندة',
    name: 'ضباط الإسعاف والطوارئ',
    icon: '🚑',
    color: '#ea580c',
    description: 'مناوبات وشفتات طواقم الإسعاف الميداني وغرفة العمليات المركزية',
    sections: ['وردية A', 'وردية B', 'وردية C', 'تدخل سريع'],
    href: '/schedule/editor?dept=surgery'
  },

  // الإدارة والخدمات والتطوع
  {
    id: 'admin_services',
    category: 'admin_volunteer',
    categoryLabel: 'الإدارة والتطوع',
    name: 'الإدارة، المحاسبة، والسكرتارية الطبية',
    icon: '💼',
    color: '#2c3e50',
    description: 'جداول دوام الإدارة، شؤون الموظفين، المحاسبة، والسجلات الطبية',
    sections: ['الاستقبال والتسجيل', 'المحاسبة', 'السجلات والملفات', 'الشؤون الإدارية'],
    href: '/schedule/editor?dept=pediatrics'
  },
  {
    id: 'volunteer_all',
    category: 'admin_volunteer',
    categoryLabel: 'الإدارة والتطوع',
    name: 'شؤون المتطوعين (طبي، تمريض، ومجتمعي)',
    icon: '🤝',
    color: '#059669',
    description: 'توزيع وجداول المتطوعين والعمل المجتمعي بكافة أروقة المستشفى',
    sections: ['تطوع طبي', 'تطوع تمريض', 'إرشاد المرضى', 'دعم مجتمعي'],
    href: '/schedule/editor?dept=pediatrics'
  }
];

export default function ScheduleSelectionPage() {
  const [activeCategory, setActiveCategory] = useState<'all' | 'medicine' | 'nursing' | 'allied' | 'admin_volunteer'>('all');

  const filteredDepts = activeCategory === 'all' 
    ? allDepartments 
    : allDepartments.filter(d => d.category === activeCategory);

  return (
    <div className="dashboard-container" style={{ maxWidth: '1280px', margin: '0 auto', padding: '20px' }}>
      {/* Header */}
      <div style={{
        background: 'linear-gradient(135deg, #1e1b4b 0%, #312e81 40%, #4338ca 100%)',
        borderRadius: '28px',
        padding: '50px 30px',
        marginBottom: '35px',
        color: 'white',
        position: 'relative',
        overflow: 'hidden',
        boxShadow: '0 20px 60px rgba(49, 46, 129, 0.4)',
        textAlign: 'center',
        border: '1px solid rgba(255,255,255,0.1)'
      }}>
        <div style={{
          position: 'absolute',
          top: 0,
          left: 0,
          right: 0,
          bottom: 0,
          background: 'radial-gradient(circle at 20% 80%, rgba(255,255,255,0.12) 0%, transparent 50%)',
          pointerEvents: 'none'
        }} />
        
        <Link href="/" style={{
          position: 'absolute',
          top: '20px',
          right: '20px',
          background: 'rgba(255,255,255,0.18)',
          padding: '10px 20px',
          borderRadius: '12px',
          color: 'white',
          textDecoration: 'none',
          display: 'flex',
          alignItems: 'center',
          gap: '8px',
          backdropFilter: 'blur(10px)',
          fontWeight: 700,
          fontSize: '0.9rem',
          transition: 'all 0.3s'
        }}>
          <i className="fas fa-arrow-right"></i>
          العودة للرئيسية
        </Link>
        
        <div style={{ position: 'relative', zIndex: 1 }}>
          <div style={{ fontSize: '4.5rem', marginBottom: '12px' }}>📅</div>
          <h1 style={{ fontSize: '2.6rem', fontWeight: '900', marginBottom: '14px', letterSpacing: '-0.5px' }}>
            توزيع جدول الدوام ومولد المناوبات لجميع التخصصات ✨
          </h1>
          <p style={{ fontSize: '1.2rem', opacity: 0.9, maxWidth: '750px', margin: '0 auto', lineHeight: 1.6 }}>
            نظام شامل لإنشاء وإدارة وتوزيع جداول المناوبات والشفتات لكافة التخصصات الطبية، التمريضية، المهن المساندة والإدارية
          </p>
        </div>
      </div>

      {/* Category Filter Tabs */}
      <div style={{
        display: 'flex',
        gap: '10px',
        flexWrap: 'wrap',
        justifyContent: 'center',
        marginBottom: '35px'
      }}>
        {[
          { key: 'all', label: `جميع التخصصات (${allDepartments.length})`, icon: 'fa-th-large' },
          { key: 'medicine', label: 'أطباء الامتياز والمتعاقدون', icon: 'fa-stethoscope' },
          { key: 'nursing', label: 'التمريض وتدريب الجامعات', icon: 'fa-user-nurse' },
          { key: 'allied', label: 'المهن الطبية المساندة', icon: 'fa-x-ray' },
          { key: 'admin_volunteer', label: 'الإدارة والخدمات والتطوع', icon: 'fa-hands-helping' },
        ].map(tab => {
          const isSelected = activeCategory === tab.key;
          return (
            <button
              key={tab.key}
              onClick={() => setActiveCategory(tab.key as any)}
              style={{
                padding: '12px 22px',
                borderRadius: '14px',
                border: isSelected ? '2px solid #4338ca' : '1.5px solid #e2e8f0',
                background: isSelected ? '#4338ca' : 'white',
                color: isSelected ? 'white' : '#475569',
                fontFamily: 'Tajawal',
                fontWeight: 800,
                fontSize: '0.95rem',
                cursor: 'pointer',
                display: 'inline-flex',
                alignItems: 'center',
                gap: '8px',
                boxShadow: isSelected ? '0 8px 20px rgba(67, 56, 202, 0.3)' : '0 2px 6px rgba(0,0,0,0.04)',
                transition: 'all 0.25s ease'
              }}
            >
              <i className={`fas ${tab.icon}`}></i>
              {tab.label}
            </button>
          );
        })}
      </div>

      {/* Department Cards Grid */}
      <div style={{
        display: 'grid',
        gridTemplateColumns: 'repeat(auto-fit, minmax(340px, 1fr))',
        gap: '24px',
        marginBottom: '45px'
      }}>
        {filteredDepts.map(dept => (
          <Link 
            key={dept.id}
            href={dept.href}
            style={{
              textDecoration: 'none',
              color: 'inherit'
            }}
          >
            <div style={{
              background: 'white',
              borderRadius: '22px',
              padding: '32px 28px',
              boxShadow: '0 8px 30px rgba(0,0,0,0.06)',
              transition: 'all 0.35s cubic-bezier(0.4, 0, 0.2, 1)',
              cursor: 'pointer',
              border: `2.5px solid transparent`,
              position: 'relative',
              overflow: 'hidden',
              display: 'flex',
              flexDirection: 'column',
              height: '100%'
            }}
            onMouseOver={(e) => {
              e.currentTarget.style.transform = 'translateY(-8px) scale(1.015)';
              e.currentTarget.style.boxShadow = `0 20px 45px ${dept.color}35`;
              e.currentTarget.style.borderColor = dept.color;
            }}
            onMouseOut={(e) => {
              e.currentTarget.style.transform = 'translateY(0) scale(1)';
              e.currentTarget.style.boxShadow = '0 8px 30px rgba(0,0,0,0.06)';
              e.currentTarget.style.borderColor = 'transparent';
            }}
            >
              {/* Color accent bar */}
              <div style={{
                position: 'absolute',
                top: 0,
                left: 0,
                right: 0,
                height: '6px',
                background: `linear-gradient(90deg, ${dept.color}, ${dept.color}aa)`
              }} />

              {/* Category tag */}
              <div style={{
                display: 'inline-block',
                alignSelf: 'flex-start',
                background: `${dept.color}15`,
                color: dept.color,
                padding: '4px 12px',
                borderRadius: '8px',
                fontSize: '0.78rem',
                fontWeight: 800,
                marginBottom: '16px'
              }}>
                {dept.categoryLabel}
              </div>

              <div style={{ textAlign: 'center', flex: 1, display: 'flex', flexDirection: 'column' }}>
                <div style={{ 
                  fontSize: '3.6rem', 
                  marginBottom: '16px',
                  filter: 'drop-shadow(0 4px 8px rgba(0,0,0,0.08))'
                }}>
                  {dept.icon}
                </div>
                
                <h2 style={{ 
                  fontSize: '1.5rem', 
                  fontWeight: '800', 
                  color: dept.color,
                  marginBottom: '10px'
                }}>
                  {dept.name}
                </h2>
                
                <p style={{ 
                  color: '#64748b', 
                  marginBottom: '20px',
                  fontSize: '0.92rem',
                  lineHeight: '1.6',
                  flex: 1
                }}>
                  {dept.description}
                </p>

                {/* Sections pills */}
                <div style={{ 
                  display: 'flex', 
                  flexWrap: 'wrap', 
                  gap: '6px', 
                  justifyContent: 'center',
                  marginBottom: '24px'
                }}>
                  {dept.sections.map(section => (
                    <span key={section} style={{
                      background: `${dept.color}12`,
                      color: dept.color,
                      padding: '5px 12px',
                      borderRadius: '16px',
                      fontSize: '0.8rem',
                      fontWeight: '700'
                    }}>
                      {section}
                    </span>
                  ))}
                </div>

                <div style={{
                  background: `linear-gradient(135deg, ${dept.color}, ${dept.color}dd)`,
                  color: 'white',
                  padding: '12px 24px',
                  borderRadius: '12px',
                  fontWeight: '800',
                  fontSize: '0.95rem',
                  display: 'inline-flex',
                  alignItems: 'center',
                  justifyContent: 'center',
                  gap: '10px',
                  boxShadow: `0 4px 15px ${dept.color}30`
                }}>
                  <span>توزيع وإنشاء الجدول</span>
                  <span style={{ fontSize: '1.1rem' }}>←</span>
                </div>
              </div>
            </div>
          </Link>
        ))}
      </div>

      {/* Quick Stats */}
      <div style={{
        background: 'white',
        borderRadius: '24px',
        padding: '35px',
        boxShadow: '0 10px 40px rgba(0,0,0,0.06)',
        border: '1px solid #f1f5f9'
      }}>
        <h3 style={{ 
          marginBottom: '22px', 
          color: 'var(--text)',
          display: 'flex',
          alignItems: 'center',
          gap: '10px',
          fontWeight: 800,
          fontSize: '1.3rem'
        }}>
          <span style={{ fontSize: '1.5rem' }}>📊</span>
          ميزات توزيع الجداول لكافة التخصصات
        </h3>
        <div style={{
          display: 'grid',
          gridTemplateColumns: 'repeat(auto-fit, minmax(220px, 1fr))',
          gap: '18px'
        }}>
          <div style={{ 
            padding: '22px', 
            background: '#f8fafc', 
            borderRadius: '16px',
            textAlign: 'center',
            border: '1px solid #e2e8f0'
          }}>
            <div style={{ fontSize: '2.2rem', marginBottom: '10px' }}>🔄</div>
            <div style={{ fontWeight: '800', color: '#1e293b', fontSize: '1.05rem' }}>تعديل ومناوبات فورية</div>
            <div style={{ fontSize: '0.82rem', color: '#64748b', marginTop: '4px' }}>مرونة كاملة لكل قسم</div>
          </div>
          <div style={{ 
            padding: '22px', 
            background: '#f8fafc', 
            borderRadius: '16px',
            textAlign: 'center',
            border: '1px solid #e2e8f0'
          }}>
            <div style={{ fontSize: '2.2rem', marginBottom: '10px' }}>📊</div>
            <div style={{ fontWeight: '800', color: '#1e293b', fontSize: '1.05rem' }}>تصدير إكسل رسمي</div>
            <div style={{ fontSize: '0.82rem', color: '#64748b', marginTop: '4px' }}>تحميل الجداول بنقرة زر</div>
          </div>
          <div style={{ 
            padding: '22px', 
            background: '#f8fafc', 
            borderRadius: '16px',
            textAlign: 'center',
            border: '1px solid #e2e8f0'
          }}>
            <div style={{ fontSize: '2.2rem', marginBottom: '10px' }}>🎨</div>
            <div style={{ fontWeight: '800', color: '#1e293b', fontSize: '1.05rem' }}>تغطية شفتات متكاملة</div>
            <div style={{ fontSize: '0.82rem', color: '#64748b', marginTop: '4px' }}>صباحي، مسائي، ليلي وطوارئ</div>
          </div>
          <div style={{ 
            padding: '22px', 
            background: '#f8fafc', 
            borderRadius: '16px',
            textAlign: 'center',
            border: '1px solid #e2e8f0'
          }}>
            <div style={{ fontSize: '2.2rem', marginBottom: '10px' }}>🖨️</div>
            <div style={{ fontWeight: '800', color: '#1e293b', fontSize: '1.05rem' }}>طباعة ولوحات إعلانات</div>
            <div style={{ fontSize: '0.82rem', color: '#64748b', marginTop: '4px' }}>تنسيق A4 جاهز للتعليق</div>
          </div>
        </div>
      </div>
    </div>
  );
}
