"use client";

import { cn } from "@/lib/utils";
import type { PatternDto } from "@/lib/api";
import { formatRelativeTime, extractDomain, extractConcept } from "./constants";
import { Badge } from "@/components/ui/badge";
import { Users, Clock, Eye } from "lucide-react";

interface CorpusRowProps {
  pattern: PatternDto;
  className?: string;
}

export function CorpusRow({ pattern, className }: CorpusRowProps) {
  const domain = extractDomain(pattern.subject);
  const concept = extractConcept(pattern.subject);

  return (
    <div
      className={cn(
        "rounded-lg border border-border bg-card p-4 hover:border-border/80 transition-colors",
        className
      )}
    >
      {/* Header */}
      <div className="flex items-start justify-between gap-2 mb-3">
        <div className="min-w-0 flex-1">
          <div className="flex items-center gap-2 mb-1">
            <Badge variant="outline" className="text-xs font-mono">
              {domain}
            </Badge>
            <span className="text-xs text-muted-foreground truncate">
              {pattern.subject}
            </span>
          </div>
          <h3 className="text-base font-medium text-foreground">
            {concept}
            <span className="text-muted-foreground font-normal">
              {" "}.{pattern.predicate}
            </span>
          </h3>
        </div>
      </div>

      {/* Value */}
      <div className="mb-4">
        <code className="text-sm bg-muted px-2 py-1 rounded font-mono break-all">
          {pattern.value}
        </code>
      </div>

      {/* Stats */}
      <div className="flex flex-wrap items-center gap-4 text-xs text-muted-foreground">
        <div className="flex items-center gap-1">
          <Users className="h-3.5 w-3.5" />
          <span>{pattern.project_count} projects</span>
        </div>
        <div className="flex items-center gap-1">
          <Eye className="h-3.5 w-3.5" />
          <span>{pattern.observation_count} observations</span>
        </div>
        <div className="flex items-center gap-1 ml-auto">
          <Clock className="h-3.5 w-3.5" />
          <span>Last seen {formatRelativeTime(pattern.last_seen)}</span>
        </div>
      </div>
    </div>
  );
}
