"use client";

import { useEffect, useState, useCallback } from "react";
import { Card, CardContent } from "@/components/ui/card";
import { Button } from "@/components/ui/button";
import { Input } from "@/components/ui/input";
import { Badge } from "@/components/ui/badge";
import {
  Table,
  TableBody,
  TableCell,
  TableHead,
  TableHeader,
  TableRow,
} from "@/components/ui/table";
import { Search, Plus, Eye, Pencil, Trash2 } from "lucide-react";
import Link from "next/link";
import { getArticlePagination, deleteArticle } from "@/service/article";
import { formatDate } from "@/utils/global";
import { close, loading } from "@/config/swal";
import Cookies from "js-cookie";
import Swal from "sweetalert2";
import type { ArticleContentKind } from "@/constants/article-content-types";
import { ARTICLE_KIND_LABEL, articleListPath } from "@/constants/article-content-types";

type Props = {
  kind: ArticleContentKind;
  typeId: number;
};

export default function NewsArticleList({ kind, typeId }: Props) {
  const [articles, setArticles] = useState<any[]>([]);
  const [page, setPage] = useState(1);
  const [totalPage, setTotalPage] = useState(1);
  const [search, setSearch] = useState("");
  const [debouncedSearch, setDebouncedSearch] = useState("");
  const [levelId, setLevelId] = useState<string | undefined>();

  useEffect(() => {
    const ulne = Cookies.get("ulne");
    setLevelId(ulne);
  }, []);

  useEffect(() => {
    const t = setTimeout(() => setDebouncedSearch(search), 350);
    return () => clearTimeout(t);
  }, [search]);

  const fetchData = useCallback(async () => {
    loading();
    try {
      const req = {
        limit: "10",
        page,
        title: debouncedSearch,
        source: "",
        categoryId: null,
        search: "",
        sort: "desc",
        sortBy: "created_at",
        typeId,
      };
      const res = await getArticlePagination(req as any);
      const payload = (res as any)?.data;
      const data = payload?.data ?? [];
      setArticles(Array.isArray(data) ? data : []);
      setTotalPage(payload?.meta?.totalPage ?? 1);
    } finally {
      close();
    }
  }, [page, debouncedSearch, typeId]);

  useEffect(() => {
    fetchData();
  }, [fetchData]);

  const getStatus = (article: any) => {
    if (article.isDraft) return "Draft";
    if (article.publishStatus?.toLowerCase() === "cancel") return "Pending";
    if (article.isPublish) return "Published";
    return "Pending";
  };

  const statusClass = (status: string) => {
    const v = status.toLowerCase();
    if (v === "published") return "bg-green-100 text-green-700";
    if (v === "pending") return "bg-yellow-100 text-yellow-700";
    if (v === "draft") return "bg-gray-200 text-gray-600";
    return "bg-gray-200 text-gray-600";
  };

  async function handleDelete(id: number) {
    const ok = await Swal.fire({
      icon: "warning",
      title: "Delete this article?",
      showCancelButton: true,
    });
    if (!ok.isConfirmed) return;
    loading();
    try {
      const res = await deleteArticle(String(id));
      if ((res as any)?.error) {
        await Swal.fire({
          icon: "error",
          title: "Delete failed",
          text: String((res as any)?.message ?? ""),
        });
        return;
      }
      await fetchData();
      await Swal.fire({ icon: "success", title: "Deleted", timer: 1200, showConfirmButton: false });
    } finally {
      close();
    }
  }

  const label = ARTICLE_KIND_LABEL[kind];
  const basePath = articleListPath(kind);

  return (
    <div className="space-y-6">
      <div className="flex items-start justify-between gap-4">
        <div>
          <h1 className="text-2xl font-semibold text-slate-800">
            News & Articles — {label}
          </h1>
          <p className="text-sm text-slate-500 mt-1">
            Create and manage {label.toLowerCase()} articles. Organize with tags only.
          </p>
        </div>
        {levelId === "3" && (
          <Link href={`${basePath}/create`}>
            <Button className="bg-blue-600 hover:bg-blue-700 rounded-lg">
              <Plus className="w-4 h-4 mr-2" />
              New {label} article
            </Button>
          </Link>
        )}
      </div>

      <div className="flex gap-3">
        <div className="relative flex-1">
          <Search className="absolute left-3 top-3 w-4 h-4 text-slate-400" />
          <Input
            placeholder="Search by title..."
            className="pl-9"
            value={search}
            onChange={(e) => {
              setSearch(e.target.value);
              setPage(1);
            }}
          />
        </div>
      </div>

      <Card className="rounded-2xl border shadow-sm">
        <CardContent className="p-0">
          <Table>
            <TableHeader>
              <TableRow>
                <TableHead className="pl-3">Article</TableHead>
                <TableHead>Tags</TableHead>
                <TableHead>Status</TableHead>
                <TableHead>Date</TableHead>
                <TableHead className="text-right">Actions</TableHead>
              </TableRow>
            </TableHeader>
            <TableBody>
              {articles.length > 0 ? (
                articles.map((article) => (
                  <TableRow key={article.id}>
                    <TableCell className="font-medium max-w-xs pl-3">
                      {article.title}
                    </TableCell>
                    <TableCell>
                      <span className="text-sm text-slate-600 line-clamp-2">
                        {article.tags || "—"}
                      </span>
                    </TableCell>
                    <TableCell>
                      {(() => {
                        const status = getStatus(article);
                        return (
                          <span
                            className={`px-3 py-1 text-xs rounded-full font-medium ${statusClass(
                              status,
                            )}`}
                          >
                            {status}
                          </span>
                        );
                      })()}
                    </TableCell>
                    <TableCell>{formatDate(article.createdAt)}</TableCell>
                    <TableCell className="text-right space-x-1">
                      <Link href={`/admin/news-article/detail/${article.id}`}>
                        <Button size="icon" variant="ghost" type="button">
                          <Eye className="w-4 h-4" />
                        </Button>
                      </Link>
                      <Link href={`/admin/news-article/detail/${article.id}`}>
                        <Button size="icon" variant="ghost" type="button">
                          <Pencil className="w-4 h-4" />
                        </Button>
                      </Link>
                      {levelId === "3" && (
                        <Button
                          size="icon"
                          variant="ghost"
                          type="button"
                          onClick={() => handleDelete(article.id)}
                        >
                          <Trash2 className="w-4 h-4 text-red-500" />
                        </Button>
                      )}
                    </TableCell>
                  </TableRow>
                ))
              ) : (
                <TableRow>
                  <TableCell colSpan={5} className="text-center py-8 text-slate-500">
                    No articles yet. Create one to get started.
                  </TableCell>
                </TableRow>
              )}
            </TableBody>
          </Table>

          <div className="flex items-center justify-between p-4 border-t text-sm text-slate-500">
            <p>
              Page {page} of {totalPage}
            </p>
            <div className="flex gap-2">
              <Button
                variant="outline"
                size="sm"
                disabled={page === 1}
                onClick={() => setPage((p) => Math.max(1, p - 1))}
              >
                Previous
              </Button>
              <Button size="sm" className="bg-blue-600">
                {page}
              </Button>
              <Button
                variant="outline"
                size="sm"
                disabled={page === totalPage}
                onClick={() => setPage((p) => p + 1)}
              >
                Next
              </Button>
            </div>
          </div>
        </CardContent>
      </Card>
    </div>
  );
}
