import { Printer, Download, Ban, Loader2 } from 'lucide-react';

const HERO_BTN = 'inline-flex h-9 items-center justify-center gap-1.5 rounded-lg border border-input bg-card px-4 text-xs font-bold transition-colors hover:border-primary hover:bg-accent/40 disabled:cursor-not-allowed disabled:opacity-60';
import { useLwrPdf } from '@/lib/pdf/useLwrPdf';

// LWR Print & Download actions for the detail hero action row (they sit beside Change Status
// / Cancel; no card of their own). Same h-9 outline shell as those two — they used to be bare
// icon+text, which read as captions rather than as things you can press.
//
// Faithful to legacy listlwrdetails.php / listlwrdetailsall.php: Print & Download
// are STATUS-GATED — legacy shows the live buttons only when the LWR status is NOT
// one of {1 Request, 2, 3 Approval PM, 5 Revise, 6, 9 Cancel}; otherwise it renders
// a disabled "Cannot Print" / "Cannot Download". At the Approval PM stage the LWR is
// still at Request (1), so the buttons are disabled there — matching legacy.
//
// Wired 2026-08-02 (was a "belum diintegrasikan" toast). Each click POSTs to
// `lwrs.print-action`, which writes the legacy labworkrequestassignment row at status 7
// (Comment "Print." / "Download") and returns the report payload; the PDF is then rendered
// in the browser by @react-pdf/renderer — see `lib/pdf/useLwrPdf.js`.
export function LwrPrintActionButtons({ printActions = null, lwrId = null }) {
    const { printable = false } = printActions || {};
    const { busy, print, download } = useLwrPdf(lwrId);

    if (printable && lwrId) {
        return (
            <>
                <button type="button" onClick={download} disabled={busy !== null}
                    title="Download LWR"
                    className={`${HERO_BTN} text-primary`}>
                    {busy === 'download'
                        ? <Loader2 aria-hidden="true" className="size-4 animate-spin" />
                        : <Download aria-hidden="true" className="size-3.5" />}
                    Download
                </button>
                <button type="button" onClick={print} disabled={busy !== null}
                    title="Print LWR"
                    className={`${HERO_BTN} text-foreground hover:text-primary`}>
                    {busy === 'print'
                        ? <Loader2 aria-hidden="true" className="size-4 animate-spin" />
                        : <Printer aria-hidden="true" className="size-3.5" />}
                    Print
                </button>
            </>
        );
    }

    return (
        <>
            <button type="button" disabled
                title="Tidak tersedia pada status LWR saat ini (legacy: Cannot Download)"
                className="flex cursor-not-allowed items-center gap-1.5 text-xs font-bold text-muted-foreground/60">
                <Ban aria-hidden="true" className="size-4" />
                Cannot Download
            </button>
            <button type="button" disabled
                title="Tidak tersedia pada status LWR saat ini (legacy: Cannot Print)"
                className="flex cursor-not-allowed items-center gap-1.5 text-xs font-bold text-muted-foreground/60">
                <Ban aria-hidden="true" className="size-4" />
                Cannot Print
            </button>
        </>
    );
}
