import { View, Text, ScrollView, Alert } from "react-native";
import { useLocalSearchParams, Stack, useRouter } from "expo-router";
import { useQuery, useMutation } from "convex/react";
import { api } from "../../convex/_generated/api";
import { useAuth } from "../_layout";
import { useTheme } from "../../components/theme";
import { Badge } from "../../components/ui/badge";
import { Button } from "../../components/ui/button";
import { Card } from "../../components/ui/card";
import { Loading } from "../../components/ui/loading";
import { Avatar } from "../../components/ui/avatar";
import { getCategoryName } from "../../lib/constants";

export default function PostDetailScreen() {
  const { id } = useLocalSearchParams<{ id: string }>();
  const router = useRouter();
  const theme = useTheme();
  const { token, userId, isAuthenticated } = useAuth();

  const post = useQuery(api.posts.getById, { id: id as any });
  const currentUser = useQuery(
    api.users.getCurrentUser,
    isAuthenticated && token ? { token } : "skip"
  );

  const closePost = useMutation(api.posts.close);
  const getOrCreateChat = useMutation(api.chats.getOrCreate);

  if (!post) {
    return (
      <>
        <Stack.Screen options={{ title: "Побарување", headerShown: true, headerBackButtonDisplayMode: "minimal" }} />
        <View style={{ flex: 1, justifyContent: "center", alignItems: "center", backgroundColor: theme.colors.background }}>
          <Loading />
        </View>
      </>
    );
  }

  const customerId = post.customerId as string;
  const isOwner = isAuthenticated && userId && customerId === userId;
  const isHandyman = currentUser?.role === "handyman";
  const isOpen = post.status === "open";

  async function handleClose() {
    Alert.alert(
      "Затвори побарување",
      "Дали сте сигурни дека сакате да го затворите оваа побарување?",
      [
        { text: "Откажи", style: "cancel" },
        {
          text: "Затвори",
          style: "destructive",
          onPress: async () => {
            try {
              await closePost({ token: token!, postId: id as any });
            } catch (e: any) {
              Alert.alert("Грешка", e.message || "Неуспешно затворање");
            }
          },
        },
      ]
    );
  }

  async function handleRespond() {
    try {
      const chatId = await getOrCreateChat({ token: token!, partnerId: post!.customerId });
      router.push(`/chat/${chatId}` as any);
    } catch (e: any) {
      Alert.alert("Грешка", e.message || "Неуспешно поврзување");
    }
  }

  const categoryName = post.category ? getCategoryName(post.category) : null;
  const timeAgo = getTimeAgo(post.createdAt);

  return (
    <>
      <Stack.Screen options={{ title: "Побарување", headerShown: true, headerBackButtonDisplayMode: "minimal" }} />
      <ScrollView contentInsetAdjustmentBehavior="automatic" contentContainerStyle={{ padding: theme.spacing.lg, gap: theme.spacing.md, paddingBottom: 100 }}>
        <View style={{ flexDirection: "row", justifyContent: "space-between", alignItems: "flex-start" }}>
          <View style={{ flex: 1 }}>
            <Text style={{ fontSize: 22, fontWeight: "700", color: theme.colors.text }}>
              {post.title}
            </Text>
          </View>
          <Badge label={isOpen ? "Отворено" : "Затворено"} variant={isOpen ? "success" : "error"} />
        </View>

        <Card>
          <View style={{ gap: theme.spacing.sm }}>
            {categoryName && (
              <View style={{ flexDirection: "row", alignItems: "center", gap: 8 }}>
                <Text style={{ fontSize: 14, color: theme.colors.textSecondary }}>Категорија:</Text>
                <Badge label={categoryName} variant="primary" />
              </View>
            )}
            {post.location && (
              <View style={{ flexDirection: "row", alignItems: "center", gap: 8 }}>
                <Text style={{ fontSize: 14, color: theme.colors.textSecondary }}>📍 Локација:</Text>
                <Text style={{ fontSize: 14, color: theme.colors.text }}>{post.location}</Text>
              </View>
            )}
            {post.budget && (
              <View style={{ flexDirection: "row", alignItems: "center", gap: 8 }}>
                <Text style={{ fontSize: 14, color: theme.colors.textSecondary }}>💰 Буџет:</Text>
                <Text style={{ fontSize: 14, fontWeight: "600", color: theme.colors.primary }}>{post.budget}</Text>
              </View>
            )}
            <View style={{ flexDirection: "row", alignItems: "center", gap: 8 }}>
              <Text style={{ fontSize: 14, color: theme.colors.textSecondary }}>🗓 Објавено:</Text>
              <Text style={{ fontSize: 14, color: theme.colors.text }}>{timeAgo}</Text>
            </View>
          </View>
        </Card>

        <Card>
          <Text style={{ fontSize: 16, color: theme.colors.text, lineHeight: 24 }}>
            {post.description}
          </Text>
        </Card>
      </ScrollView>

      {isOwner && isOpen && (
        <View style={{ position: "absolute", bottom: 0, left: 0, right: 0, padding: theme.spacing.lg, backgroundColor: theme.colors.background }}>
          <Button variant="destructive" onPress={handleClose}>
            Затвори оглас
          </Button>
        </View>
      )}

      {!isOwner && isHandyman && isOpen && isAuthenticated && (
        <View style={{ position: "absolute", bottom: 0, left: 0, right: 0, padding: theme.spacing.lg, backgroundColor: theme.colors.background }}>
          <Button onPress={handleRespond}>
            Одговори
          </Button>
        </View>
      )}
    </>
  );
}

function getTimeAgo(timestamp: number): string {
  const seconds = Math.floor((Date.now() - timestamp) / 1000);
  if (seconds < 60) return "Пред неколку секунди";
  const minutes = Math.floor(seconds / 60);
  if (minutes < 60) return `Пред ${minutes} мин`;
  const hours = Math.floor(minutes / 60);
  if (hours < 24) return `Пред ${hours} ч`;
  const days = Math.floor(hours / 24);
  if (days < 30) return `Пред ${days} ден`;
  const months = Math.floor(days / 30);
  return `Пред ${months} месеци`;
}