﻿"use client";

import { useState } from "react";
import Link from "next/link";
import { buttonVariants } from "@/components/ui/button";
import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card";
import { SearchIcon, ShieldCheckIcon } from "@/components/dashboard-icons";
import {
  formatPurchaseRequestMoney,
  getPendingPurchaseRequestRows,
  getPurchaseRequestApprovalRoleLabel,
  getPurchaseRequestApprovalStatusClass,
  getPurchaseRequestApprovalStatusLabel,
  getPurchaseRequestApprovalTrail,
} from "@/lib/purchase-request-data";
import { cn } from "@/lib/utils";
import { Pagination } from "@/components/ui/pagination";

function Badge({ label, className }: { label: string; className: string }) {
  return <span className={cn("inline-flex rounded-full px-2.5 py-1 text-xs font-semibold", className)}>{label}</span>;
}

export function PurchaseRequestApprovalQueue() {
  const pendingRows = getPendingPurchaseRequestRows();

  const [page, setPage] = useState(1);
  const pageSize = 5;
  const totalPages = Math.ceil(pendingRows.length / pageSize);
  const paginatedRows = pendingRows.slice((page - 1) * pageSize, page * pageSize);

  return (
    <div className="space-y-6">
      <div className="flex flex-col gap-4 lg:flex-row lg:items-end lg:justify-between">
        <div>
          <h2 className="text-2xl font-semibold tracking-tight md:text-3xl">PR Approval</h2>
        </div>

      </div>

      <Card className="overflow-hidden">
        <CardHeader className="p-0 md:p-0 px-8 md:px-8 pt-4 pb-1 md:pt-5 md:pb-1.5">
          <div className="flex flex-col gap-3 lg:flex-row lg:items-start lg:justify-between">
            <div>
              <CardTitle className="text-lg">Pending PR approvals</CardTitle>
            </div>

            <label className="relative flex items-center">
              <SearchIcon className="pointer-events-none absolute left-3 h-4 w-4 text-muted-foreground" />
              <input
                type="search"
                placeholder="Search..."
                className="h-9 w-64 rounded-2xl border border-border/80 bg-background pl-9 pr-4 text-sm outline-none transition placeholder:text-muted-foreground focus:border-primary/40 focus:ring-2 focus:ring-primary/10"
              />
            </label>
          </div>
        </CardHeader>

        <CardContent className="p-0">
          <div className="overflow-x-auto">
            <table className="min-w-full border-separate border-spacing-0 text-left text-sm">
              <thead className="sticky top-0 bg-card/95 text-xs uppercase tracking-[0.2em] text-muted-foreground backdrop-blur">
                <tr>
                  <th className="border-b border-border/70 px-5 py-2 font-semibold">PR</th>
                  <th className="border-b border-border/70 px-5 py-2 font-semibold">Project</th>
                  <th className="border-b border-border/70 px-5 py-2 font-semibold">Total requested</th>
                  <th className="border-b border-border/70 px-5 py-2 font-semibold">Approval trail</th>
                  <th className="border-b border-border/70 px-5 py-2 font-semibold">Action</th>
                </tr>
              </thead>
              <tbody>
                {pendingRows.length ? (
                  paginatedRows.map((row) => {
                    const approvals = getPurchaseRequestApprovalTrail(row);
                    const detailHref = `/purchase-requests/${row.id}`;

                    return (
                      <tr key={row.id} className="transition hover:bg-accent/60">
                        <td className="border-b border-border/70 px-5 py-4 align-top">
                          <div className="font-semibold text-foreground">{row.prNumber}</div>
                          <div className="mt-1 text-xs text-muted-foreground">{row.rapNumber}</div>
                        </td>
                        <td className="border-b border-border/70 px-5 py-4 align-top">
                          <div className="font-semibold text-foreground">{row.projectName}</div>
                          <div className="mt-1 text-xs text-muted-foreground">{row.customerName}</div>
                        </td>
                        <td className="border-b border-border/70 px-5 py-4 align-top font-medium">{formatPurchaseRequestMoney(row.totalValue)}</td>
                        <td className="border-b border-border/70 px-5 py-4 align-top">
                          <div className="flex flex-col gap-1.5">
                            {approvals.map((approval) => (
                              <Badge
                                key={approval.role}
                                label={`${getPurchaseRequestApprovalRoleLabel(approval.role)}: ${getPurchaseRequestApprovalStatusLabel(approval.status)}`}
                                className={getPurchaseRequestApprovalStatusClass(approval.status)}
                              />
                            ))}
                          </div>
                        </td>
                        <td className="border-b border-border/70 px-5 py-4 align-top">
                          <Link
                            href={detailHref}
                            className={buttonVariants({
                              variant: "outline",
                              size: "sm",
                              className:
                                "h-10 gap-2 border-emerald-300 bg-emerald-50/80 px-3 text-emerald-700 shadow-sm shadow-emerald-950/5 hover:border-emerald-400 hover:bg-emerald-100/90",
                            })}
                          >
                            <ShieldCheckIcon className="h-4 w-4 stroke-[2.25]" />
                            <span>Review</span>
                          </Link>
                        </td>
                      </tr>
                    );
                  })
                ) : (
                  <tr>
                    <td className="px-5 py-8 text-center text-sm text-muted-foreground" colSpan={5}>
                      Tidak ada PR yang menunggu approval.
                    </td>
                  </tr>
                )}
              </tbody>
            </table>
          </div>

          <Pagination currentPage={page} totalPages={totalPages} onPageChange={setPage} />
        </CardContent>
      </Card>


    </div>
  );
}
