import { User, Clock, ChevronRight } from 'lucide-react';
import { Input } from '@/ui/Input';
import { Heading } from '@/ui/Heading';
import { CountrySelect } from '@/ui/CountrySelect';
import { Box } from '@/ui/Box';
import { Stack } from '@/ui/Stack';
import { Text } from '@/ui/Text';
import { Icon } from '@/ui/Icon';
import { Grid } from '@/ui/Grid';
import { Select } from '@/ui/Select';

export interface PersonalInfo {
  firstName: string;
  lastName: string;
  displayName: string;
  country: string;
  timezone: string;
}

interface FormErrors {
  [key: string]: string | undefined;
}

interface PersonalInfoStepProps {
  personalInfo: PersonalInfo;
  setPersonalInfo: (info: PersonalInfo) => void;
  errors: FormErrors;
  loading: boolean;
}

const TIMEZONES = [
  { value: 'America/New_York', label: 'Eastern Time (ET)' },
  { value: 'America/Chicago', label: 'Central Time (CT)' },
  { value: 'America/Denver', label: 'Mountain Time (MT)' },
  { value: 'America/Los_Angeles', label: 'Pacific Time (PT)' },
  { value: 'Europe/London', label: 'Greenwich Mean Time (GMT)' },
  { value: 'Europe/Berlin', label: 'Central European Time (CET)' },
  { value: 'Europe/Paris', label: 'Central European Time (CET)' },
  { value: 'Australia/Sydney', label: 'Australian Eastern Time (AET)' },
  { value: 'Asia/Tokyo', label: 'Japan Standard Time (JST)' },
  { value: 'America/Sao_Paulo', label: 'Brasília Time (BRT)' },
];

export function PersonalInfoStep({ personalInfo, setPersonalInfo, errors, loading }: PersonalInfoStepProps) {
  return (
    <Stack gap={6}>
      <Box>
        <Heading level={2} icon={<Icon icon={User} size={5} color="text-primary-blue" />}>
          Personal Information
        </Heading>
        <Text size="sm" color="text-gray-400" block mt={1}>
          Tell us a bit about yourself
        </Text>
      </Box>

      <Grid cols={2} gap={4}>
        <Box>
          <Text as="label" htmlFor="firstName" size="sm" weight="medium" color="text-gray-300" block mb={2}>
            First Name *
          </Text>
          <Input
            id="firstName"
            type="text"
            value={personalInfo.firstName}
            onChange={(e: React.ChangeEvent<HTMLInputElement>) =>
              setPersonalInfo({ ...personalInfo, firstName: e.target.value })
            }
            variant={errors.firstName ? 'error' : 'default'}
            errorMessage={errors.firstName}
            placeholder="John"
            disabled={loading}
          />
        </Box>

        <Box>
          <Text as="label" htmlFor="lastName" size="sm" weight="medium" color="text-gray-300" block mb={2}>
            Last Name *
          </Text>
          <Input
            id="lastName"
            type="text"
            value={personalInfo.lastName}
            onChange={(e: React.ChangeEvent<HTMLInputElement>) =>
              setPersonalInfo({ ...personalInfo, lastName: e.target.value })
            }
            variant={errors.lastName ? 'error' : 'default'}
            errorMessage={errors.lastName}
            placeholder="Racer"
            disabled={loading}
          />
        </Box>
      </Grid>

      <Box>
        <Text as="label" htmlFor="displayName" size="sm" weight="medium" color="text-gray-300" block mb={2}>
          Display Name * <Text color="text-gray-500" weight="normal">(shown publicly)</Text>
        </Text>
        <Input
          id="displayName"
          type="text"
          value={personalInfo.displayName}
          onChange={(e: React.ChangeEvent<HTMLInputElement>) =>
            setPersonalInfo({ ...personalInfo, displayName: e.target.value })
          }
          variant={errors.displayName ? 'error' : 'default'}
          errorMessage={errors.displayName}
          placeholder="SpeedyRacer42"
          disabled={loading}
        />
      </Box>

      <Grid cols={2} gap={4}>
        <Box>
          <Text as="label" htmlFor="country" size="sm" weight="medium" color="text-gray-300" block mb={2}>
            Country *
          </Text>
          <CountrySelect
            value={personalInfo.country}
            onChange={(value: string) =>
              setPersonalInfo({ ...personalInfo, country: value })
            }
            error={!!errors.country}
            errorMessage={errors.country ?? ''}
            disabled={loading}
          />
        </Box>

        <Box>
          <Text as="label" htmlFor="timezone" size="sm" weight="medium" color="text-gray-300" block mb={2}>
            Timezone
          </Text>
          <Box position="relative">
            <Box position="absolute" left={3} top="50%" style={{ transform: 'translateY(-50%)' }} zIndex={10}>
              <Icon icon={Clock} size={4} color="text-gray-500" />
            </Box>
            <Select
              id="timezone"
              value={personalInfo.timezone}
              onChange={(e: React.ChangeEvent<HTMLSelectElement>) =>
                setPersonalInfo({ ...personalInfo, timezone: e.target.value })
              }
              options={[
                { value: '', label: 'Select timezone' },
                ...TIMEZONES
              ]}
              className="pl-10"
              disabled={loading}
            />
            <Box position="absolute" right={3} top="50%" style={{ transform: 'translateY(-50%)' }} pointerEvents="none">
              <Icon icon={ChevronRight} size={4} color="text-gray-500" className="rotate-90" />
            </Box>
          </Box>
        </Box>
      </Grid>
    </Stack>
  );
}
