import type { ReactNode } from 'react';
import {
  Pressable,
  StyleSheet,
  Text,
  TextInput,
  View,
  type KeyboardTypeOptions,
  type TextInputProps,
} from 'react-native';
import { colors } from './theme';

type ButtonVariant = 'primary' | 'secondary' | 'danger' | 'ghost';

export function Button({
  title,
  onPress,
  disabled = false,
  variant = 'primary',
}: {
  title: string;
  onPress: () => void;
  disabled?: boolean;
  variant?: ButtonVariant;
}) {
  return (
    <Pressable
      accessibilityRole="button"
      disabled={disabled}
      onPress={onPress}
      style={({ pressed }) => [
        styles.button,
        buttonVariants[variant],
        disabled && styles.buttonDisabled,
        pressed && !disabled && styles.buttonPressed,
      ]}
    >
      <Text style={[styles.buttonText, buttonTextVariants[variant]]}>{title}</Text>
    </Pressable>
  );
}

export function Field({
  label,
  value,
  onChangeText,
  placeholder,
  secureTextEntry,
  keyboardType,
  multiline,
  autoCapitalize,
  maxLength,
  editable = true,
  autoComplete,
}: {
  label: string;
  value: string;
  onChangeText: (value: string) => void;
  placeholder?: string;
  secureTextEntry?: boolean;
  keyboardType?: KeyboardTypeOptions;
  multiline?: boolean;
  autoCapitalize?: TextInputProps['autoCapitalize'];
  maxLength?: number;
  editable?: boolean;
  autoComplete?: TextInputProps['autoComplete'];
}) {
  return (
    <View style={styles.fieldWrap}>
      <Text style={styles.fieldLabel}>{label}</Text>
      <TextInput
        accessibilityLabel={label}
        autoCapitalize={autoCapitalize || 'sentences'}
        autoComplete={autoComplete}
        editable={editable}
        keyboardType={keyboardType}
        maxLength={maxLength}
        multiline={multiline}
        onChangeText={onChangeText}
        placeholder={placeholder}
        placeholderTextColor={colors.muted}
        secureTextEntry={secureTextEntry}
        style={[styles.input, multiline && styles.multilineInput, !editable && styles.inputDisabled]}
        value={value}
      />
    </View>
  );
}

export function Card({ children, style }: { children: ReactNode; style?: object }) {
  return <View style={[styles.card, style]}>{children}</View>;
}

export function Tag({ children, tone = 'default' }: { children: ReactNode; tone?: 'default' | 'positive' | 'warning' }) {
  const toneStyle = tone === 'positive'
    ? styles.tagPositive
    : tone === 'warning'
      ? styles.tagWarning
      : styles.tagDefault;
  return (
    <View style={[styles.tag, toneStyle]}>
      <Text style={styles.tagText}>{children}</Text>
    </View>
  );
}

export function Notice({ message, kind = 'error' }: { message: string; kind?: 'error' | 'success' }) {
  return (
    <View style={[styles.notice, kind === 'success' ? styles.noticeSuccess : styles.noticeError]}>
      <Text style={styles.noticeText}>{message}</Text>
    </View>
  );
}

export const uiStyles = StyleSheet.create({
  page: {
    flex: 1,
    backgroundColor: colors.canvas,
  },
  scrollContent: {
    padding: 20,
    paddingBottom: 42,
  },
  heading: {
    color: colors.ink,
    fontSize: 24,
    fontWeight: '800',
  },
  subheading: {
    color: colors.muted,
    fontSize: 14,
    lineHeight: 20,
    marginTop: 6,
  },
  sectionTitle: {
    color: colors.ink,
    fontSize: 18,
    fontWeight: '800',
    marginBottom: 12,
  },
  body: {
    color: colors.ink,
    fontSize: 15,
    lineHeight: 22,
  },
  small: {
    color: colors.muted,
    fontSize: 12,
    lineHeight: 17,
  },
  row: {
    alignItems: 'center',
    flexDirection: 'row',
    justifyContent: 'space-between',
  },
  gap: {
    height: 14,
  },
});

const styles = StyleSheet.create({
  button: {
    alignItems: 'center',
    borderRadius: 13,
    justifyContent: 'center',
    minHeight: 48,
    paddingHorizontal: 18,
    paddingVertical: 12,
  },
  buttonText: {
    fontSize: 15,
    fontWeight: '800',
  },
  buttonDisabled: {
    opacity: 0.48,
  },
  buttonPressed: {
    opacity: 0.83,
    transform: [{ scale: 0.99 }],
  },
  fieldWrap: {
    marginBottom: 14,
  },
  fieldLabel: {
    color: colors.ink,
    fontSize: 13,
    fontWeight: '700',
    marginBottom: 7,
  },
  input: {
    backgroundColor: colors.surface,
    borderColor: colors.border,
    borderRadius: 12,
    borderWidth: 1,
    color: colors.ink,
    fontSize: 15,
    minHeight: 48,
    paddingHorizontal: 13,
    paddingVertical: 11,
  },
  multilineInput: {
    minHeight: 100,
    textAlignVertical: 'top',
  },
  inputDisabled: {
    backgroundColor: colors.canvas,
  },
  card: {
    backgroundColor: colors.surface,
    borderColor: colors.border,
    borderRadius: 18,
    borderWidth: 1,
    padding: 16,
  },
  tag: {
    alignSelf: 'flex-start',
    borderRadius: 999,
    paddingHorizontal: 10,
    paddingVertical: 5,
  },
  tagDefault: {
    backgroundColor: colors.primarySoft,
  },
  tagPositive: {
    backgroundColor: colors.successSoft,
  },
  tagWarning: {
    backgroundColor: '#FFF3DE',
  },
  tagText: {
    color: colors.primaryDark,
    fontSize: 11,
    fontWeight: '800',
  },
  notice: {
    borderRadius: 12,
    marginBottom: 14,
    paddingHorizontal: 13,
    paddingVertical: 11,
  },
  noticeError: {
    backgroundColor: colors.dangerSoft,
  },
  noticeSuccess: {
    backgroundColor: colors.successSoft,
  },
  noticeText: {
    color: colors.danger,
    fontSize: 13,
    fontWeight: '600',
    lineHeight: 19,
  },
});

const buttonVariants = StyleSheet.create({
  primary: { backgroundColor: colors.primary },
  secondary: { backgroundColor: colors.primarySoft },
  danger: { backgroundColor: colors.dangerSoft },
  ghost: { backgroundColor: 'transparent' },
});

const buttonTextVariants = StyleSheet.create({
  primary: { color: colors.surface },
  secondary: { color: colors.primaryDark },
  danger: { color: colors.danger },
  ghost: { color: colors.primaryDark },
});
