﻿import React from 'react';
import { Link, useParams } from 'react-router-dom';
import { Button } from '@/components/ui/button';
import { Card, CardContent, CardDescription, CardHeader, CardTitle } from '@/components/ui/card';
import { Separator } from '@/components/ui/separator';
import EmotionPicker from '../components/EmotionPicker';
import GalleryPreview from '../components/GalleryPreview';
import { useGuestIdentity } from '../context/GuestIdentityContext';
import { useEventStats } from '../context/EventStatsContext';
import { useEventData } from '../hooks/useEventData';
import { useGuestTaskProgress } from '../hooks/useGuestTaskProgress';
import { Sparkles, UploadCloud, Images, CheckCircle2, Users, TimerReset } from 'lucide-react';
import { useTranslation, type TranslateFn } from '../i18n/useTranslation';

export default function HomePage() {
  const { token } = useParams<{ token: string }>();
  const { name, hydrated } = useGuestIdentity();
  const stats = useEventStats();
  const { event } = useEventData();
  const { completedCount } = useGuestTaskProgress(token);
  const { t } = useTranslation();

  if (!token) return null;

  const displayName = hydrated && name ? name : t('home.fallbackGuestName');
  const eventNameDisplay = event?.name ?? t('home.hero.defaultEventName');
  const latestUploadText = formatLatestUpload(stats.latestPhotoAt, t);

  const primaryActions = React.useMemo(
    () => [
      {
        to: 'tasks',
        label: t('home.actions.items.tasks.label'),
        description: t('home.actions.items.tasks.description'),
        icon: <Sparkles className="h-5 w-5" />,
      },
      {
        to: 'upload',
        label: t('home.actions.items.upload.label'),
        description: t('home.actions.items.upload.description'),
        icon: <UploadCloud className="h-5 w-5" />,
      },
      {
        to: 'gallery',
        label: t('home.actions.items.gallery.label'),
        description: t('home.actions.items.gallery.description'),
        icon: <Images className="h-5 w-5" />,
      },
    ],
    [t],
  );

  const checklistItems = React.useMemo(
    () => [
      t('home.checklist.steps.first'),
      t('home.checklist.steps.second'),
      t('home.checklist.steps.third'),
    ],
    [t],
  );

  return (
    <div className="space-y-6 pb-24">
      <HeroCard
        name={displayName}
        eventName={eventNameDisplay}
        tasksCompleted={completedCount}
        t={t}
      />

      <Card>
        <CardContent className="grid grid-cols-1 gap-4 py-4 sm:grid-cols-4">
          <StatTile
            icon={<Users className="h-4 w-4" />}
            label={t('home.stats.online')}
            value={`${stats.onlineGuests}`}
          />
          <StatTile
            icon={<Sparkles className="h-4 w-4" />}
            label={t('home.stats.tasksSolved')}
            value={`${stats.tasksSolved}`}
          />
          <StatTile
            icon={<TimerReset className="h-4 w-4" />}
            label={t('home.stats.lastUpload')}
            value={latestUploadText}
          />
          <StatTile
            icon={<CheckCircle2 className="h-4 w-4" />}
            label={t('home.stats.completedTasks')}
            value={`${completedCount}`}
          />
        </CardContent>
      </Card>

      <section className="space-y-3">
        <div className="flex items-center justify-between">
          <h2 className="text-sm font-semibold uppercase tracking-wide text-muted-foreground">
            {t('home.actions.title')}
          </h2>
          <span className="text-xs text-muted-foreground">{t('home.actions.subtitle')}</span>
        </div>
        <div className="grid gap-3 sm:grid-cols-2">
          {primaryActions.map((action) => (
            <Link to={action.to} key={action.to} className="block">
              <Card className="transition-all hover:shadow-lg">
                <CardContent className="flex items-center gap-3 py-4">
                  <div className="flex h-10 w-10 items-center justify-center rounded-lg bg-pink-100 text-pink-600">
                    {action.icon}
                  </div>
                  <div className="flex flex-col">
                    <span className="text-base font-semibold">{action.label}</span>
                    <span className="text-sm text-muted-foreground">{action.description}</span>
                  </div>
                </CardContent>
              </Card>
            </Link>
          ))}
        </div>
        <Button variant="outline" asChild className="w-full">
          <Link to="queue">{t('home.actions.queueButton')}</Link>
        </Button>
      </section>

      <Card>
        <CardHeader>
          <CardTitle>{t('home.checklist.title')}</CardTitle>
          <CardDescription>{t('home.checklist.description')}</CardDescription>
        </CardHeader>
        <CardContent className="space-y-3">
          {checklistItems.map((item) => (
            <div key={item} className="flex items-start gap-3">
              <CheckCircle2 className="mt-0.5 h-5 w-5 text-green-500" />
              <span className="text-sm leading-relaxed text-muted-foreground">{item}</span>
            </div>
          ))}
        </CardContent>
      </Card>

      <Separator />

      <EmotionPicker />

      <GalleryPreview slug={token} />
    </div>
  );
}

function HeroCard({
  name,
  eventName,
  tasksCompleted,
  t,
}: {
  name: string;
  eventName: string;
  tasksCompleted: number;
  t: TranslateFn;
}) {
  const heroTitle = t('home.hero.title').replace('{name}', name);
  const heroDescription = t('home.hero.description').replace('{eventName}', eventName);
  const progressMessage = tasksCompleted > 0
    ? t('home.hero.progress.some').replace('{count}', `${tasksCompleted}`)
    : t('home.hero.progress.none');

  return (
    <Card className="overflow-hidden border-0 bg-gradient-to-r from-pink-500 via-fuchsia-500 to-purple-500 text-white shadow-md">
      <CardHeader className="space-y-1">
        <CardDescription className="text-sm text-white/80">{t('home.hero.subtitle')}</CardDescription>
        <CardTitle className="text-2xl font-bold">{heroTitle}</CardTitle>
        <p className="text-sm text-white/80">{heroDescription}</p>
        <p className="text-sm font-medium text-white/90">{progressMessage}</p>
      </CardHeader>
    </Card>
  );
}

function StatTile({ icon, label, value }: { icon: React.ReactNode; label: string; value: string }) {
  return (
    <div className="flex items-center gap-3 rounded-lg border bg-muted/30 px-3 py-2">
      <div className="flex h-10 w-10 items-center justify-center rounded-full bg-white text-pink-600 shadow-sm">
        {icon}
      </div>
      <div className="flex flex-col">
        <span className="text-xs uppercase tracking-wide text-muted-foreground">{label}</span>
        <span className="text-lg font-semibold text-foreground">{value}</span>
      </div>
    </div>
  );
}

function formatLatestUpload(isoDate: string | null, t: TranslateFn) {
  if (!isoDate) {
    return t('home.latestUpload.none');
  }
  const date = new Date(isoDate);
  if (Number.isNaN(date.getTime())) {
    return t('home.latestUpload.invalid');
  }
  const diffMs = Date.now() - date.getTime();
  const diffMinutes = Math.round(diffMs / 60000);
  if (diffMinutes < 1) {
    return t('home.latestUpload.justNow');
  }
  if (diffMinutes < 60) {
    return t('home.latestUpload.minutes').replace('{count}', `${diffMinutes}`);
  }
  const diffHours = Math.round(diffMinutes / 60);
  if (diffHours < 24) {
    return t('home.latestUpload.hours').replace('{count}', `${diffHours}`);
  }
  const diffDays = Math.round(diffHours / 24);
  return t('home.latestUpload.days').replace('{count}', `${diffDays}`);
}
