'use client'

import { Icons, formatTime, formatDate, type Meeting, type MeetingStats } from './types'

interface DashboardTabProps {
  isDark: boolean
  loading: boolean
  stats: MeetingStats
  activeMeetings: Meeting[]
  scheduledMeetings: Meeting[]
  errorMessage: string | null
  creating: boolean
  setErrorMessage: (msg: string | null) => void
  setShowNewMeetingModal: (show: boolean) => void
  setMeetingType: (type: string) => void
  startQuickMeeting: () => void
  joinMeeting: (roomName: string, title: string) => void
  copyMeetingLink: (roomName: string) => void
}

export function DashboardTab({
  isDark, loading, stats, activeMeetings, scheduledMeetings,
  errorMessage, creating,
  setErrorMessage, setShowNewMeetingModal, setMeetingType,
  startQuickMeeting, joinMeeting, copyMeetingLink,
}: DashboardTabProps) {
  const statsData = [
    { label: 'Aktive Meetings', value: loading ? '-' : String(stats.active), icon: Icons.video, color: 'from-green-400 to-emerald-600' },
    { label: 'Geplante Termine', value: loading ? '-' : String(stats.scheduled), icon: Icons.calendar, color: 'from-blue-400 to-blue-600' },
    { label: 'Aufzeichnungen', value: loading ? '-' : String(stats.recordings), icon: Icons.record, color: 'from-red-400 to-rose-600' },
    { label: 'Teilnehmer', value: loading ? '-' : String(stats.participants), icon: Icons.users, color: 'from-amber-400 to-orange-600' },
  ]

  return (
    <>
      {/* Error Banner */}
      {errorMessage && (
        <div className={`mb-6 p-4 rounded-xl flex items-center justify-between ${
          isDark ? 'bg-red-500/20 border border-red-500/30' : 'bg-red-50 border border-red-200'
        }`}>
          <div className="flex items-center gap-3">
            <svg className={`w-5 h-5 ${isDark ? 'text-red-400' : 'text-red-500'}`} fill="none" stroke="currentColor" viewBox="0 0 24 24">
              <path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M12 8v4m0 4h.01M21 12a9 9 0 11-18 0 9 9 0 0118 0z" />
            </svg>
            <span className={isDark ? 'text-red-200' : 'text-red-700'}>{errorMessage}</span>
          </div>
          <button
            onClick={() => setErrorMessage(null)}
            className={`p-1 rounded-lg transition-colors ${isDark ? 'hover:bg-white/10' : 'hover:bg-red-100'}`}
          >
            <svg className={`w-4 h-4 ${isDark ? 'text-red-400' : 'text-red-500'}`} fill="none" stroke="currentColor" viewBox="0 0 24 24">
              <path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M6 18L18 6M6 6l12 12" />
            </svg>
          </button>
        </div>
      )}

      {/* Quick Actions */}
      <div className="grid grid-cols-1 sm:grid-cols-2 lg:grid-cols-4 gap-6 mb-8">
        <QuickActionCard isDark={isDark} icon={Icons.video} color="from-green-400 to-emerald-600"
          title="Sofort-Meeting" subtitle="Jetzt starten" onClick={startQuickMeeting} disabled={creating} />
        <QuickActionCard isDark={isDark} icon={Icons.calendar} color="from-blue-400 to-blue-600"
          title="Meeting planen" subtitle="Termin festlegen"
          onClick={() => { setMeetingType('scheduled'); setShowNewMeetingModal(true) }} />
        <QuickActionCard isDark={isDark} icon={Icons.graduation} color="from-purple-400 to-purple-600"
          title="Schulung erstellen" subtitle="Training planen"
          onClick={() => { setMeetingType('training'); setShowNewMeetingModal(true) }} />
        <QuickActionCard isDark={isDark} icon={Icons.users} color="from-amber-400 to-orange-600"
          title="Elterngespraech" subtitle="Termin vereinbaren"
          onClick={() => { setMeetingType('parent'); setShowNewMeetingModal(true) }} />
      </div>

      {/* Statistics */}
      <div className="grid grid-cols-1 sm:grid-cols-2 lg:grid-cols-4 gap-6 mb-8">
        {statsData.map((stat, index) => (
          <div
            key={index}
            className={`backdrop-blur-xl border rounded-3xl p-6 transition-all hover:scale-105 hover:shadow-xl cursor-pointer ${
              isDark
                ? 'bg-white/10 border-white/20 hover:bg-white/15'
                : 'bg-white/70 border-black/10 hover:bg-white/90 shadow-lg'
            }`}
          >
            <div className="flex items-center justify-between mb-4">
              <div className={`w-12 h-12 bg-gradient-to-br ${stat.color} rounded-2xl flex items-center justify-center text-white shadow-lg`}>
                {stat.icon}
              </div>
              <svg className={`w-5 h-5 ${isDark ? 'text-white/40' : 'text-slate-400'}`} fill="none" stroke="currentColor" viewBox="0 0 24 24">
                <path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M9 5l7 7-7 7" />
              </svg>
            </div>
            <p className={`text-sm mb-1 ${isDark ? 'text-white/60' : 'text-slate-600'}`}>{stat.label}</p>
            <p className={`text-3xl font-bold ${isDark ? 'text-white' : 'text-slate-900'}`}>{stat.value}</p>
          </div>
        ))}
      </div>

      {/* Active Meetings */}
      {activeMeetings.length > 0 && (
        <ActiveMeetingsList isDark={isDark} meetings={activeMeetings}
          joinMeeting={joinMeeting} copyMeetingLink={copyMeetingLink} />
      )}

      {/* Scheduled Meetings */}
      <ScheduledMeetingsList isDark={isDark} loading={loading} meetings={scheduledMeetings}
        joinMeeting={joinMeeting} copyMeetingLink={copyMeetingLink}
        setShowNewMeetingModal={setShowNewMeetingModal} />
    </>
  )
}

// ============================================
// SUB-COMPONENTS
// ============================================

function QuickActionCard({ isDark, icon, color, title, subtitle, onClick, disabled }: {
  isDark: boolean; icon: React.ReactNode; color: string
  title: string; subtitle: string; onClick: () => void; disabled?: boolean
}) {
  return (
    <button
      onClick={onClick}
      disabled={disabled}
      className={`backdrop-blur-xl border rounded-3xl p-6 text-left transition-all hover:scale-105 hover:shadow-xl ${
        isDark
          ? 'bg-white/10 border-white/20 hover:bg-white/15'
          : 'bg-white/70 border-black/10 hover:bg-white/90 shadow-lg'
      }`}
    >
      <div className={`w-14 h-14 bg-gradient-to-br ${color} rounded-2xl flex items-center justify-center text-white shadow-lg mb-4`}>
        {icon}
      </div>
      <div className={`font-semibold mb-1 ${isDark ? 'text-white' : 'text-slate-900'}`}>{title}</div>
      <div className={`text-sm ${isDark ? 'text-white/60' : 'text-slate-500'}`}>{subtitle}</div>
    </button>
  )
}

function ActiveMeetingsList({ isDark, meetings, joinMeeting, copyMeetingLink }: {
  isDark: boolean; meetings: Meeting[]
  joinMeeting: (roomName: string, title: string) => void
  copyMeetingLink: (roomName: string) => void
}) {
  return (
    <div className={`backdrop-blur-xl border rounded-3xl mb-8 overflow-hidden ${
      isDark ? 'bg-white/10 border-white/20' : 'bg-white/70 border-black/10 shadow-lg'
    }`}>
      <div className={`p-6 border-b flex items-center justify-between ${isDark ? 'border-white/10' : 'border-slate-200'}`}>
        <h2 className={`text-xl font-semibold ${isDark ? 'text-white' : 'text-slate-900'}`}>Aktive Meetings</h2>
        <span className="px-3 py-1 bg-green-500/20 text-green-400 text-sm font-medium rounded-full">
          {meetings.length} Live
        </span>
      </div>
      <div className="divide-y divide-white/10">
        {meetings.map((meeting) => (
          <div key={meeting.room_name} className={`p-6 flex items-center gap-4 transition-colors ${isDark ? 'hover:bg-white/5' : 'hover:bg-slate-50'}`}>
            <div className="w-14 h-14 bg-gradient-to-br from-green-400 to-emerald-600 rounded-2xl flex items-center justify-center text-white animate-pulse">
              {Icons.video}
            </div>
            <div className="flex-1">
              <div className={`font-medium ${isDark ? 'text-white' : 'text-slate-900'}`}>{meeting.title}</div>
              <div className={`flex items-center gap-4 text-sm mt-1 ${isDark ? 'text-white/50' : 'text-slate-500'}`}>
                <span className="flex items-center gap-1">{Icons.users} {meeting.participants || 0} Teilnehmer</span>
                {meeting.started_at && (
                  <span className="flex items-center gap-1">{Icons.clock} Gestartet {formatTime(meeting.started_at)}</span>
                )}
              </div>
            </div>
            <div className="flex items-center gap-2">
              <button onClick={() => joinMeeting(meeting.room_name, meeting.title)}
                className="px-5 py-2.5 bg-gradient-to-r from-green-500 to-emerald-500 text-white rounded-xl font-medium hover:shadow-lg hover:shadow-green-500/30 transition-all hover:scale-105">
                Beitreten
              </button>
              <button onClick={() => copyMeetingLink(meeting.room_name)}
                className={`p-2.5 rounded-xl transition-all ${isDark ? 'text-white/40 hover:text-white hover:bg-white/10' : 'text-slate-400 hover:text-slate-600 hover:bg-slate-100'}`}
                title="Link kopieren">
                {Icons.copy}
              </button>
            </div>
          </div>
        ))}
      </div>
    </div>
  )
}

function ScheduledMeetingsList({ isDark, loading, meetings, joinMeeting, copyMeetingLink, setShowNewMeetingModal }: {
  isDark: boolean; loading: boolean; meetings: Meeting[]
  joinMeeting: (roomName: string, title: string) => void
  copyMeetingLink: (roomName: string) => void
  setShowNewMeetingModal: (show: boolean) => void
}) {
  return (
    <div className={`backdrop-blur-xl border rounded-3xl overflow-hidden ${
      isDark ? 'bg-white/10 border-white/20' : 'bg-white/70 border-black/10 shadow-lg'
    }`}>
      <div className={`p-6 border-b flex items-center justify-between ${isDark ? 'border-white/10' : 'border-slate-200'}`}>
        <h2 className={`text-xl font-semibold ${isDark ? 'text-white' : 'text-slate-900'}`}>Naechste Meetings</h2>
        <button className={`text-sm transition-colors ${isDark ? 'text-white/60 hover:text-white' : 'text-slate-500 hover:text-slate-900'}`}>
          Alle anzeigen →
        </button>
      </div>
      {loading ? (
        <div className={`p-12 text-center ${isDark ? 'text-white/40' : 'text-slate-400'}`}>Laet...</div>
      ) : meetings.length === 0 ? (
        <div className="p-12 text-center">
          <div className={`w-20 h-20 rounded-full flex items-center justify-center mx-auto mb-4 ${isDark ? 'bg-white/10' : 'bg-slate-100'}`}>
            <span className="text-4xl">{Icons.calendar}</span>
          </div>
          <p className={`mb-4 ${isDark ? 'text-white/50' : 'text-slate-500'}`}>Keine geplanten Meetings</p>
          <button onClick={() => setShowNewMeetingModal(true)} className="text-blue-500 hover:text-blue-400 font-medium">
            Meeting planen
          </button>
        </div>
      ) : (
        <div className={`divide-y ${isDark ? 'divide-white/10' : 'divide-slate-100'}`}>
          {meetings.slice(0, 5).map((meeting) => (
            <div key={meeting.room_name} className={`p-6 flex items-center gap-4 transition-colors ${isDark ? 'hover:bg-white/5' : 'hover:bg-slate-50'}`}>
              <div className={`text-center min-w-[70px] px-3 py-2 rounded-2xl ${isDark ? 'bg-white/10' : 'bg-slate-100'}`}>
                <div className={`text-lg font-bold ${isDark ? 'text-white' : 'text-slate-900'}`}>
                  {meeting.scheduled_at ? formatTime(meeting.scheduled_at) : '--:--'}
                </div>
                <div className={`text-xs ${isDark ? 'text-white/50' : 'text-slate-500'}`}>
                  {meeting.scheduled_at ? formatDate(meeting.scheduled_at) : ''}
                </div>
              </div>
              <div className="flex-1">
                <div className={`font-medium ${isDark ? 'text-white' : 'text-slate-900'}`}>{meeting.title}</div>
                <div className={`flex items-center gap-4 text-sm mt-1 ${isDark ? 'text-white/50' : 'text-slate-500'}`}>
                  <span className="flex items-center gap-1">{Icons.clock} {meeting.duration} Min</span>
                  <span className="capitalize">{meeting.type}</span>
                </div>
              </div>
              <span className={`px-3 py-1 text-xs font-medium rounded-full ${isDark ? 'bg-blue-500/20 text-blue-300' : 'bg-blue-100 text-blue-700'}`}>
                Geplant
              </span>
              <div className="flex items-center gap-2">
                <button onClick={() => joinMeeting(meeting.room_name, meeting.title)}
                  className="px-5 py-2.5 bg-gradient-to-r from-blue-500 to-purple-500 text-white rounded-xl font-medium hover:shadow-lg hover:shadow-purple-500/30 transition-all hover:scale-105">
                  Beitreten
                </button>
                <button onClick={() => copyMeetingLink(meeting.room_name)}
                  className={`p-2.5 rounded-xl transition-all ${isDark ? 'text-white/40 hover:text-white hover:bg-white/10' : 'text-slate-400 hover:text-slate-600 hover:bg-slate-100'}`}
                  title="Link kopieren">
                  {Icons.copy}
                </button>
              </div>
            </div>
          ))}
        </div>
      )}
    </div>
  )
}
