import { Head, Link, router, usePage } from '@inertiajs/react';
import { PortfolioTabs, LocationFilter } from '@/components/portfolio/PortfolioTabs';
import { ArrowLeft } from 'lucide-react';

type Month = {
    month: string;
    counts: Record<string, number>;
    projected: Record<string, number>;
    non_renewal_projected: number;
    broken_projected: number;
    scheduled_move_outs: number;
    grand_total_mo: number;
};

export default function MonthlyProjections() {
    const props = usePage().props as unknown as {
        months: Month[]; location: string | null;
        locationOptions: { id: number; name: string }[];
        rates: Record<string, number>;
    };
    const { months, location, locationOptions, rates } = props;

    return (
        <>
            <Head title="Monthly Projections" />
            <div className="mx-auto w-full max-w-6xl px-6 py-8">
                <PortfolioTabs />

                <Link href={`/portfolio/retention?location=${location ?? ''}`}
                    className="mb-5 inline-flex items-center gap-1.5 rounded-md bg-primary px-3.5 py-2 text-sm font-medium text-primary-foreground hover:opacity-90">
                    <ArrowLeft className="size-4" /> Back to Lease List
                </Link>

                <h1 className="text-xl font-semibold text-foreground">Upcoming 12 months</h1>
                <p className="mt-1 text-sm text-muted-foreground">
                    Each cell shows the renewal count and the projected move-outs in parentheses, using the
                    non-renewal rates: 1st {Math.round(rates.first * 100)}%, 2nd {Math.round(rates.second * 100)}%,
                    3rd {Math.round(rates.third * 100)}%, long term {Math.round(rates.long_term * 100)}%.
                </p>

                <div className="my-4">
                    <LocationFilter options={locationOptions} value={location}
                        onChange={(v) => router.get('/portfolio/monthly-projections', { location: v }, { preserveState: true })} />
                </div>

                <div className="overflow-x-auto rounded-xl border border-border">
                    <table className="w-full text-sm">
                        <thead className="bg-muted/50 text-xs uppercase tracking-wide text-muted-foreground">
                            <tr>
                                <th className="px-3 py-2.5 text-left font-medium">Month</th>
                                <th className="px-3 py-2.5 text-right font-medium">1st renewal</th>
                                <th className="px-3 py-2.5 text-right font-medium">2nd renewal</th>
                                <th className="px-3 py-2.5 text-right font-medium">3rd renewal</th>
                                <th className="px-3 py-2.5 text-right font-medium">Long term</th>
                                <th className="px-3 py-2.5 text-right font-medium">Non renewal proj</th>
                                <th className="px-3 py-2.5 text-right font-medium">Broken proj</th>
                                <th className="px-3 py-2.5 text-right font-medium">Scheduled MO</th>
                                <th className="px-3 py-2.5 text-right font-medium">Grand total MO</th>
                            </tr>
                        </thead>
                        <tbody className="divide-y divide-border">
                            {months.map((m) => {
                                const totalCount = m.counts.first + m.counts.second + m.counts.third + m.counts.long_term;
                                return (
                                    <tr key={m.month}>
                                        <td className="px-3 py-2.5 font-medium text-foreground">{m.month}</td>
                                        <Cell n={m.counts.first} p={m.projected.first} tone="bg-rose-50 dark:bg-rose-500/10" />
                                        <Cell n={m.counts.second} p={m.projected.second} tone="bg-amber-50 dark:bg-amber-500/10" />
                                        <Cell n={m.counts.third} p={m.projected.third} tone="bg-yellow-50 dark:bg-yellow-500/10" />
                                        <Cell n={m.counts.long_term} p={m.projected.long_term} tone="bg-emerald-50 dark:bg-emerald-500/10" />
                                        <Cell n={totalCount} p={m.non_renewal_projected} />
                                        <td className="bg-slate-100 px-3 py-2.5 text-right text-foreground dark:bg-slate-500/10">
                                            −{m.broken_projected}
                                        </td>
                                        <td className="bg-slate-800 px-3 py-2.5 text-right font-medium text-white">
                                            {m.scheduled_move_outs > 0 ? `−${m.scheduled_move_outs}` : 0}
                                        </td>
                                        <td className="bg-accent/40 px-3 py-2.5 text-right font-semibold text-foreground">
                                            −{m.grand_total_mo}
                                        </td>
                                    </tr>
                                );
                            })}
                        </tbody>
                    </table>
                </div>
            </div>
        </>
    );
}

function Cell({ n, p, tone }: { n: number; p: number; tone?: string }) {
    return (
        <td className={`px-3 py-2.5 text-right ${tone ?? ''}`}>
            <span className="font-medium text-foreground">{n}</span>
            {p > 0 && <span className="ml-1.5 text-xs text-destructive">−{p}</span>}
        </td>
    );
}
