"use client";

import { useState, useEffect, useMemo } from "react";
import { usePoll } from "@/lib/use-poll";
import { fetchAPI } from "@/lib/api";
import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card";
import { ScrollArea } from "@/components/ui/scroll-area";
import { cn } from "@/lib/utils";

interface LogFile {
  name: string;
  filename?: string;
  size?: string;
  size_bytes?: number;
  modified?: number;
}

export default function LogsPage() {
  const { data: logsRaw } = usePoll<LogFile[] | { files: LogFile[] }>("/api/logs", 60000);
  const logFiles = Array.isArray(logsRaw) ? logsRaw : (logsRaw?.files ?? []);

  const [selected, setSelected] = useState<string | null>(null);
  const [content, setContent] = useState<string>("");
  const [loadingContent, setLoadingContent] = useState(false);
  const [search, setSearch] = useState("");

  useEffect(() => {
    if (!selected) {
      setContent("");
      return;
    }
    let cancelled = false;
    setLoadingContent(true);
    fetchAPI<{ lines?: string[]; content?: string } | string>(`/api/logs/${encodeURIComponent(selected)}?tail=200`)
      .then((data) => {
        if (cancelled) return;
        if (typeof data === "string") setContent(data);
        else if (Array.isArray((data as Record<string,unknown>).lines)) setContent(((data as Record<string,unknown>).lines as string[]).join("\n"));
        else if ((data as Record<string,unknown>).content) setContent(String((data as Record<string,unknown>).content));
        else setContent(JSON.stringify(data, null, 2));
      })
      .catch((err) => {
        if (cancelled) return;
        setContent(`Error loading log: ${err}`);
      })
      .finally(() => {
        if (!cancelled) setLoadingContent(false);
      });
    return () => { cancelled = true; };
  }, [selected]);

  const filteredLines = useMemo(() => {
    if (!content) return [];
    const lines = content.split("\n");
    if (!search.trim()) return lines;
    const lower = search.toLowerCase();
    return lines.filter(line => line.toLowerCase().includes(lower));
  }, [content, search]);

  return (
    <div className="space-y-6">
      <h1 className="text-lg font-semibold">Logs</h1>

      <div className="grid grid-cols-1 lg:grid-cols-4 gap-4" style={{ minHeight: "500px" }}>
        {/* Left sidebar: log file list */}
        <Card className="lg:col-span-1">
          <CardHeader className="pb-2">
            <CardTitle className="text-sm font-medium">Log Files</CardTitle>
          </CardHeader>
          <CardContent>
            <ScrollArea className="h-[460px]">
              {logFiles.length === 0 ? (
                <p className="text-xs text-muted-foreground">Loading...</p>
              ) : (
                <div className="space-y-1">
                  {logFiles.map((file) => (
                    <button
                      key={file.name}
                      onClick={() => setSelected(file.name)}
                      className={cn(
                        "w-full text-left rounded-md px-2 py-1.5 text-xs transition-colors",
                        selected === file.name
                          ? "bg-primary/10 text-foreground"
                          : "text-muted-foreground hover:bg-secondary hover:text-foreground"
                      )}
                    >
                      <p className="font-medium truncate">{file.name}</p>
                      {(file.size || file.size_bytes != null) && (
                        <p className="text-[10px] text-muted-foreground">
                          {file.size ?? (file.size_bytes != null ? `${(file.size_bytes / 1024).toFixed(0)} KB` : "")}
                        </p>
                      )}
                    </button>
                  ))}
                </div>
              )}
            </ScrollArea>
          </CardContent>
        </Card>

        {/* Right: log content viewer */}
        <Card className="lg:col-span-3">
          <CardHeader className="pb-2 flex flex-row items-center justify-between gap-4">
            <CardTitle className="text-sm font-medium">
              {selected ?? "Select a log file"}
            </CardTitle>
            {selected && (
              <input
                value={search}
                onChange={(e) => setSearch(e.target.value)}
                placeholder="Filter lines..."
                className="rounded-md border border-border bg-background px-2 py-1 text-xs focus:outline-none focus:ring-1 focus:ring-primary w-48"
              />
            )}
          </CardHeader>
          <CardContent>
            <ScrollArea className="h-[460px]">
              {!selected ? (
                <p className="text-xs text-muted-foreground py-4 text-center">
                  Select a log file from the sidebar
                </p>
              ) : loadingContent ? (
                <p className="text-xs text-muted-foreground py-4 text-center">
                  Loading...
                </p>
              ) : (
                <pre className="text-[11px] font-mono text-foreground whitespace-pre-wrap break-all leading-relaxed">
                  {filteredLines.join("\n") || "No matching lines"}
                </pre>
              )}
            </ScrollArea>
          </CardContent>
        </Card>
      </div>
    </div>
  );
}
