"use client"

import { useState } from "react"
import { Card } from "@/components/ui/card"
import { Button } from "@/components/ui/button"
import { Input } from "@/components/ui/input"
import { Label } from "@/components/ui/label"
import { Progress } from "@/components/ui/progress"
import { User, CheckCircle2, Lock, ChevronLeft, ChevronRight, Sparkles, Zap } from "lucide-react"
import Link from "next/link"

export default function ProfilePage() {
  const [activeTab, setActiveTab] = useState("profile")
  const [currentBadgeSlide, setCurrentBadgeSlide] = useState(0)

  // Mock user data
  const userData = {
    nickname: "pennytige",
    totalPoints: 1337,
    level: 5,
    questsCompleted: 7,
    totalQuests: 12,
    gameProgress: 58
  }

  const quests = [
    { id: 1, name: "Registration", date: "2024-08-15 14:23", points: 100, completed: true, active: false },
    { id: 2, name: "Memories", date: "", points: 100, completed: false, active: true, badge: "Получить подарочек" },
    { id: 3, name: "Cyber toy", date: "", points: 100, completed: false, active: false },
    { id: 4, name: "Flood", date: "", points: 100, completed: false, active: false },
    { id: 5, name: "Core", date: "", points: 100, completed: false, active: false },
    { id: 6, name: "Access point", date: "", points: 100, completed: false, active: false }
  ]

  const badges = [
    { 
      id: 1, 
      name: "Speed Runner", 
      date: "16.08.2024",
      description: "За скорость",
      requirement: "Решить: 8%",
      icon: "🏃",
      color: "from-cyan-500 to-blue-500"
    },
    { 
      id: 2, 
      name: "Precision Master", 
      date: "17.08.2024",
      description: "За точность",
      requirement: "Решить: 25%",
      icon: "🎯",
      color: "from-purple-500 to-pink-500"
    },
    { 
      id: 3, 
      name: "Secret Master", 
      date: "Заблокировано",
      description: "Заблокировано",
      requirement: "Решить: ???",
      locked: true,
      icon: "🔒",
      color: "from-gray-500 to-gray-700"
    },
    { 
      id: 4, 
      name: "NoName", 
      date: "Заблокировано",
      description: "",
      requirement: "Решить:",
      locked: true,
      icon: "❓",
      color: "from-gray-500 to-gray-700"
    }
  ]

  const visibleBadges = badges.slice(currentBadgeSlide, currentBadgeSlide + 3)

  const nextSlide = () => {
    if (currentBadgeSlide < badges.length - 3) {
      setCurrentBadgeSlide(currentBadgeSlide + 1)
    }
  }

  const prevSlide = () => {
    if (currentBadgeSlide > 0) {
      setCurrentBadgeSlide(currentBadgeSlide - 1)
    }
  }

  return (
    <div className="min-h-screen bg-[#0a0e1a] relative overflow-hidden">
      {/* Animated background effects */}
      <div className="fixed top-20 left-20 w-96 h-96 bg-cyan-500/5 rounded-full blur-[120px] animate-pulse" />
      <div className="fixed bottom-20 right-20 w-96 h-96 bg-pink-500/5 rounded-full blur-[120px] animate-pulse" style={{ animationDelay: '1s' }} />
      <div className="fixed top-1/2 left-1/2 -translate-x-1/2 -translate-y-1/2 w-[600px] h-[600px] bg-blue-500/3 rounded-full blur-[150px] animate-pulse" style={{ animationDelay: '2s' }} />

      {/* Header */}
      <header className="bg-[#0d1117]/80 backdrop-blur-xl border-b border-cyan-500/30 sticky top-0 z-50 shadow-lg shadow-cyan-500/5">
        <nav className="container mx-auto px-6 py-4">
          <div className="flex items-center justify-center">
            <div className="flex items-center gap-12">
              <Link href="/main">
                <button className="text-sm font-bold tracking-[0.2em] transition-all duration-200 font-mono uppercase text-cyan-500/50 hover:text-cyan-400/80 hover:scale-105">
                  Main
                </button>
              </Link>
              <Link href="/main/progress">
                <button className="text-sm font-bold tracking-[0.2em] transition-all duration-200 font-mono uppercase text-cyan-500/50 hover:text-cyan-400/80 hover:scale-105">
                  Progress
                </button>
              </Link>
              <button className="text-sm font-bold tracking-[0.2em] transition-all duration-200 font-mono uppercase text-pink-500 scale-110 drop-shadow-[0_0_8px_rgba(236,72,153,0.5)]">
                Profile
              </button>
              <Link href="/main/rules">
                <button className="text-sm font-bold tracking-[0.2em] transition-all duration-200 font-mono uppercase text-cyan-500/50 hover:text-cyan-400/80 hover:scale-105">
                  Rules
                </button>
              </Link>
              <button className="text-sm font-bold tracking-[0.2em] transition-all duration-200 font-mono uppercase text-cyan-500/50 hover:text-cyan-400/80 hover:scale-105">
                Logoff
              </button>
            </div>
          </div>
        </nav>
      </header>

      {/* Main Content */}
      <main className="container mx-auto px-6 py-8 max-w-7xl relative z-10">
        <div className="grid grid-cols-1 lg:grid-cols-3 gap-6">
          {/* Left Column - Personal Progress & Quests */}
          <div className="lg:col-span-2 space-y-6">
            {/* Personal Progress */}
            <Card className="bg-[#0d1117]/50 backdrop-blur-xl border-2 border-cyan-500/30 p-6 shadow-[0_0_30px_rgba(6,182,212,0.1)] hover:shadow-[0_0_50px_rgba(6,182,212,0.2)] transition-all duration-300 relative overflow-hidden group">
              {/* Animated border glow */}
              <div className="absolute inset-0 bg-gradient-to-r from-transparent via-cyan-500/10 to-transparent -translate-x-full group-hover:translate-x-full transition-transform duration-1000" />
              
              <div className="relative z-10">
                <div className="flex items-center gap-3 mb-6">
                  <Sparkles className="w-5 h-5 text-cyan-400 animate-pulse" />
                  <h3 className="text-xl font-bold text-cyan-400 font-mono">
                    Personal progress
                  </h3>
                </div>
                
                <div className="grid grid-cols-2 md:grid-cols-4 gap-4 mb-6">
                  <div className="bg-gradient-to-br from-[#1a2332] to-[#0d1117] border border-cyan-500/30 rounded-lg p-4 text-center hover:border-cyan-500/50 transition-all hover:scale-105 hover:shadow-[0_0_20px_rgba(6,182,212,0.2)] group/card relative overflow-hidden">
                    <div className="absolute inset-0 bg-gradient-to-br from-cyan-500/5 to-transparent opacity-0 group-hover/card:opacity-100 transition-opacity" />
                    <div className="relative z-10">
                      <div className="text-3xl font-bold text-transparent bg-clip-text bg-gradient-to-r from-cyan-400 to-blue-400 font-mono mb-1">
                        {userData.totalPoints}
                      </div>
                      <div className="text-xs text-cyan-400/60 font-mono">
                        Total Points
                      </div>
                    </div>
                  </div>
                  <div className="bg-gradient-to-br from-[#1a2332] to-[#0d1117] border border-cyan-500/30 rounded-lg p-4 text-center hover:border-cyan-500/50 transition-all hover:scale-105 hover:shadow-[0_0_20px_rgba(6,182,212,0.2)] group/card relative overflow-hidden">
                    <div className="absolute inset-0 bg-gradient-to-br from-cyan-500/5 to-transparent opacity-0 group-hover/card:opacity-100 transition-opacity" />
                    <div className="relative z-10">
                      <div className="text-3xl font-bold text-transparent bg-clip-text bg-gradient-to-r from-cyan-400 to-blue-400 font-mono mb-1">
                        Level {userData.level}
                      </div>
                      <div className="text-xs text-cyan-400/60 font-mono">
                        Current Level
                      </div>
                    </div>
                  </div>
                  <div className="bg-gradient-to-br from-[#1a2332] to-[#0d1117] border border-cyan-500/30 rounded-lg p-4 text-center hover:border-cyan-500/50 transition-all hover:scale-105 hover:shadow-[0_0_20px_rgba(6,182,212,0.2)] group/card relative overflow-hidden">
                    <div className="absolute inset-0 bg-gradient-to-br from-cyan-500/5 to-transparent opacity-0 group-hover/card:opacity-100 transition-opacity" />
                    <div className="relative z-10">
                      <div className="text-3xl font-bold text-transparent bg-clip-text bg-gradient-to-r from-cyan-400 to-blue-400 font-mono mb-1">
                        {userData.questsCompleted}/{userData.totalQuests}
                      </div>
                      <div className="text-xs text-cyan-400/60 font-mono">
                        Quests Completed
                      </div>
                    </div>
                  </div>
                  <div className="bg-gradient-to-br from-[#1a2332] to-[#0d1117] border border-cyan-500/30 rounded-lg p-4 text-center hover:border-cyan-500/50 transition-all hover:scale-105 hover:shadow-[0_0_20px_rgba(6,182,212,0.2)] group/card relative overflow-hidden">
                    <div className="absolute inset-0 bg-gradient-to-br from-cyan-500/5 to-transparent opacity-0 group-hover/card:opacity-100 transition-opacity" />
                    <div className="relative z-10">
                      <div className="text-3xl font-bold text-transparent bg-clip-text bg-gradient-to-r from-cyan-400 to-blue-400 font-mono mb-1">
                        {userData.gameProgress}%
                      </div>
                      <div className="text-xs text-cyan-400/60 font-mono">
                        Game Progress
                      </div>
                    </div>
                  </div>
                </div>

                {/* Quests List */}
                <div className="space-y-2">
                  {quests.map((quest) => (
                    <div
                      key={quest.id}
                      className={`flex items-center justify-between p-4 rounded-lg border transition-all duration-300 group/quest relative overflow-hidden ${
                        quest.active 
                          ? 'bg-gradient-to-r from-yellow-900/30 via-yellow-800/20 to-yellow-900/30 border-yellow-500/50 shadow-[0_0_20px_rgba(234,179,8,0.15)] hover:shadow-[0_0_30px_rgba(234,179,8,0.25)] animate-pulse-slow'
                          : quest.completed
                          ? 'bg-[#0a0e1a]/80 border-green-500/40 hover:border-green-500/60 hover:shadow-[0_0_20px_rgba(34,197,94,0.1)]'
                          : 'bg-[#0a0e1a]/50 border-cyan-500/20 hover:border-cyan-500/40 hover:shadow-[0_0_15px_rgba(6,182,212,0.1)]'
                      }`}
                    >
                      {quest.active && (
                        <div className="absolute inset-0 bg-gradient-to-r from-transparent via-yellow-500/10 to-transparent -translate-x-full group-hover/quest:translate-x-full transition-transform duration-1000" />
                      )}
                      
                      <div className="flex items-center gap-4 flex-1 relative z-10">
                        {quest.completed ? (
                          <div className="relative">
                            <CheckCircle2 className="w-5 h-5 text-green-400 flex-shrink-0 drop-shadow-[0_0_8px_rgba(34,197,94,0.5)]" />
                            <div className="absolute inset-0 animate-ping opacity-20">
                              <CheckCircle2 className="w-5 h-5 text-green-400" />
                            </div>
                          </div>
                        ) : (
                          <Lock className={`w-5 h-5 flex-shrink-0 ${quest.active ? 'text-yellow-400 animate-pulse' : 'text-cyan-400/50'}`} />
                        )}
                        <div className="flex-1">
                          <div className="flex items-center gap-3">
                            <span className={`font-bold font-mono text-sm ${
                              quest.active ? 'text-yellow-300' : 'text-cyan-400'
                            }`}>
                              {quest.name}
                            </span>
                            {quest.badge && (
                              <span className="px-3 py-1 bg-gradient-to-r from-pink-500 to-pink-600 text-black text-xs font-mono font-bold rounded shadow-[0_0_15px_rgba(236,72,153,0.4)] animate-pulse">
                                <Zap className="inline w-3 h-3 mr-1" />
                                {quest.badge}
                              </span>
                            )}
                          </div>
                          {quest.date && (
                            <div className="text-xs text-cyan-400/50 font-mono mt-1">
                              {quest.date}
                            </div>
                          )}
                        </div>
                      </div>
                      <div className={`font-mono font-bold text-sm ${
                        quest.completed ? 'text-green-400' : 'text-cyan-400'
                      } drop-shadow-[0_0_8px_rgba(6,182,212,0.5)]`}>
                        {quest.points} pts
                      </div>
                    </div>
                  ))}
                </div>
              </div>
            </Card>

            {/* Badges Carousel */}
            <Card className="bg-[#0d1117]/50 backdrop-blur-xl border-2 border-cyan-500/30 p-6 shadow-[0_0_30px_rgba(6,182,212,0.1)] hover:shadow-[0_0_50px_rgba(6,182,212,0.2)] transition-all duration-300 relative overflow-hidden group">
              <div className="absolute inset-0 bg-gradient-to-r from-transparent via-cyan-500/10 to-transparent -translate-x-full group-hover:translate-x-full transition-transform duration-1000" />
              
              <div className="relative z-10">
                <div className="flex items-center justify-between mb-6">
                  <button
                    onClick={prevSlide}
                    disabled={currentBadgeSlide === 0}
                    className="w-12 h-12 rounded-full bg-gradient-to-br from-cyan-500/20 to-cyan-600/10 border border-cyan-500/40 flex items-center justify-center hover:from-cyan-500/30 hover:to-cyan-600/20 hover:border-cyan-400/60 transition-all disabled:opacity-20 disabled:cursor-not-allowed hover:scale-110 hover:shadow-[0_0_20px_rgba(6,182,212,0.3)] active:scale-95"
                  >
                    <ChevronLeft className="w-6 h-6 text-cyan-400" />
                  </button>
                  
                  <div className="flex-1 px-4">
                    <div className="grid grid-cols-3 gap-4">
                      {visibleBadges.map((badge) => (
                        <Card
                          key={badge.id}
                          className={`p-6 text-center transition-all duration-300 relative overflow-hidden group/badge ${
                            badge.locked
                              ? 'bg-[#0a0e1a]/30 border-cyan-500/10 opacity-40'
                              : 'bg-gradient-to-br from-[#1a2332] to-[#0d1117] border-cyan-500/40 hover:border-cyan-500/70 hover:scale-105 hover:shadow-[0_0_30px_rgba(6,182,212,0.2)]'
                          }`}
                        >
                          {!badge.locked && (
                            <div className="absolute inset-0 bg-gradient-to-br from-cyan-500/5 to-transparent opacity-0 group-hover/badge:opacity-100 transition-opacity" />
                          )}
                          
                          <div className="relative z-10">
                            <div className={`w-20 h-20 rounded-xl mx-auto mb-3 flex items-center justify-center text-4xl relative ${
                              badge.locked ? 'bg-cyan-500/5' : `bg-gradient-to-br ${badge.color}/20 shadow-inner`
                            }`}>
                              {!badge.locked && (
                                <div className={`absolute inset-0 rounded-xl bg-gradient-to-br ${badge.color} opacity-0 group-hover/badge:opacity-20 blur transition-opacity`} />
                              )}
                              <span className="relative z-10 drop-shadow-lg">{badge.icon}</span>
                            </div>
                            <h4 className={`font-bold font-mono text-sm mb-1 ${
                              badge.locked ? 'text-cyan-400/30' : 'text-cyan-400'
                            }`}>
                              {badge.name}
                            </h4>
                            <p className={`text-xs font-mono mb-2 ${
                              badge.locked ? 'text-cyan-400/20' : 'text-cyan-400/60'
                            }`}>
                              {badge.date}
                            </p>
                            {badge.description && (
                              <p className="text-xs text-cyan-400/60 font-mono mb-1">
                                {badge.description}
                              </p>
                            )}
                            <p className="text-xs text-cyan-400/50 font-mono">
                              {badge.requirement}
                            </p>
                          </div>
                        </Card>
                      ))}
                    </div>
                  </div>

                  <button
                    onClick={nextSlide}
                    disabled={currentBadgeSlide >= badges.length - 3}
                    className="w-12 h-12 rounded-full bg-gradient-to-br from-cyan-500/20 to-cyan-600/10 border border-cyan-500/40 flex items-center justify-center hover:from-cyan-500/30 hover:to-cyan-600/20 hover:border-cyan-400/60 transition-all disabled:opacity-20 disabled:cursor-not-allowed hover:scale-110 hover:shadow-[0_0_20px_rgba(6,182,212,0.3)] active:scale-95"
                  >
                    <ChevronRight className="w-6 h-6 text-cyan-400" />
                  </button>
                </div>

                {/* Dots Indicator */}
                <div className="flex justify-center gap-2">
                  {Array.from({ length: badges.length - 2 }).map((_, index) => (
                    <button
                      key={index}
                      onClick={() => setCurrentBadgeSlide(index)}
                      className={`h-2 rounded-full transition-all ${
                        currentBadgeSlide === index
                          ? 'bg-cyan-400 w-8 shadow-[0_0_10px_rgba(6,182,212,0.5)]'
                          : 'bg-cyan-500/30 w-2 hover:bg-cyan-500/50'
                      }`}
                    />
                  ))}
                </div>
              </div>
            </Card>
          </div>

          {/* Right Column - Profile Settings */}
          <Card className="bg-[#0d1117]/50 backdrop-blur-xl border-2 border-cyan-500/30 p-6 shadow-[0_0_30px_rgba(6,182,212,0.1)] hover:shadow-[0_0_50px_rgba(6,182,212,0.2)] transition-all duration-300 relative overflow-hidden group">
            <div className="absolute inset-0 bg-gradient-to-br from-cyan-500/5 via-transparent to-pink-500/5 opacity-0 group-hover:opacity-100 transition-opacity duration-500" />
            
            <div className="relative z-10">
              <div className="text-center mb-6">
                <div className="w-24 h-24 bg-gradient-to-br from-cyan-500/30 to-blue-500/30 rounded-full flex items-center justify-center mx-auto mb-4 border-4 border-cyan-500/40 shadow-[0_0_30px_rgba(6,182,212,0.2)] hover:scale-110 hover:shadow-[0_0_50px_rgba(6,182,212,0.4)] transition-all duration-300 group/avatar relative overflow-hidden">
                  <div className="absolute inset-0 bg-gradient-to-br from-cyan-400/20 to-blue-400/20 animate-pulse" />
                  <User className="w-12 h-12 text-cyan-400 relative z-10 drop-shadow-[0_0_10px_rgba(6,182,212,0.5)]" />
                </div>
                <h3 className="text-xl font-bold text-transparent bg-clip-text bg-gradient-to-r from-cyan-400 to-blue-400 font-mono">
                  Profile settings
                </h3>
              </div>

              {/* School Nickname */}
              <div className="space-y-2 mb-6">
                <Label className="text-cyan-400 font-mono text-sm flex items-center gap-2">
                  <Sparkles className="w-3 h-3" />
                  School nickname:
                </Label>
                <Input
                  value={userData.nickname}
                  readOnly
                  className="bg-[#0a0e1a]/80 backdrop-blur border-cyan-500/40 text-cyan-400 font-mono hover:border-cyan-500/60 transition-all focus:shadow-[0_0_20px_rgba(6,182,212,0.2)]"
                />
              </div>

              {/* Change Password */}
              <div className="space-y-4 mb-6">
                <Label className="text-cyan-400 font-mono text-sm flex items-center gap-2">
                  <Lock className="w-3 h-3" />
                  Change password
                </Label>
                <Input
                  type="password"
                  placeholder="Current pswd"
                  className="bg-[#0a0e1a]/80 backdrop-blur border-cyan-500/40 text-cyan-400 placeholder:text-cyan-400/30 font-mono hover:border-cyan-500/60 transition-all focus:border-cyan-400 focus:shadow-[0_0_20px_rgba(6,182,212,0.2)]"
                />
                <Input
                  type="password"
                  placeholder="New pswd"
                  className="bg-[#0a0e1a]/80 backdrop-blur border-cyan-500/40 text-cyan-400 placeholder:text-cyan-400/30 font-mono hover:border-cyan-500/60 transition-all focus:border-cyan-400 focus:shadow-[0_0_20px_rgba(6,182,212,0.2)]"
                />
                <Input
                  type="password"
                  placeholder="Confirm new pswd"
                  className="bg-[#0a0e1a]/80 backdrop-blur border-cyan-500/40 text-cyan-400 placeholder:text-cyan-400/30 font-mono hover:border-cyan-500/60 transition-all focus:border-cyan-400 focus:shadow-[0_0_20px_rgba(6,182,212,0.2)]"
                />
                <Button className="w-full bg-gradient-to-r from-cyan-500 to-cyan-600 hover:from-cyan-400 hover:to-cyan-500 text-black font-mono font-bold shadow-[0_0_20px_rgba(6,182,212,0.3)] hover:shadow-[0_0_40px_rgba(6,182,212,0.5)] transition-all hover:scale-105 active:scale-95">
                  Apply
                </Button>
              </div>

              {/* Change Avatar */}
              <div className="space-y-3">
                <Label className="text-cyan-400 font-mono text-sm flex items-center gap-2">
                  <User className="w-3 h-3" />
                  Change avatar
                </Label>
                <div className="grid grid-cols-2 gap-3">
                  <Button 
                    variant="outline"
                    className="bg-gradient-to-r from-cyan-500/90 to-cyan-600/90 hover:from-cyan-400 hover:to-cyan-500 border-0 text-black font-mono font-bold shadow-[0_0_15px_rgba(6,182,212,0.2)] hover:shadow-[0_0_30px_rgba(6,182,212,0.4)] transition-all hover:scale-105 active:scale-95"
                  >
                    Choose File
                  </Button>
                  <Button 
                    variant="outline"
                    className="bg-gradient-to-r from-cyan-500/90 to-cyan-600/90 hover:from-cyan-400 hover:to-cyan-500 border-0 text-black font-mono font-bold shadow-[0_0_15px_rgba(6,182,212,0.2)] hover:shadow-[0_0_30px_rgba(6,182,212,0.4)] transition-all hover:scale-105 active:scale-95"
                  >
                    Upload Avatar
                  </Button>
                </div>
              </div>
            </div>
          </Card>
        </div>
      </main>

      {/* Enhanced Background grid */}
      <div 
        className="fixed inset-0 pointer-events-none -z-10 opacity-50"
        style={{
          backgroundImage: `
            linear-gradient(rgba(6, 182, 212, 0.05) 1px, transparent 1px),
            linear-gradient(90deg, rgba(6, 182, 212, 0.05) 1px, transparent 1px)
          `,
          backgroundSize: '50px 50px'
        }}
      />

      <style jsx>{`
        @keyframes pulse-slow {
          0%, 100% { opacity: 1; }
          50% { opacity: 0.8; }
        }
        .animate-pulse-slow {
          animation: pulse-slow 3s ease-in-out infinite;
        }
      `}</style>
    </div>
  )
}