'use client';

import type { DriverProfileViewData } from '@/lib/types/view-data/DriverProfileViewData';
import { DriverProfileTemplate } from '@/templates/DriverProfileTemplate';
import { Container } from '@/ui/Container';
import { Stack } from '@/ui/primitives/Stack';
import type { ProfileTab } from '@/ui/ProfileTabs';
import { Text } from '@/ui/Text';
import { useRouter } from 'next/navigation';
import { useState } from 'react';

interface DriverProfilePageClientProps {
  viewData: DriverProfileViewData | null;
  error?: string;
  empty?: {
    title: string;
    description: string;
  };
}

/**
 * DriverProfilePageClient
 *
 * Client component that:
 * 1. Handles UI state (tabs, friend requests)
 * 2. Passes ViewData directly to Template
 */
export function DriverProfilePageClient({ viewData, error, empty }: DriverProfilePageClientProps) {
  const router = useRouter();
  
  // UI State (UI-only concerns)
  const [activeTab, setActiveTab] = useState<ProfileTab>('overview');
  const [friendRequestSent, setFriendRequestSent] = useState(false);

  // Event handlers (UI-only concerns)
  const handleAddFriend = () => {
    setFriendRequestSent(true);
  };

  const handleBackClick = () => {
    router.push('/drivers');
  };

  // Handle error/empty states
  if (error) {
    return (
      <Container size="lg" py={12}>
        <Stack align="center" gap={4}>
          <Text color="text-red-400">Error loading driver profile</Text>
          <Text color="text-gray-400">Please try again later</Text>
        </Stack>
      </Container>
    );
  }

  if (!viewData || !viewData.currentDriver) {
    if (empty) {
      return (
        <Container size="lg" py={12}>
          <Stack align="center" gap={2}>
            <Text size="xl" weight="semibold" color="text-white">{empty.title}</Text>
            <Text color="text-gray-400">{empty.description}</Text>
          </Stack>
        </Container>
      );
    }
    return null;
  }

  // Pass ViewData directly to template
  return (
    <DriverProfileTemplate
      viewData={viewData}
      onBackClick={handleBackClick}
      onAddFriend={handleAddFriend}
      friendRequestSent={friendRequestSent}
      activeTab={activeTab}
      onTabChange={setActiveTab}
    />
  );
}