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

type Month = {
    month: string;
    non_renewal_projected: number; broken_projected: number; scheduled_move_outs: number;
    grand_total_mo: number; projected_leases_signed: number; scheduled_move_ins: number;
    projected_net_move_in: number; projected_occupied: number; total_units: number;
    projected_occupancy_rate: number;
};

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

    return (
        <>
            <Head title="Projections" />
            <div className="mx-auto w-full max-w-7xl px-6 py-8">
                <header className="mb-6">
                    <h1 className="flex items-center gap-2 text-2xl font-semibold tracking-tight text-foreground">
                        <TrendingUp className="size-5 text-primary" /> Projections
                    </h1>
                    <p className="mt-1 text-sm text-muted-foreground">
                        Twelve-month occupancy forecast from renewals, broken leases, and expected new signings.
                    </p>
                </header>

                <PortfolioTabs />

                <div className="mb-4">
                    <LocationFilter options={locationOptions} value={location}
                        onChange={(v) => router.get('/portfolio/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">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="border-l border-border bg-accent/30 px-3 py-2.5 text-right font-medium">= Grand total MO</th>
                                <th className="px-3 py-2.5 text-right font-medium">Projected leases signed</th>
                                <th className="px-3 py-2.5 text-right font-medium">Scheduled move ins</th>
                                <th className="border-l border-border px-3 py-2.5 text-right font-medium">Proj net move in</th>
                                <th className="px-3 py-2.5 text-right font-medium">Proj occupancy</th>
                            </tr>
                        </thead>
                        <tbody className="divide-y divide-border">
                            {months.map((m) => (
                                <tr key={m.month}>
                                    <td className="px-3 py-2.5 font-medium text-foreground">{m.month}</td>
                                    <td className="px-3 py-2.5 text-right text-muted-foreground">{m.non_renewal_projected ? `−${m.non_renewal_projected}` : 0}</td>
                                    <td className="px-3 py-2.5 text-right text-muted-foreground">−{m.broken_projected}</td>
                                    <td className="px-3 py-2.5 text-right text-muted-foreground">{m.scheduled_move_outs ? `−${m.scheduled_move_outs}` : 0}</td>
                                    <td className="border-l border-border bg-accent/30 px-3 py-2.5 text-right font-semibold text-foreground">−{m.grand_total_mo}</td>
                                    <td className="px-3 py-2.5 text-right text-emerald-600">+{m.projected_leases_signed}</td>
                                    <td className="px-3 py-2.5 text-right text-emerald-600">{m.scheduled_move_ins ? `+${m.scheduled_move_ins}` : 0}</td>
                                    <td className={`border-l border-border px-3 py-2.5 text-right font-semibold ${m.projected_net_move_in >= 0 ? 'text-emerald-600' : 'text-destructive'}`}>
                                        {m.projected_net_move_in >= 0 ? '+' : ''}{m.projected_net_move_in}
                                    </td>
                                    <td className="px-3 py-2.5 text-right text-foreground">
                                        {m.projected_occupied} / {m.total_units} <span className="text-muted-foreground">({m.projected_occupancy_rate}%)</span>
                                    </td>
                                </tr>
                            ))}
                        </tbody>
                    </table>
                </div>
            </div>
        </>
    );
}
