import { TenantEvent } from '../../api';
import { adminPath } from '../../constants';

export type ReadinessStep = {
  id: string;
  label: string;
  description?: string; // New: Contextual Why
  isComplete: boolean;
  ctaLabel: string;
  targetPath: string;
  priority: number; 
};

export type ReadinessStatus = {
  steps: ReadinessStep[];
  totalSteps: number;
  completedSteps: number;
  progress: number;
  nextStep: ReadinessStep | null;
  isReady: boolean;
};

export function useEventReadiness(event: TenantEvent | null, t: (key: string, fallback?: string) => string): ReadinessStatus {
  if (!event) {
    return { steps: [], totalSteps: 0, completedSteps: 0, progress: 0, nextStep: null, isReady: false };
  }

  const settings = (event.settings ?? {}) as Record<string, unknown>;
  
  const hasDate = Boolean(event.event_date);
  const hasLocation = Boolean(
    (settings.location as string) || 
    (settings.address as string) || 
    (settings.city as string)
  );
  
  const tasksEnabled = event.engagement_mode !== 'photo_only';
  const hasTasks = (event.tasks_count ?? 0) > 0;
  
  const hasInvite = (event.active_invites_count ?? 0) > 0 || (event.total_invites_count ?? 0) > 0;

  const steps: ReadinessStep[] = [
    {
      id: 'basics',
      label: t('management:events.form.date', 'Datum & Ort'),
      description: 'Grundlage für die Gäste-Info.',
      isComplete: hasDate && hasLocation,
      ctaLabel: t('management:events.actions.edit', 'Bearbeiten'),
      targetPath: `/mobile/events/${event.slug}/edit`,
      priority: 1
    },
    {
      id: 'access',
      label: t('management:invites.badge', 'QR-Codes'),
      description: 'Der Schlüssel für deine Gäste.',
      ctaLabel: t('management:invites.actions.create', 'QR-Code erstellen'),
      isComplete: hasInvite,
      targetPath: `/mobile/events/${event.slug}/qr`,
      priority: 3
    }
  ];

  if (tasksEnabled) {
    steps.push({
      id: 'tasks',
      label: t('management:tasks.badge', 'Aufgaben'),
      description: 'Sorgt für 3x mehr Interaktion.',
      isComplete: hasTasks,
      ctaLabel: t('management:tasks.actions.assign', 'Aufgaben hinzufügen'),
      targetPath: `/mobile/events/${event.slug}/tasks`,
      priority: 2
    });
  }

  steps.sort((a, b) => a.priority - b.priority);

  const completedSteps = steps.filter(s => s.isComplete).length;
  const totalSteps = steps.length;
  const nextStep = steps.find(s => !s.isComplete) ?? null;

  return {
    steps,
    totalSteps,
    completedSteps,
    progress: totalSteps > 0 ? completedSteps / totalSteps : 0,
    nextStep,
    isReady: !nextStep
  };
}
