"use client";

import Link from "next/link";
import { useLicense } from "@/lib/useLicense";

export function LicenseBanner() {
    const { license } = useLicense();
    if (!license) return null;

    if (license.state === "expired") {
        const when = license.expires_at ? new Date(license.expires_at).toLocaleDateString() : "recently";
        return (
            <div className="border-b border-warning/40 bg-warning/10 px-4 py-2 text-sm text-warning">
                Your Vantage licence expired on {when}. Your servers and monitors are still running,
                but changes are disabled until it is renewed.{" "}
                <Link href="/settings/license" className="underline">Add a licence</Link>
            </div>
        );
    }

    if (license.state === "invalid") {
        return (
            <div className="border-b border-danger/40 bg-danger/10 px-4 py-2 text-sm text-danger">
                This instance has no valid licence. Changes are disabled.{" "}
                <Link href="/settings/license" className="underline">Add a licence</Link>
            </div>
        );
    }

    if (typeof license.days_remaining === "number" && license.days_remaining <= 14) {
        return (
            <div className="border-b border-warning/25 bg-warning/[0.07] px-4 py-2 text-sm text-warning">
                Your licence expires in {license.days_remaining} day
                {license.days_remaining === 1 ? "" : "s"}.
            </div>
        );
    }

    return null;
}
