import React from 'react';
import { useNavigate } from 'react-router-dom';
import { useTranslation } from 'react-i18next';
import { CheckCircle2, Circle, Loader2, Pencil, Plus, Trash2 } from 'lucide-react';

import { Alert, AlertDescription, AlertTitle } from '@/components/ui/alert';
import { Badge } from '@/components/ui/badge';
import { Button } from '@/components/ui/button';
import { Card, CardContent, CardDescription, CardHeader, CardTitle } from '@/components/ui/card';
import { Dialog, DialogContent, DialogHeader, DialogTitle } from '@/components/ui/dialog';
import { Input } from '@/components/ui/input';
import { Label } from '@/components/ui/label';
import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from '@/components/ui/select';
import { Switch } from '@/components/ui/switch';

import { AdminLayout } from '../components/AdminLayout';
import {
  createTask,
  deleteTask,
  getTasks,
  PaginationMeta,
  TenantTask,
  TaskPayload,
  updateTask,
} from '../api';
import { isAuthError } from '../auth/tokens';
import { buildEngagementTabPath } from '../constants';

type TaskFormState = {
  title: string;
  description: string;
  priority: TaskPayload['priority'];
  due_date: string;
  is_completed: boolean;
};

const INITIAL_FORM: TaskFormState = {
  title: '',
  description: '',
  priority: 'medium',
  due_date: '',
  is_completed: false,
};

export type TasksSectionProps = {
  embedded?: boolean;
  onNavigateToCollections?: () => void;
};

export function TasksSection({ embedded = false, onNavigateToCollections }: TasksSectionProps) {
  const navigate = useNavigate();
  const { t } = useTranslation('common');

  const [tasks, setTasks] = React.useState<TenantTask[]>([]);
  const [meta, setMeta] = React.useState<PaginationMeta | null>(null);
  const [page, setPage] = React.useState(1);
  const [search, setSearch] = React.useState('');
  const [loading, setLoading] = React.useState(true);
  const [error, setError] = React.useState<string | null>(null);
  const [dialogOpen, setDialogOpen] = React.useState(false);
  const [editingTask, setEditingTask] = React.useState<TenantTask | null>(null);
  const [form, setForm] = React.useState<TaskFormState>(INITIAL_FORM);
  const [saving, setSaving] = React.useState(false);

  React.useEffect(() => {
    let cancelled = false;
    setLoading(true);
    setError(null);

    getTasks({ page, search: search.trim() || undefined })
      .then((result) => {
        if (cancelled) return;
        setTasks(result.data);
        setMeta(result.meta);
      })
      .catch((err) => {
        if (!isAuthError(err)) {
          setError('Tasks konnten nicht geladen werden.');
        }
      })
      .finally(() => {
        if (!cancelled) {
          setLoading(false);
        }
      });

    return () => {
      cancelled = true;
    };
  }, [page, search]);

  const openCreate = React.useCallback(() => {
    setEditingTask(null);
    setForm(INITIAL_FORM);
    setDialogOpen(true);
  }, []);

  const openEdit = React.useCallback((task: TenantTask) => {
    setEditingTask(task);
    setForm({
      title: task.title,
      description: task.description ?? '',
      priority: task.priority ?? 'medium',
      due_date: task.due_date ? task.due_date.slice(0, 10) : '',
      is_completed: task.is_completed,
    });
    setDialogOpen(true);
  }, []);

  const handleNavigateToCollections = React.useCallback(() => {
    if (onNavigateToCollections) {
      onNavigateToCollections();
      return;
    }
    navigate(buildEngagementTabPath('collections'));
  }, [navigate, onNavigateToCollections]);

  async function handleSubmit(event: React.FormEvent<HTMLFormElement>) {
    event.preventDefault();
    if (!form.title.trim()) {
      setError('Bitte gib einen Titel ein.');
      return;
    }

    setSaving(true);
    setError(null);
    const payload: TaskPayload = {
      title: form.title.trim(),
      description: form.description.trim() || null,
      priority: form.priority ?? undefined,
      due_date: form.due_date || undefined,
      is_completed: form.is_completed,
    };

    try {
      if (editingTask) {
        const updated = await updateTask(editingTask.id, payload);
        setTasks((prev) => prev.map((task) => (task.id === updated.id ? updated : task)));
      } else {
        const created = await createTask(payload);
        setTasks((prev) => [created, ...prev]);
      }
      setDialogOpen(false);
    } catch (err) {
      if (!isAuthError(err)) {
        setError('Task konnte nicht gespeichert werden.');
      }
    } finally {
      setSaving(false);
    }
  }

  async function handleDelete(taskId: number) {
    if (!window.confirm('Task wirklich löschen?')) {
      return;
    }

    try {
      await deleteTask(taskId);
      setTasks((prev) => prev.filter((task) => task.id !== taskId));
    } catch (err) {
      if (!isAuthError(err)) {
        setError('Task konnte nicht gelöscht werden.');
      }
    }
  }

  async function toggleCompletion(task: TenantTask) {
    if (task.tenant_id === null) {
      return;
    }
    try {
      const updated = await updateTask(task.id, { is_completed: !task.is_completed });
      setTasks((prev) => prev.map((entry) => (entry.id === updated.id ? updated : entry)));
    } catch (err) {
      if (!isAuthError(err)) {
        setError('Status konnte nicht aktualisiert werden.');
      }
    }
  }

  const title = embedded ? 'Aufgaben' : 'Task Bibliothek';
  const subtitle = embedded
    ? 'Plane Aufgaben, Aktionen und Highlights für deine Gäste.'
    : 'Weise Aufgaben zu und tracke Fortschritt rund um deine Events.';

  return (
    <div className="space-y-6">
      {error && (
        <Alert variant="destructive">
          <AlertTitle>Fehler</AlertTitle>
          <AlertDescription>{error}</AlertDescription>
        </Alert>
      )}

      <Card className="border-0 bg-white/85 shadow-xl shadow-pink-100/60">
        <CardHeader className="flex flex-col gap-4 sm:flex-row sm:items-start sm:justify-between">
          <div>
            <CardTitle className="text-xl text-slate-900">{title}</CardTitle>
            <CardDescription className="text-sm text-slate-600">{subtitle}</CardDescription>
          </div>
          <div className="flex flex-wrap gap-2">
            <Button variant="outline" onClick={handleNavigateToCollections}>
              {t('navigation.collections')}
            </Button>
            <Button
              className="bg-gradient-to-r from-pink-500 via-fuchsia-500 to-purple-500 text-white shadow-lg shadow-pink-500/20"
              onClick={openCreate}
            >
              <Plus className="h-4 w-4" />
              Neu
            </Button>
          </div>
        </CardHeader>
        <CardContent className="space-y-6">
          <div className="flex flex-col gap-3 sm:flex-row sm:items-center sm:justify-between">
            <Input
              placeholder="Nach Aufgaben suchen ..."
              value={search}
              onChange={(event) => {
                setPage(1);
                setSearch(event.target.value);
              }}
              className="max-w-sm"
            />
            {meta && meta.total > 0 ? (
              <div className="text-xs text-slate-500">
                Seite {meta.current_page} von {meta.last_page} · {meta.total} Einträge
              </div>
            ) : null}
          </div>

          {loading ? (
            <TasksSkeleton />
          ) : tasks.length === 0 ? (
            <EmptyState onCreate={openCreate} />
          ) : (
            <div className="grid gap-3">
              {tasks.map((task) => (
                <TaskRow
                  key={task.id}
                  task={task}
                  onToggle={() => toggleCompletion(task)}
                  onEdit={() => openEdit(task)}
                  onDelete={() => handleDelete(task.id)}
                />
              ))}
            </div>
          )}

          {meta && meta.last_page > 1 ? (
            <div className="flex flex-wrap items-center justify-between gap-2 border-t border-slate-100 pt-4 text-sm">
              <div className="text-slate-500">
                Insgesamt {meta.total} Aufgaben · Seite {meta.current_page} von {meta.last_page}
              </div>
              <div className="flex gap-2">
                <Button variant="outline" size="sm" onClick={() => setPage((page) => Math.max(page - 1, 1))} disabled={meta.current_page <= 1}>
                  Zurück
                </Button>
                <Button
                  variant="outline"
                  size="sm"
                  onClick={() => setPage((page) => Math.min(page + 1, meta.last_page ?? page + 1))}
                  disabled={meta.current_page >= (meta.last_page ?? 1)}
                >
                  Weiter
                </Button>
              </div>
            </div>
          ) : null}
        </CardContent>
      </Card>

      <Dialog open={dialogOpen} onOpenChange={setDialogOpen}>
        <DialogContent>
          <DialogHeader>
            <DialogTitle>{editingTask ? 'Task bearbeiten' : 'Neue Task erstellen'}</DialogTitle>
          </DialogHeader>
          <form onSubmit={handleSubmit} className="space-y-4">
            <div className="space-y-2">
              <Label htmlFor="task-title">Titel</Label>
              <Input
                id="task-title"
                value={form.title}
                onChange={(event) => setForm((prev) => ({ ...prev, title: event.target.value }))}
                required
              />
            </div>
            <div className="space-y-2">
              <Label htmlFor="task-description">Beschreibung</Label>
              <Input
                id="task-description"
                value={form.description}
                onChange={(event) => setForm((prev) => ({ ...prev, description: event.target.value }))}
                placeholder="Was sollen Gäste machen?"
              />
            </div>
            <div className="grid gap-4 sm:grid-cols-2">
              <div className="space-y-2">
                <Label htmlFor="task-priority">Priorität</Label>
                <Select
                  value={form.priority ?? 'medium'}
                  onValueChange={(value) =>
                    setForm((prev) => ({ ...prev, priority: value as TaskPayload['priority'] }))
                  }
                >
                  <SelectTrigger id="task-priority">
                    <SelectValue placeholder="Priorität wählen" />
                  </SelectTrigger>
                  <SelectContent>
                    <SelectItem value="low">Niedrig</SelectItem>
                    <SelectItem value="medium">Mittel</SelectItem>
                    <SelectItem value="high">Hoch</SelectItem>
                    <SelectItem value="urgent">Dringend</SelectItem>
                  </SelectContent>
                </Select>
              </div>
              <div className="space-y-2">
                <Label htmlFor="task-due-date">Fälligkeitsdatum</Label>
                <Input
                  id="task-due-date"
                  type="date"
                  value={form.due_date}
                  onChange={(event) => setForm((prev) => ({ ...prev, due_date: event.target.value }))}
                />
              </div>
            </div>

            <div className="flex items-center justify-between rounded-lg border border-slate-200 bg-slate-50/50 p-3">
              <div>
                <p className="text-sm font-medium text-slate-700">Bereits erledigt?</p>
                <p className="text-xs text-slate-500">Markiere Aufgaben als abgeschlossen, wenn sie nicht mehr sichtbar sein sollen.</p>
              </div>
              <Switch checked={form.is_completed} onCheckedChange={(checked) => setForm((prev) => ({ ...prev, is_completed: checked }))} />
            </div>

            <div className="flex justify-end gap-2">
              <Button type="button" variant="outline" onClick={() => setDialogOpen(false)}>
                Abbrechen
              </Button>
              <Button type="submit" disabled={saving} className="bg-gradient-to-r from-pink-500 via-fuchsia-500 to-purple-500 text-white">
                {saving ? <Loader2 className="mr-2 h-4 w-4 animate-spin" /> : null}
                Speichern
              </Button>
            </div>
          </form>
        </DialogContent>
      </Dialog>
    </div>
  );
}

export default function TasksPage() {
  const navigate = useNavigate();
  const { t: tc } = useTranslation('common');
  return (
    <AdminLayout
      title={tc('navigation.tasks')}
      subtitle="Weise Aufgaben zu und tracke Fortschritt rund um deine Events."
    >
      <TasksSection onNavigateToCollections={() => navigate(buildEngagementTabPath('collections'))} />
    </AdminLayout>
  );
}

function TaskRow({
  task,
  onToggle,
  onEdit,
  onDelete,
}: {
  task: TenantTask;
  onToggle: () => void;
  onEdit: () => void;
  onDelete: () => void;
}) {
  const isCompleted = task.is_completed;
  const statusIcon = isCompleted ? <CheckCircle2 className="h-4 w-4 text-emerald-500" /> : <Circle className="h-4 w-4 text-slate-300" />;
  return (
    <div className="flex flex-col gap-3 rounded-xl border border-slate-200/70 bg-white/80 p-4 shadow-sm shadow-pink-100/20 sm:flex-row sm:items-center sm:justify-between">
      <div className="flex items-start gap-3">
        <button type="button" onClick={onToggle} className="mt-1 text-slate-500 transition-colors hover:text-emerald-500">
          {statusIcon}
        </button>
        <div className="space-y-1">
          <div className="flex flex-wrap items-center gap-2">
            <span className="text-sm font-medium text-slate-900">{task.title}</span>
            {task.priority ? <PriorityBadge priority={task.priority} /> : null}
            {task.collection_id ? <Badge variant="secondary">Vorlage #{task.collection_id}</Badge> : null}
          </div>
          {task.description ? <p className="text-xs text-slate-500">{task.description}</p> : null}
        </div>
      </div>
      <div className="flex gap-2">
        <Button variant="outline" size="sm" onClick={onEdit}>
          <Pencil className="mr-1 h-4 w-4" />
          Bearbeiten
        </Button>
        <Button variant="ghost" size="sm" onClick={onDelete} className="text-slate-500 hover:text-rose-500">
          <Trash2 className="mr-1 h-4 w-4" />
          Löschen
        </Button>
      </div>
    </div>
  );
}

function PriorityBadge({ priority }: { priority: NonNullable<TaskPayload['priority']> }) {
  const mapping: Record<NonNullable<TaskPayload['priority']>, { label: string; className: string }> = {
    low: { label: 'Niedrig', className: 'bg-emerald-50 text-emerald-600' },
    medium: { label: 'Mittel', className: 'bg-amber-50 text-amber-600' },
    high: { label: 'Hoch', className: 'bg-rose-50 text-rose-600' },
    urgent: { label: 'Dringend', className: 'bg-red-50 text-red-600' },
  };
  const { label, className } = mapping[priority];
  return <Badge className={`border-none ${className}`}>{label}</Badge>;
}

function TasksSkeleton() {
  return (
    <div className="space-y-3">
      {Array.from({ length: 4 }).map((_, index) => (
        <div key={`task-skeleton-${index}`} className="h-16 animate-pulse rounded-xl bg-gradient-to-r from-white/40 via-white/60 to-white/40" />
      ))}
    </div>
  );
}

function EmptyState({ onCreate }: { onCreate: () => void }) {
  return (
    <div className="flex flex-col items-center justify-center gap-3 rounded-xl border border-dashed border-slate-200 bg-slate-50/60 p-10 text-center">
      <h3 className="text-base font-semibold text-slate-800">Noch keine Tasks angelegt</h3>
      <p className="text-sm text-slate-500">
        Starte mit einer neuen Aufgabe oder importiere Aufgabenvorlagen, um deine Gäste zu inspirieren.
      </p>
      <Button onClick={onCreate} className="bg-gradient-to-r from-pink-500 via-fuchsia-500 to-purple-500 text-white">
        <Plus className="mr-1 h-4 w-4" />
        Erste Task erstellen
      </Button>
    </div>
  );
}
