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

type Loc = {
    location_id: number | null; name: string; units: number; occupied: number; vacant: number;
    occupancy_rate: number; expiring_soon: number; move_ins: number; move_outs: number; net: number;
};
type Totals = Loc extends never ? never : {
    locations: number; units: number; occupied: number; vacant: number;
    move_ins: number; move_outs: number; net: number; expiring_soon: number; occupancy_rate: number;
};

function rateColor(rate: number) {
    if (rate >= 90) return 'text-emerald-600';
    if (rate >= 75) return 'text-foreground';
    return 'text-amber-600';
}

export default function Overview() {
    const { totals, locations, window: win, seesAll } = usePage().props as unknown as {
        totals: Totals; locations: Loc[]; window: number; seesAll: boolean;
    };

    return (
        <>
            <Head title="Portfolio" />
            <div className="mx-auto w-full max-w-6xl px-6 py-8">
                <header className="mb-6">
                    <h1 className="flex items-center gap-2 text-2xl font-semibold tracking-tight text-foreground">
                        <Building2 className="size-5 text-primary" /> Portfolio
                    </h1>
                    <p className="mt-1 text-sm text-muted-foreground">
                        {seesAll ? 'All locations' : 'Your assigned locations'} · live from Rentec
                    </p>
                </header>

                <PortfolioTabs />

                {/* Company totals */}
                <section className="mb-6 rounded-xl border border-border bg-accent/30 p-6">
                    <div className="mb-4 flex items-baseline justify-between">
                        <h2 className="font-semibold text-foreground">
                            Company Totals <span className="ml-2 text-sm font-normal text-muted-foreground">{totals.locations} locations</span>
                        </h2>
                        <div className="text-right">
                            <p className={`text-sm font-semibold ${rateColor(totals.occupancy_rate)}`}>{totals.occupancy_rate}% occupied</p>
                            <p className="text-xs text-muted-foreground">{totals.expiring_soon} exp ≤{win}d</p>
                        </div>
                    </div>

                    <div className="grid grid-cols-3 gap-4 border-b border-border pb-4">
                        <Stat label="Units" value={totals.units} />
                        <Stat label="Occupied" value={totals.occupied} />
                        <Stat label="Vacant" value={totals.vacant} />
                    </div>
                    <div className="grid grid-cols-3 gap-4 pt-4">
                        <Stat label="Move-Ins" value={totals.move_ins} />
                        <Stat label="Move-Outs" value={totals.move_outs} />
                        <Stat label="Net" value={totals.net} signed />
                    </div>
                </section>

                {/* Per-location cards */}
                <div className="grid gap-4 sm:grid-cols-2 lg:grid-cols-3">
                    {locations.map((l) => (
                        <div key={l.name} className="rounded-xl border border-border bg-card p-5 shadow-sm">
                            <div className="mb-3 flex items-start justify-between">
                                <p className="font-semibold text-foreground">{l.name}</p>
                                <div className="text-right">
                                    <p className={`text-xs font-semibold ${rateColor(l.occupancy_rate)}`}>{l.occupancy_rate}% occupied</p>
                                    <p className="text-[11px] text-muted-foreground">{l.expiring_soon} exp ≤{win}d</p>
                                </div>
                            </div>
                            <div className="grid grid-cols-3 gap-2 border-b border-border pb-3">
                                <Mini label="Units" value={l.units} />
                                <Mini label="Occupied" value={l.occupied} />
                                <Mini label="Vacant" value={l.vacant} />
                            </div>
                            <div className="grid grid-cols-3 gap-2 pt-3">
                                <Mini label="Move-Ins" value={l.move_ins} />
                                <Mini label="Move-Outs" value={l.move_outs} />
                                <Mini label="Net" value={l.net} signed />
                            </div>
                        </div>
                    ))}
                    {locations.length === 0 && (
                        <p className="col-span-3 rounded-xl border border-dashed border-border p-12 text-center text-sm text-muted-foreground">
                            No locations available. If you're not an administrator, ask to be assigned to a location.
                        </p>
                    )}
                </div>
            </div>
        </>
    );
}

function Stat({ label, value, signed }: { label: string; value: number; signed?: boolean }) {
    const color = signed ? (value > 0 ? 'text-emerald-600' : value < 0 ? 'text-destructive' : 'text-foreground') : 'text-foreground';
    return (
        <div className="text-center">
            <p className={`text-3xl font-semibold ${color}`}>{signed && value > 0 ? '+' : ''}{value.toLocaleString()}</p>
            <p className="mt-0.5 text-xs text-muted-foreground">{label}</p>
        </div>
    );
}

function Mini({ label, value, signed }: { label: string; value: number; signed?: boolean }) {
    const color = signed ? (value > 0 ? 'text-emerald-600' : value < 0 ? 'text-destructive' : 'text-foreground') : 'text-foreground';
    return (
        <div className="text-center">
            <p className={`text-lg font-semibold ${color}`}>{signed && value > 0 ? '+' : ''}{value}</p>
            <p className="text-[10px] text-muted-foreground">{label}</p>
        </div>
    );
}
