import { View, Text, ScrollView, Pressable, 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 { Rating } from "../../components/ui/rating";
import { Button } from "../../components/ui/button";
import { Avatar } from "../../components/ui/avatar";
import { Loading } from "../../components/ui/loading";
import { Card } from "../../components/ui/card";
import { getCategoryName, CATEGORY_EMOJI } from "../../lib/constants";
export default function AdDetailScreen() {
const { id } = useLocalSearchParams<{ id: string }>();
const theme = useTheme();
const router = useRouter();
const { token, isAuthenticated, userId } = useAuth();
const ad = useQuery(api.ads.getById, id ? { id: id as any } : "skip");
const handyman = useQuery(
api.users.getById,
ad ? { id: ad.handymanId } : "skip"
);
const deleteAd = useMutation(api.ads.remove);
if (!ad) {
return (
<>
>
);
}
const isOwner = userId === ad.handymanId;
const categoryName = getCategoryName(ad.category);
const categoryEmoji = CATEGORY_EMOJI[ad.category] || "📋";
function handleDelete() {
Alert.alert(
"Избриши оглас",
"Дали сте сигурни дека сакате да го избришете овој оглас?",
[
{ text: "Откажи", style: "cancel" },
{
text: "Избриши",
style: "destructive",
onPress: async () => {
try {
if (!ad) return;
await deleteAd({ token: token!, adId: ad._id as any });
router.back();
} catch (e: any) {
Alert.alert("Грешка", e.message || "Неуспешно бришење");
}
},
},
]
);
}
return (
<>
20 ? ad.title.slice(0, 20) + "…" : ad.title,
headerShown: true,
headerBackButtonDisplayMode: "minimal",
}}
/>
{/* Gallery placeholder */}
{categoryEmoji}
Нема слики
{/* Title + Price */}
{ad.title}
{ad.priceRange && (
{ad.priceRange}
)}
{/* Category + Location */}
📍 {ad.location}
{/* Rating */}
{ad.ratingAvg != null && ad.ratingAvg > 0 && (
)}
{/* Availability */}
{ad.availability && (
Расположивост
{ad.availability}
)}
{/* Description */}
Опис
{ad.description}
{/* Handyman info */}
{
if (handyman) router.push(`/(tabs)/(profile)`);
}}
style={{ flexDirection: "row", alignItems: "center", gap: theme.spacing.md }}
>
{handyman?.name || "Мајстор"}
Мајстор
{/* Action buttons */}
{!isOwner && isAuthenticated && (
)}
{/* Owner actions */}
{isOwner && (
)}
{!isOwner && !isAuthenticated && (
Најавете се за да започнете разговор или да напишете оценка.
)}
>
);
}