import {
  useMetricsByPortal,
  useMetricsBySystems,
} from '@/app/hooks/useMetrics';
import logoAImg from '@/assets/images/a-agape.png';
import logoImg from '@/assets/images/agape-logo.png';
import { ChartLineUpIcon, SpinnerIcon } from '@phosphor-icons/react';
import { motion } from 'framer-motion';
import { useSearchParams } from 'react-router-dom';
import ProfileCard from './ProfileCard';
import { StorySlide } from './story-slide';

export function RetrospectiveSlides() {
  const [searchParams] = useSearchParams();
  const cpf = searchParams.get('cpf') || '';
  const ano = searchParams.get('ano')
    ? parseInt(searchParams.get('ano')!)
    : new Date().getFullYear();

  const {
    data: metricsPortal,
    isLoading: isLoadingPortal,
    error: errorPortal,
  } = useMetricsByPortal(cpf, ano);

  const {
    data: metricsSystems,
    isLoading: isLoadingSystems,
    error: errorSystems,
  } = useMetricsBySystems(cpf, ano);

  const slides: number[] = [];
  slides.push(0);
  if (metricsPortal && metricsPortal.length > 0) slides.push(1);
  if (
    (metricsPortal && metricsPortal.length > 0) ||
    (metricsSystems && metricsSystems.length > 0)
  ) {
    slides.push(2);
  }
  slides.push(3);

  const isLoading = isLoadingPortal || isLoadingSystems;
  const hasError = errorPortal || errorSystems;

  const totalAcessos = [
    ...(metricsPortal || []),
    ...(metricsSystems || []),
  ].reduce((acc, item) => acc + item.totalAcessos, 0);

  const topItems = [...(metricsPortal || []), ...(metricsSystems || [])]
    .filter((item) => (item.nomeSistema || '').toLowerCase() !== 'agportal')
    .sort((a, b) => b.totalAcessos - a.totalAcessos)
    .slice(0, 5);

  const allItems = [...(metricsPortal || []), ...(metricsSystems || [])];
  const aplicacoesCount = new Set(
    allItems.map((i) => (i.nomeSistema || '').trim()).filter((n) => !!n),
  ).size;
  const topAppName = topItems[0]?.nomeSistema ?? null;
  const topAppAcessos = topItems[0]?.totalAcessos ?? null;

  return (
    <div className="w-full h-screen overflow-hidden">
      {isLoading ? (
        <div className="h-screen w-full bg-black flex items-center justify-center">
          <div className="text-center space-y-4">
            <SpinnerIcon className="h-12 w-12 animate-spin mx-auto text-white" />
            <p className="text-white text-lg">
              Carregando sua retrospectiva...
            </p>
          </div>
        </div>
      ) : hasError ? (
        <div className="h-screen w-full bg-black flex items-center justify-center">
          <div className="text-center space-y-4 max-w-md">
            <p className="text-white text-lg font-semibold">
              Erro ao carregar dados
            </p>
            <p className="text-white/70">
              Verifique seu CPF e ano, e tente novamente.
            </p>
          </div>
        </div>
      ) : (
        <>
          <div className="h-full snap-y snap-mandatory overflow-y-scroll scroll-smooth">
            <StorySlide className="bg-linear-to-br from-[#0e233d] via-[#173b63] to-[#145190] text-white">
              <div className="max-w-7xl mx-auto w-full px-6">
                <div className="text-center space-y-8">
                  <motion.div
                    initial={{ scale: 0.8, opacity: 0 }}
                    whileInView={{ scale: 1, opacity: 1 }}
                    viewport={{ once: true }}
                    transition={{ duration: 0.6 }}>
                    <img
                      src={logoImg}
                      alt="Ágape Logo"
                      className="mx-auto w-52 mb-6"
                    />
                  </motion.div>

                  <motion.h1
                    className="text-6xl md:text-7xl font-medium mb-4"
                    initial={{ y: 20, opacity: 0 }}
                    whileInView={{ y: 0, opacity: 1 }}
                    viewport={{ once: true }}
                    transition={{ duration: 0.6, delay: 0.2 }}>
                    Sua Retrospectiva
                    <br />
                    Ágape {ano}
                  </motion.h1>

                  <motion.p
                    className="text-xl md:text-2xl text-white/90 max-w-2xl mx-auto font-light"
                    initial={{ y: 20, opacity: 0 }}
                    whileInView={{ y: 0, opacity: 1 }}
                    viewport={{ once: true }}
                    transition={{ duration: 0.6, delay: 0.4 }}>
                    Um ano de conquistas, eficiência e transparência na gestão
                    pública
                  </motion.p>

                  <motion.div
                    className="flex items-center justify-center gap-4 text-sm text-white/70 pt-8"
                    initial={{ opacity: 0 }}
                    whileInView={{ opacity: 1 }}
                    viewport={{ once: true }}
                    transition={{ duration: 0.6, delay: 0.8 }}>
                    <span>Transparência</span>
                    <span>•</span>
                    <span>Eficiência</span>
                    <span>•</span>
                    <span>Parceria</span>
                  </motion.div>
                </div>
              </div>
            </StorySlide>

            {metricsPortal && metricsPortal.length > 0 && (
              <>
                <StorySlide className="bg-white text-gray-900 overflow-hidden relative">
                  <div className="absolute inset-0 overflow-hidden">
                    <div className="absolute -top-24 -right-24 w-96 h-96 bg-gradient-to-br from-blue-50 to-indigo-50 rounded-full opacity-70"></div>
                    <div className="absolute -bottom-32 -left-32 w-80 h-80 bg-gradient-to-tr from-blue-50 to-cyan-50 rounded-full opacity-60"></div>
                  </div>

                  <div className="max-w-7xl mx-auto w-full h-full px-6 py-8 md:py-16 relative z-10">
                    <div className="flex flex-col lg:flex-row items-center justify-between h-full">
                      <motion.div
                        className="lg:w-1/2 flex flex-col items-start mb-10 lg:mb-0 space-y-8 lg:pr-12"
                        initial={{ opacity: 0, x: -30 }}
                        whileInView={{ opacity: 1, x: 0 }}
                        viewport={{ once: true }}
                        transition={{ duration: 0.6 }}>
                        <motion.div
                          initial={{ scale: 0 }}
                          whileInView={{ scale: 1 }}
                          viewport={{ once: true }}
                          transition={{ type: 'spring', duration: 0.6 }}>
                          <img src={logoAImg} className="w-10" />
                        </motion.div>

                        <motion.h2
                          className="text-3xl md:text-4xl lg:text-5xl font-bold text-gray-900 mb-4"
                          initial={{ opacity: 0, y: -20 }}
                          whileInView={{ opacity: 1, y: 0 }}
                          viewport={{ once: true }}
                          transition={{ duration: 0.6, delay: 0.1 }}>
                          <span className="text-[#0e233d mt-2">
                            AgPortal <br />
                            Sua Porta de Entrada Digital
                          </span>
                        </motion.h2>

                        <motion.p
                          className="text-lg md:text-xl text-gray-600 mb-8 max-w-2xl"
                          initial={{ opacity: 0 }}
                          whileInView={{ opacity: 1 }}
                          viewport={{ once: true }}
                          transition={{ duration: 0.6, delay: 0.2 }}>
                          O AgPortal é o hub central que conecta todos os
                          colaboradores às ferramentas, sistemas e informações
                          necessárias para o dia a dia de trabalho.
                        </motion.p>
                      </motion.div>

                      <div className="lg:w-1/2 h-full flex flex-col items-center justify-center">
                        <motion.div
                          className="space-y-4 mb-8"
                          initial={{ opacity: 0, scale: 0.95 }}
                          whileInView={{ opacity: 1, scale: 1 }}
                          viewport={{ once: true }}
                          transition={{ duration: 0.8, delay: 0.3 }}>
                          <div className="flex flex-col items-center justify-center gap-3">
                            <div className="text-9xl font-black text-gray-900 tracking-tight">
                              {metricsPortal
                                .reduce((acc, p) => acc + p.totalAcessos, 0)
                                .toLocaleString('pt-BR')}
                            </div>

                            <div className="flex flex-col">
                              <span className="text-lg md:text-xl text-muted-foreground">
                                Acessos em {ano}
                              </span>
                            </div>
                          </div>
                        </motion.div>
                      </div>
                    </div>
                  </div>
                </StorySlide>

                <StorySlide className="bg-linear-to-br from-[#0e233d] via-[#173b63] to-[#145190] text-white overflow-hidden">
                  <div className="max-w-7xl mx-auto w-full h-full px-6 py-8 md:py-12">
                    <div className="flex flex-col md:flex-row items-start md:items-center justify-between h-full">
                      <div className="space-y-8 w-full md:w-1/2 text-center md:text-left">
                        <motion.div
                          className="space-y-8"
                          initial={{ scale: 0.9, opacity: 0 }}
                          whileInView={{ scale: 1, opacity: 1 }}
                          viewport={{ once: true }}
                          transition={{ duration: 0.6 }}>
                          <div className="inline-flex items-center gap-2 px-4 py-2 rounded-full bg-white/10 backdrop-blur-sm border border-white/20 mb-4">
                            <ChartLineUpIcon className="h-4 w-4 text-yellow-300" />
                            <span className="text-sm font-semibold text-white">
                              Top 5 Aplicativos
                            </span>
                          </div>

                          <h2 className="text-4xl md:text-5xl font-bold text-white/80">
                            Aplicativos mais acessados
                          </h2>

                          <p className="text-xl text-white/80 max-w-xl">
                            Aqui estão as 5 aplicações com mais acessos e o
                            total de horas registradas nelas durante o ano.
                          </p>
                        </motion.div>
                      </div>

                      <div className="md:w-3/5 w-full mt-8 md:mt-0">
                        <div className="w-full grid grid-cols-1 gap-3 md:gap-4">
                          {topItems.slice(0, 5).map((item, index) => {
                            const medalColors = [
                              'bg-yellow-400 text-yellow-900',
                              'bg-gray-300 text-gray-900',
                              'bg-orange-400 text-orange-900',
                              'bg-blue-400/20 text-blue-300',
                              'bg-purple-400/20 text-purple-300',
                            ];
                            const isMedal = index < 3;
                            const medalColor =
                              medalColors[index] || 'bg-white/20';

                            return (
                              <motion.div
                                key={index}
                                className={`flex items-center p-4 md:p-5 rounded-xl backdrop-blur-sm border border-white/20 ${
                                  isMedal
                                    ? 'bg-white/15'
                                    : 'bg-white/10 hover:bg-white/15'
                                } transition-all`}
                                initial={{ x: 50, opacity: 0 }}
                                whileInView={{ x: 0, opacity: 1 }}
                                viewport={{ once: true }}
                                transition={{
                                  duration: 0.5,
                                  delay: index * 0.1,
                                }}>
                                <div
                                  className={`w-10 h-10 md:w-12 md:h-12 rounded-full flex items-center justify-center font-bold text-base md:text-lg mr-4 ${medalColor}`}>
                                  {index === 0 && '🥇'}
                                  {index === 1 && '🥈'}
                                  {index === 2 && '🥉'}
                                  {index >= 3 && `#${index + 1}`}
                                </div>

                                <div className="flex-1 min-w-0">
                                  <p className="text-base md:text-lg font-semibold truncate">
                                    {item.nomeSistema}
                                  </p>
                                  <p className="text-sm text-white/70">
                                    {item.totalAcessos.toLocaleString('pt-BR')}{' '}
                                    acessos •{' '}
                                    {Math.round(
                                      item.horasLogadas || 0,
                                    ).toLocaleString('pt-BR')}{' '}
                                    h
                                  </p>
                                </div>

                                <motion.div
                                  className="ml-4 flex flex-col items-end"
                                  initial={{ scale: 0 }}
                                  whileInView={{ scale: 1 }}
                                  viewport={{ once: true }}
                                  transition={{
                                    type: 'spring',
                                    delay: index * 0.1 + 0.2,
                                  }}>
                                  <p className="text-xl md:text-2xl font-bold text-yellow-300 mb-1">
                                    {(
                                      (item.totalAcessos / (totalAcessos || 1)) *
                                      100
                                    ).toFixed(0)}
                                    %
                                  </p>
                                  <div className="w-24 md:w-32 h-1.5 bg-white/20 rounded-full overflow-hidden">
                                    <motion.div
                                      className="h-full bg-yellow-400 rounded-full"
                                      initial={{ width: 0 }}
                                      whileInView={{
                                        width: `${
                                          (item.totalAcessos /
                                            (totalAcessos || 1)) *
                                          100
                                        }%`,
                                      }}
                                      viewport={{ once: true }}
                                      transition={{
                                        duration: 1,
                                        delay: index * 0.1 + 0.3,
                                      }}
                                    />
                                  </div>
                                </motion.div>
                              </motion.div>
                            );
                          })}
                        </div>
                      </div>
                    </div>
                  </div>
                </StorySlide>
              </>
            )}

            <StorySlide className="bg-linear-to-br from-slate-50 to-blue-50">
              <div className="max-w-7xl mx-auto w-full h-full px-6 py-12">
                <div className="h-full flex flex-col md:flex-row items-center justify-center gap-12">
                  <div className="space-y-8 w-full md:w-1/2 text-center md:text-left">
                    <motion.div
                      initial={{ scale: 0 }}
                      whileInView={{ scale: 1 }}
                      viewport={{ once: true }}
                      transition={{ type: 'spring', duration: 0.6 }}>
                      <img src={logoAImg} className="w-10" />
                    </motion.div>

                    <h2 className="text-4xl md:text-5xl font-bold text-[#0e233d]">
                      A Ágape deseja a você um {ano + 1} repleto de sucesso!
                    </h2>

                    <p className="text-xl text-muted-foreground max-w-xl">
                      Obrigado por fazer parte desta jornada de transformação
                      digital na gestão pública. Sua dedicação é essencial para
                      construirmos um serviço público mais eficiente e
                      transparente.
                    </p>

                    <motion.div
                      className="text-sm text-muted-foreground"
                      initial={{ opacity: 0 }}
                      whileInView={{ opacity: 1 }}
                      viewport={{ once: true }}
                      transition={{ duration: 0.6, delay: 0.4 }}>
                      <p>&copy; {ano} Ágape Sistemas e Tecnologia</p>
                      <p className="mt-2">
                        O Futuro da Gestão Pública começa aqui!
                      </p>
                    </motion.div>
                  </div>

                  <div className="w-full md:w-1/2 flex justify-center">
                    <div className="max-w-md w-full">
                      <ProfileCard
                        name="Usuário Ágape"
                        title="Gestor Público"
                        handle="agape"
                        status="Online"
                        contactText="Contato"
                        avatarUrl="https://github.com/GuilhermeSantosUI.png"
                        totalAcessos={totalAcessos}
                        aplicacoesCount={aplicacoesCount}
                        topAppName={topAppName}
                        topAppAcessos={topAppAcessos}
                        showUserInfo={true}
                        enableTilt={true}
                        enableMobileTilt={false}
                        onContactClick={() => console.log('Contact clicked')}
                      />
                    </div>
                  </div>
                </div>
              </div>
            </StorySlide>
          </div>
        </>
      )}
    </div>
  );
}