import Link from "next/link";
import { Badge } from "@/components/ui/badge";
import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card";
import { formatDate } from "@/lib/utils";

interface BlogSidebarProps {
  recentPosts: Array<{
    _id: string;
    title: string;
    slug: string;
    publishedAt: number;
    excerpt?: string;
  }>;
  allTags: string[];
}

export function BlogSidebar({ recentPosts, allTags }: BlogSidebarProps) {
  return (
    <div className="space-y-6">
      {/* Recent Posts */}
      <Card>
        <CardHeader>
          <CardTitle className="text-lg">Recent Posts</CardTitle>
        </CardHeader>
        <CardContent className="space-y-4">
          {recentPosts.map((post) => (
            <div key={post._id} className="space-y-1">
              <Link
                href={`/labs/${post.slug}`}
                className="font-medium hover:text-primary line-clamp-2 text-sm"
              >
                {post.title}
              </Link>
              <p className="text-xs text-muted-foreground">
                {formatDate(post.publishedAt)}
              </p>
            </div>
          ))}
        </CardContent>
      </Card>

      {/* Tags */}
      <Card>
        <CardHeader>
          <CardTitle className="text-lg">Tags</CardTitle>
        </CardHeader>
        <CardContent>
          <div className="flex flex-wrap gap-2">
            {allTags.map((tag) => (
              <Link key={tag} href={`/labs/tag/${encodeURIComponent(tag)}`}>
                <Badge variant="outline" className="hover:bg-secondary/80">
                  {tag}
                </Badge>
              </Link>
            ))}
          </div>
        </CardContent>
      </Card>
    </div>
  );
}