import HeadingSmall from '@/components/heading-small';
import { Badge } from '@/components/ui/badge';
import AppLayout from '@/layouts/app-layout';
import { dateTime, money } from '@/lib/format';
import { cn } from '@/lib/utils';
import { Head } from '@inertiajs/react';
import { Check } from 'lucide-react';

interface Props {
    tenant: { name: string; status: string; trial_ends_at: string | null };
    subscription: {
        status: string;
        seats: number;
        plan: { slug: string; name: string; price_cents: number };
        current_period_end: string | null;
    } | null;
    plans: { slug: string; name: string; price_cents: number; max_agents: number | null; features: string[] | null }[];
}

export default function Billing({ tenant, subscription, plans }: Props) {
    const trialing = subscription?.status === 'trialing';

    return (
        <AppLayout breadcrumbs={[{ title: 'Billing', href: '/billing' }]}>
            <Head title="Billing" />

            <div className="mx-auto w-full max-w-5xl space-y-8 px-4 py-6">
                <HeadingSmall title="Billing" description="Your plan, seats and trial." />

                {subscription && (
                    <section className="grid gap-4 rounded-lg border p-5 sm:grid-cols-3">
                        <Stat label="Plan" value={subscription.plan.name}>
                            <Badge variant={trialing ? 'outline' : 'default'} className="ml-2 capitalize">
                                {subscription.status}
                            </Badge>
                        </Stat>
                        <Stat label="Billable seats" value={String(subscription.seats)} />
                        <Stat
                            label={trialing ? 'Trial ends' : 'Renews'}
                            value={dateTime(trialing ? tenant.trial_ends_at : subscription.current_period_end) || '—'}
                        />
                        <p className="text-muted-foreground text-sm sm:col-span-3">
                            {trialing
                                ? `During the trial you have every ${subscription.plan.name} feature. After it you would pay ${money(subscription.plan.price_cents * subscription.seats)} a month for ${subscription.seats} ${subscription.seats === 1 ? 'seat' : 'seats'}.`
                                : `${money(subscription.plan.price_cents * subscription.seats)} a month for ${subscription.seats} ${subscription.seats === 1 ? 'seat' : 'seats'}.`}
                        </p>
                    </section>
                )}

                <section className="space-y-4">
                    <HeadingSmall title="Plans" description="Online payment is coming soon. To change plan now, contact Tawkzo support." />
                    <div className="grid gap-4 sm:grid-cols-2 lg:grid-cols-4">
                        {plans.map((plan) => {
                            const current = plan.slug === subscription?.plan.slug;
                            return (
                                <div key={plan.slug} className={cn('flex flex-col rounded-lg border p-4', current && 'border-primary ring-primary/20 ring-2')}>
                                    <div className="flex items-center justify-between">
                                        <h3 className="font-semibold">{plan.name}</h3>
                                        {current && <Badge>Current</Badge>}
                                    </div>
                                    <p className="mt-2 text-2xl font-semibold">
                                        {money(plan.price_cents)}
                                        <span className="text-muted-foreground text-sm font-normal"> / agent / mo</span>
                                    </p>
                                    <p className="text-muted-foreground text-xs">{plan.max_agents ? `Up to ${plan.max_agents} agents` : 'Unlimited agents'}</p>
                                    <ul className="mt-3 space-y-1.5 text-sm">
                                        {(plan.features ?? []).map((f) => (
                                            <li key={f} className="flex gap-2">
                                                <Check className="mt-0.5 size-4 shrink-0 text-emerald-600" /> {f}
                                            </li>
                                        ))}
                                    </ul>
                                </div>
                            );
                        })}
                    </div>
                </section>
            </div>
        </AppLayout>
    );
}

function Stat({ label, value, children }: { label: string; value: string; children?: React.ReactNode }) {
    return (
        <div>
            <p className="text-muted-foreground text-xs">{label}</p>
            <p className="mt-1 flex items-center text-lg font-semibold">
                {value}
                {children}
            </p>
        </div>
    );
}
