import React from 'react';
import { Navigate, useNavigate } from 'react-router-dom';
import { useTranslation } from 'react-i18next';
import { YStack, XStack } from '@tamagui/stacks';
import { SizableText as Text } from '@tamagui/text';
import { Pressable } from '@tamagui/react-native-web-lite';
import { MobileShell } from './components/MobileShell';
import { MobileCard, CTAButton } from './components/Primitives';
import { useEventContext } from '../context/EventContext';
import { formatEventDate, resolveEngagementMode, resolveEventDisplayName } from '../lib/events';
import { adminPath } from '../constants';
import { useAdminTheme } from './theme';

export default function MobileTasksTabPage() {
  const { events, activeEvent, hasEvents, selectEvent } = useEventContext();
  const { t, i18n } = useTranslation('management');
  const navigate = useNavigate();
  const { text, muted, border, primary } = useAdminTheme();
  const tasksEnabled = resolveEngagementMode(activeEvent ?? null) !== 'photo_only';

  if (activeEvent?.slug && tasksEnabled) {
    return <Navigate to={adminPath(`/mobile/events/${activeEvent.slug}/tasks`)} replace />;
  }

  if (activeEvent?.slug && !tasksEnabled) {
    return (
      <MobileShell activeTab="tasks" title={t('events.tasks.title', 'Photo tasks')}>
        <MobileCard alignItems="flex-start" gap="$3">
          <Text fontSize="$lg" fontWeight="800" color={text}>
            {t('events.tasks.disabledTitle', 'Photo task mode is off for this event')}
          </Text>
          <Text fontSize="$sm" color={muted}>
            {t('events.tasks.disabledBody', 'Guests see only the photo feed. Enable photo tasks in the event settings to show them again.')}
          </Text>
          <CTAButton
            label={t('events.actions.settings', 'Event settings')}
            onPress={() => navigate(adminPath(`/mobile/events/${activeEvent.slug}/edit`))}
          />
        </MobileCard>
      </MobileShell>
    );
  }

  if (!hasEvents) {
    return (
      <MobileShell activeTab="tasks" title={t('events.tasks.title', 'Photo tasks')}>
        <MobileCard alignItems="flex-start" gap="$3">
          <Text fontSize="$lg" fontWeight="800" color={text}>
            {t('events.tasks.emptyTitle', 'Create an event first')}
          </Text>
          <Text fontSize="$sm" color={muted}>
            {t('events.tasks.emptyBody', 'Start an event to add photo tasks, challenges, and checklists.')}
          </Text>
          <CTAButton
            label={t('events.actions.create', 'Create Event')}
            onPress={() => navigate(adminPath('/mobile/events/new'))}
          />
        </MobileCard>
      </MobileShell>
    );
  }

  const locale = i18n.language?.startsWith('en') ? 'en-GB' : 'de-DE';

  return (
    <MobileShell activeTab="tasks" title={t('events.tasks.title', 'Photo tasks')}>
      <YStack gap="$2">
        <Text fontSize="$sm" color={text} fontWeight="700">
          {t('events.tasks.pickEvent', 'Pick an event to manage photo tasks')}
        </Text>
        {events.map((event) => (
          <Pressable
            key={event.slug}
            onPress={() => {
              selectEvent(event.slug ?? null);
              if (event.slug) {
                navigate(adminPath(`/mobile/events/${event.slug}/tasks`));
              }
            }}
          >
            <MobileCard borderColor={border} gap="$2">
              <XStack alignItems="center" justifyContent="space-between">
                <YStack gap="$1">
                  <Text fontSize="$md" fontWeight="800" color={text}>
                    {resolveEventDisplayName(event)}
                  </Text>
                  <Text fontSize="$xs" color={muted}>
                    {formatEventDate(event.event_date, locale) ?? t('events.status.draft', 'Draft')}
                  </Text>
                </YStack>
                <Text fontSize="$sm" color={primary} fontWeight="700">
                  {t('events.actions.open', 'Open')}
                </Text>
              </XStack>
            </MobileCard>
          </Pressable>
        ))}
      </YStack>
    </MobileShell>
  );
}
