"use client";

import { useState } from "react";
import { Button, Modal } from "@/components/ui";
import type { VulnFinding } from "@/lib/api";

/*
 * Accepting a finding needs a reason and an expiry, and the API refuses without
 * both. The expiry is the point: a permanent dismissal is where risk goes to be
 * forgotten, and it is exactly what an auditor asks to see. This dialog says so
 * in as many words, because someone clicking it a year later needs to know the
 * finding will come back on its own.
 */

const DEFAULT_DAYS = 30;

function defaultUntil(): string {
    const d = new Date();
    d.setDate(d.getDate() + DEFAULT_DAYS);
    return d.toISOString().slice(0, 10);
}

interface Props {
    finding: VulnFinding;
    serverName: string;
    onClose: () => void;
    onAccept: (reason: string, untilISO: string) => void;
    pending?: boolean;
}

export function AcceptDialog({ finding, serverName, onClose, onAccept, pending }: Props) {
    const [reason, setReason] = useState("");
    const [until, setUntil] = useState(defaultUntil());

    const untilDate = new Date(`${until}T23:59:59`);
    const validUntil = !Number.isNaN(untilDate.getTime()) && untilDate.getTime() > Date.now();
    const canSubmit = reason.trim().length > 0 && validUntil && !pending;

    return (
        <Modal open onClose={onClose} title="Accept finding">
            <div className="space-y-4">
                <div className="rounded border border-border bg-well px-3 py-2 font-mono text-xs text-text-secondary">
                    {finding.cve_id} · {finding.package_name} on {serverName}
                    <br />
                    {finding.fixed_in ? `fixed in ${finding.fixed_in}` : "no fix published"}
                </div>

                <label className="block">
                    <span className="mb-1 block text-xs font-medium text-text-secondary">Reason</span>
                    <textarea
                        value={reason}
                        onChange={(e) => setReason(e.target.value)}
                        rows={3}
                        placeholder="Why this cannot be fixed now"
                        className="w-full rounded border border-border bg-surface px-3 py-2 text-sm text-text-primary placeholder:text-text-tertiary focus:border-accent focus:outline-none"
                    />
                </label>

                <label className="block">
                    <span className="mb-1 block text-xs font-medium text-text-secondary">Reopens on</span>
                    <input
                        type="date"
                        value={until}
                        onChange={(e) => setUntil(e.target.value)}
                        className="w-full rounded border border-border bg-surface px-3 py-2 text-sm text-text-primary focus:border-accent focus:outline-none"
                    />
                    {!validUntil && <span className="mt-1 block text-xs text-danger">Pick a future date.</span>}
                </label>

                <p className="text-xs text-text-tertiary">
                    The finding is hidden from counts and alerts until this date, then reopens automatically. Your name and reason are recorded in
                    the audit log.
                </p>

                <div className="flex justify-end gap-2">
                    <Button variant="secondary" onClick={onClose}>
                        Cancel
                    </Button>
                    <Button disabled={!canSubmit} onClick={() => onAccept(reason.trim(), untilDate.toISOString())}>
                        {pending ? "Accepting…" : "Accept"}
                    </Button>
                </div>
            </div>
        </Modal>
    );
}
