import { useQuery } from '@tanstack/react-query';
import { Link } from '@tanstack/react-router';
import { fetchPinnedLiveBlogs } from '@/lib/api';
import { Button } from '@/components/ui/button';
import { Calendar, Eye, MessageSquare, Pin } from 'lucide-react';

export function PinnedLiveBlogsSidebar() {
  const { data: liveBlogs, isLoading, error } = useQuery({
    queryKey: ['pinned-live-blogs'],
    queryFn: fetchPinnedLiveBlogs,
  });

  const getStatusBadge = (status: string) => {
    switch (status) {
      case 'live':
        return (
          <span className="px-2 py-0.5 bg-accent text-foreground text-xs font-body font-bold uppercase">
            LIVE
          </span>
        );
      case 'ended':
        return (
          <span className="px-2 py-0.5 border-2 border-foreground/40 text-foreground/40 text-xs font-body font-bold uppercase">
            ENDED
          </span>
        );
      case 'archived':
        return (
          <span className="px-2 py-0.5 border-2 border-foreground/30 text-foreground/30 text-xs font-body font-bold uppercase">
            ARCHIVED
          </span>
        );
      default:
        return (
          <span className="px-2 py-0.5 border-2 border-foreground text-foreground text-xs font-body font-bold uppercase">
            DRAFT
          </span>
        );
    }
  };

  const formatDate = (dateString: string) => {
    const date = new Date(dateString);
    return date.toLocaleDateString('mk-MK', {
      day: 'numeric',
      month: 'short',
    });
  };

  if (isLoading) {
    return (
      <div className="border-brutal-sm bg-card p-6">
        <div className="flex items-center gap-2 mb-6">
          <Pin className="h-5 w-5" />
          <h3 className="text-xl font-display">Pinned Live</h3>
        </div>
        <div className="space-y-4">
          {[1, 2, 3].map((i) => (
            <div key={i} className="animate-pulse">
              <div className="h-4 bg-muted rounded w-3/4 mb-2"></div>
              <div className="h-3 bg-muted rounded w-1/2"></div>
            </div>
          ))}
        </div>
      </div>
    );
  }

  if (error) {
    return (
      <div className="border-brutal-sm bg-card p-6">
        <div className="flex items-center gap-2 mb-6">
          <Pin className="h-5 w-5" />
          <h3 className="text-xl font-display">Pinned Live</h3>
        </div>
        <div className="text-center py-4">
          <div className="text-destructive font-body text-sm mb-2">ERROR</div>
          <Button variant="brutal" size="sm" onClick={() => window.location.reload()}>
            Retry
          </Button>
        </div>
      </div>
    );
  }

  if (!liveBlogs || liveBlogs.length === 0) {
    return (
      <div className="border-brutal-sm bg-card p-6">
        <div className="flex items-center gap-2 mb-6">
          <Pin className="h-5 w-5" />
          <h3 className="text-xl font-display">Pinned Live</h3>
        </div>
        <div className="text-center py-6 border-2 border-dashed border-foreground/20 p-4">
          <div className="font-body text-muted-foreground mb-2">No pinned live blogs</div>
          <p className="font-body text-xs text-muted-foreground">
            Pin live blogs from the admin panel.
          </p>
        </div>
      </div>
    );
  }

  return (
    <div className="border-brutal-sm bg-card p-6">
      <div className="flex items-center justify-between mb-6 pb-4 border-b-2 border-foreground/10">
        <div className="flex items-center gap-2">
          <Pin className="h-5 w-5 text-accent" />
          <h3 className="text-xl font-display">Pinned Live</h3>
        </div>
        <span className="px-2 py-1 border-2 border-foreground bg-foreground text-background text-xs font-body font-bold uppercase">
          {liveBlogs.length}
        </span>
      </div>

      <div className="space-y-4">
        {liveBlogs.map((liveBlog) => (
          <Link
            key={liveBlog.id}
            to="/live-blogs/$slug"
            params={{ slug: liveBlog.slug }}
            className="block group"
          >
            <div className="p-4 border-2 border-foreground/10 hover:border-foreground hover:shadow-brutal-sm transition-all duration-150 group-hover:-translate-y-1">
              <div className="flex items-start justify-between mb-2">
                <h4 className="font-body text-sm font-bold leading-tight line-clamp-2 group-hover:text-accent transition-colors">
                  {liveBlog.title}
                </h4>
                {getStatusBadge(liveBlog.status)}
              </div>

              {liveBlog.description && (
                <p className="text-xs font-body text-muted-foreground mb-3 line-clamp-2">
                  {liveBlog.description}
                </p>
              )}

              <div className="flex flex-wrap items-center gap-3 text-xs font-body text-muted-foreground">
                <div className="flex items-center gap-1">
                  <Calendar className="h-3 w-3" />
                  <span>{formatDate(liveBlog.createdAt)}</span>
                </div>

                <div className="flex items-center gap-1">
                  <Eye className="h-3 w-3" />
                  <span>{liveBlog.viewCount}</span>
                </div>

                {liveBlog.updates && liveBlog.updates.length > 0 && (
                  <div className="flex items-center gap-1">
                    <MessageSquare className="h-3 w-3" />
                    <span>{liveBlog.updates.length}</span>
                  </div>
                )}

                {liveBlog.author && (
                  <div className="text-xs text-muted-foreground">
                    {liveBlog.author.name}
                  </div>
                )}
              </div>

              {liveBlog.featuredImage && (
                <div className="mt-3">
                  <div className="relative h-20 border-2 border-foreground/10 overflow-hidden">
                    <img
                      src={liveBlog.featuredImage}
                      alt={liveBlog.title}
                      className="w-full h-full object-cover transition-transform duration-300 group-hover:scale-110"
                    />
                  </div>
                </div>
              )}
            </div>
          </Link>
        ))}
      </div>

      <div className="mt-6 pt-4 border-t-2 border-foreground/10">
        <Link to="/live-blogs" className="block">
          <Button variant="brutal" className="w-full justify-center">
            Сите Live
          </Button>
        </Link>
      </div>
    </div>
  );
}
