import { useState, useRef } from 'react';
import {
  View,
  Text,
  TextInput,
  Pressable,
  TouchableOpacity,
  ActivityIndicator,
  Alert,
} from 'react-native';
import { Image } from 'expo-image';
import { Ionicons } from '@expo/vector-icons';
import { useQueryClient } from '@tanstack/react-query';
import { useTranslation } from 'react-i18next';
// TODO(sdk54): migrate to new expo-file-system class-based API (File/Directory/Paths) — see Task #14
import * as FileSystem from 'expo-file-system/legacy';
import * as ImagePicker from 'expo-image-picker';
import { apiFetch } from '../lib/api';
import { resolveAvatar } from '../lib/resolveAvatar';
import { useMe } from '../hooks/useMe';
import { useColors } from '../lib/theme';

type Props = {
  onPosted?: () => void;
};

export function ComposeCard({ onPosted }: Props) {
  const { t } = useTranslation();
  const colors = useColors();
  const { me } = useMe();
  const queryClient = useQueryClient();
  const inputRef = useRef<TextInput>(null);
  const [focused, setFocused] = useState(false);
  const [content, setContent] = useState('');
  const [imageUri, setImageUri] = useState<string | null>(null);
  const [posting, setPosting] = useState(false);

  const nickname = me?.nickname ?? t('community.compose_default_user');
  const avatarUrl = resolveAvatar(me?.avatar ?? null, nickname);

  const cancel = () => {
    setContent('');
    setImageUri(null);
    setFocused(false);
    inputRef.current?.blur();
  };

  const pickImage = async () => {
    const perm = await ImagePicker.requestMediaLibraryPermissionsAsync();
    if (!perm.granted) {
      Alert.alert(t('community.compose_photo_perm_title'), t('community.compose_photo_perm_desc'));
      return;
    }
    const result = await ImagePicker.launchImageLibraryAsync({
      mediaTypes: ImagePicker.MediaTypeOptions.Images,
      allowsEditing: false,
      quality: 0.8,
    });
    if (!result.canceled && result.assets[0]?.uri) {
      setImageUri(result.assets[0].uri);
    }
  };

  const submit = async () => {
    if (!content.trim() && !imageUri) return;
    setPosting(true);
    try {
      let uploadedImageUrl: string | undefined;

      if (imageUri) {
        const base64 = await FileSystem.readAsStringAsync(imageUri, {
          encoding: FileSystem.EncodingType.Base64,
        });
        const upload = await apiFetch<{ url: string }>('/api/community/upload-image', {
          method: 'POST',
          body: {
            image: `data:image/jpeg;base64,${base64}`,
            mimeType: 'image/jpeg',
          },
        });
        uploadedImageUrl = upload?.url;
      }

      await apiFetch('/api/community/post', {
        method: 'POST',
        body: {
          category: 'story',
          content: content.trim(),
          ...(uploadedImageUrl ? { imageUrl: uploadedImageUrl } : {}),
        },
      });
      cancel();
      queryClient.invalidateQueries({ queryKey: ['community-posts'] });
      onPosted?.();
    } catch (err: any) {
      Alert.alert(t('common.error'), err?.message ?? t('community.post_failed'));
    } finally {
      setPosting(false);
    }
  };

  const showActions = focused || content.length > 0;

  return (
    <View style={{ backgroundColor: colors.bg, borderWidth: 1, borderColor: colors.border, borderRadius: 16, padding: 16, marginBottom: 16 }}>
      <View className="flex-row items-start gap-3">
        <Image
          source={{ uri: avatarUrl }}
          className="w-8 h-8 rounded-full bg-neutral-100 shrink-0 mt-0.5"
        />
        <View className="flex-1 min-w-0">
          <TextInput
            ref={inputRef}
            value={content}
            onChangeText={setContent}
            onFocus={() => setFocused(true)}
            placeholder={t('community.compose_placeholder')}
            placeholderTextColor={colors.textMuted}
            multiline
            className="text-sm leading-5 min-h-[40px]"
            style={{ textAlignVertical: 'top', fontFamily: 'Nunito_400Regular', color: colors.text }}
          />
          {imageUri && (
            <View className="relative mt-2">
              <Image
                source={{ uri: imageUri }}
                className="w-full rounded-xl"
                style={{ height: 160 }}
                contentFit="cover"
              />
              <Pressable
                onPress={() => setImageUri(null)}
                hitSlop={{ top: 9, bottom: 9, left: 9, right: 9 }}
                android_ripple={{ color: 'rgba(255,255,255,0.18)', borderless: true, radius: 22 }}
                className="absolute top-2 right-2 w-7 h-7 rounded-full bg-black/50 items-center justify-center"
              >
                <Ionicons name="close" size={14} color="#fff" />
              </Pressable>
            </View>
          )}
        </View>
      </View>

      {showActions && (
        <View className="flex-row items-center justify-between mt-3 pt-3 border-t border-neutral-100">
          <Pressable
            onPress={pickImage}
            android_ripple={{ color: 'rgba(0,0,0,0.08)', borderless: true, radius: 22 }}
            className="flex-row items-center gap-1.5 px-2"
            style={{ height: 44 }}
          >
            <Ionicons name="image-outline" size={22} color="#737373" />
            <Text className="text-sm text-neutral-500" style={{ fontFamily: 'Nunito_400Regular' }}>{t('community.image')}</Text>
          </Pressable>

          <View className="flex-row items-center gap-2">
            <TouchableOpacity
              onPress={cancel}
              hitSlop={{ top: 12, bottom: 12, left: 12, right: 12 }}
              activeOpacity={0.5}
            >
              <Text className="text-sm text-neutral-400" style={{ fontFamily: 'Nunito_600SemiBold' }}>{t('common.cancel')}</Text>
            </TouchableOpacity>
            <TouchableOpacity
              onPress={() => { if (!content.trim() || posting) return; submit(); }}
              disabled={!content.trim() || posting}
              activeOpacity={0.5}
              className="bg-rebreak-500 items-center justify-center rounded-full px-5 h-8"
              style={{ opacity: !content.trim() || posting ? 0.5 : 1 }}
            >
              {posting ? (
                <ActivityIndicator size="small" color="#fff" />
              ) : (
                <Text className="text-white text-sm" style={{ fontFamily: 'Nunito_600SemiBold' }}>{t('community.share')}</Text>
              )}
            </TouchableOpacity>
          </View>
        </View>
      )}
    </View>
  );
}
