import React, { useEffect, useState } from 'react';
import { Box, Text, useInput } from 'ink';
import type { CountryDTO } from '@effigenix/api-client';
import { useNavigation } from '../../../state/navigation-context.js';
import { useSuppliers } from '../../../hooks/useSuppliers.js';
import { FormInput } from '../../shared/FormInput.js';
import { CountryPicker } from '../../shared/CountryPicker.js';
import { LoadingSpinner } from '../../shared/LoadingSpinner.js';
import { ErrorDisplay } from '../../shared/ErrorDisplay.js';
import { client } from '../../../utils/api-client.js';
import { DEFAULT_COUNTRY, DACH_FALLBACK } from '../../shared/country-defaults.js';

type Field = 'name' | 'phone' | 'email' | 'contactPerson' | 'street' | 'houseNumber' | 'postalCode' | 'city' | 'countryPicker' | 'paymentDueDays';
const FIELDS: Field[] = ['name', 'phone', 'email', 'contactPerson', 'street', 'houseNumber', 'postalCode', 'city', 'countryPicker', 'paymentDueDays'];

const FIELD_LABELS: Record<Field, string> = {
  name: 'Name *',
  phone: 'Telefon *',
  email: 'E-Mail',
  contactPerson: 'Ansprechpartner',
  street: 'Straße',
  houseNumber: 'Hausnummer',
  postalCode: 'PLZ',
  city: 'Stadt',
  countryPicker: 'Land',
  paymentDueDays: 'Zahlungsziel (Tage)',
};

export function SupplierCreateScreen() {
  const { replace, back } = useNavigation();
  const { createSupplier, loading, error, clearError } = useSuppliers();

  const [values, setValues] = useState<Record<Exclude<Field, 'countryPicker'>, string>>({
    name: '', phone: '', email: '', contactPerson: '',
    street: '', houseNumber: '', postalCode: '', city: '',
    paymentDueDays: '',
  });
  const [activeField, setActiveField] = useState<Field>('name');
  const [fieldErrors, setFieldErrors] = useState<Partial<Record<Field, string>>>({});
  const [countryQuery, setCountryQuery] = useState('');
  const [countryCode, setCountryCode] = useState(DEFAULT_COUNTRY.code);
  const [countryName, setCountryName] = useState(DEFAULT_COUNTRY.name);
  const [countries, setCountries] = useState<CountryDTO[]>([]);

  useEffect(() => {
    client.countries.search().then(setCountries).catch(() => setCountries(DACH_FALLBACK));
  }, []);

  const setField = (field: Exclude<Field, 'countryPicker'>) => (value: string) => {
    setValues((v) => ({ ...v, [field]: value }));
  };

  useInput((_input, key) => {
    if (loading) return;
    if (key.tab || key.downArrow) {
      setActiveField((f) => {
        const idx = FIELDS.indexOf(f);
        return FIELDS[(idx + 1) % FIELDS.length] ?? f;
      });
    }
    if (key.upArrow) {
      setActiveField((f) => {
        const idx = FIELDS.indexOf(f);
        return FIELDS[(idx - 1 + FIELDS.length) % FIELDS.length] ?? f;
      });
    }
    if (key.escape) back();
  });

  const handleSubmit = async () => {
    const errors: Partial<Record<Field, string>> = {};
    if (!values.name.trim()) errors.name = 'Name ist erforderlich.';
    if (!values.phone.trim()) errors.phone = 'Telefon ist erforderlich.';
    setFieldErrors(errors);
    if (Object.keys(errors).length > 0) return;

    const result = await createSupplier({
      name: values.name.trim(),
      phone: values.phone.trim(),
      ...(values.email.trim() ? { email: values.email.trim() } : {}),
      ...(values.contactPerson.trim() ? { contactPerson: values.contactPerson.trim() } : {}),
      ...(values.street.trim() ? { street: values.street.trim() } : {}),
      ...(values.houseNumber.trim() ? { houseNumber: values.houseNumber.trim() } : {}),
      ...(values.postalCode.trim() ? { postalCode: values.postalCode.trim() } : {}),
      ...(values.city.trim() ? { city: values.city.trim() } : {}),
      ...(countryCode ? { country: countryCode } : {}),
      ...(values.paymentDueDays.trim() ? { paymentDueDays: parseInt(values.paymentDueDays, 10) } : {}),
    });
    if (result) replace('supplier-list');
  };

  const handleFieldSubmit = (field: Field) => (_value: string) => {
    const idx = FIELDS.indexOf(field);
    if (idx < FIELDS.length - 1) {
      setActiveField(FIELDS[idx + 1] ?? field);
    } else {
      void handleSubmit();
    }
  };

  if (loading) {
    return (
      <Box flexDirection="column" alignItems="center" paddingY={2}>
        <LoadingSpinner label="Lieferant wird angelegt..." />
      </Box>
    );
  }

  return (
    <Box flexDirection="column" gap={1}>
      <Text color="cyan" bold>Neuer Lieferant</Text>
      {error && <ErrorDisplay message={error} onDismiss={clearError} />}

      <Box flexDirection="column" gap={1} width={60}>
        {FIELDS.map((field) => {
          if (field === 'countryPicker') {
            return (
              <CountryPicker
                key="countryPicker"
                countries={countries}
                query={countryQuery}
                onQueryChange={setCountryQuery}
                onSelect={(c) => {
                  setCountryCode(c.code);
                  setCountryName(c.name);
                  setCountryQuery('');
                  const idx = FIELDS.indexOf('countryPicker');
                  if (idx < FIELDS.length - 1) {
                    setActiveField(FIELDS[idx + 1] ?? 'countryPicker');
                  }
                }}
                focus={activeField === 'countryPicker'}
                selectedName={countryName}
              />
            );
          }
          const f = field as Exclude<Field, 'countryPicker'>;
          return (
            <FormInput
              key={f}
              label={FIELD_LABELS[f]}
              value={values[f]}
              onChange={setField(f)}
              onSubmit={handleFieldSubmit(f)}
              focus={activeField === f}
              {...(fieldErrors[f] ? { error: fieldErrors[f] } : {})}
            />
          );
        })}
      </Box>

      <Box marginTop={1}>
        <Text color="gray" dimColor>
          Tab/↑↓ Feld wechseln · Enter auf letztem Feld speichern · Escape Abbrechen
        </Text>
      </Box>
    </Box>
  );
}
