import { View, Text, Pressable } from "react-native";
import { Link } from "expo-router";
import { useTheme } from "./theme";
import { Badge } from "./ui/badge";
import { getCategoryName } from "../lib/constants";

interface PostCardProps {
  post: {
    _id: string;
    title: string;
    description: string;
    category?: string;
    location?: string;
    budget?: string;
    status: string;
    createdAt: number;
  };
}

export function PostCard({ post }: PostCardProps) {
  const theme = useTheme();
  const categoryName = post.category ? getCategoryName(post.category) : null;
  const isOpen = post.status === "open";

  return (
    <Link href={`/post/${post._id}` as any} asChild>
      <Pressable
        style={{
          backgroundColor: theme.colors.card,
          borderRadius: theme.radius.lg,
          padding: theme.spacing.md,
          borderWidth: 1,
          borderColor: theme.colors.border,
          gap: theme.spacing.sm,
        }}
      >
        <View style={{ flexDirection: "row", justifyContent: "space-between", alignItems: "flex-start" }}>
          <Text style={{ fontSize: 16, fontWeight: "600", color: theme.colors.text, flex: 1 }} numberOfLines={2}>
            {post.title}
          </Text>
          <Badge label={isOpen ? "Отворено" : "Затворено"} variant={isOpen ? "success" : "error"} />
        </View>

        <Text style={{ fontSize: 14, color: theme.colors.textSecondary }} numberOfLines={2}>
          {post.description}
        </Text>

        <View style={{ flexDirection: "row", alignItems: "center", gap: 8, flexWrap: "wrap" }}>
          {categoryName && <Badge label={categoryName} variant="primary" />}
          {post.location && (
            <Text style={{ fontSize: 13, color: theme.colors.textTertiary }}>📍 {post.location}</Text>
          )}
          {post.budget && (
            <Text style={{ fontSize: 13, color: theme.colors.primary, fontWeight: "600" }}>
              💰 {post.budget}
            </Text>
          )}
        </View>
      </Pressable>
    </Link>
  );
}