import { Link, router, usePage } from '@inertiajs/react';
import { Building2, Check, ChevronDown, Settings2 } from 'lucide-react';
import BusinessSwitchController from '@/actions/App/Http/Controllers/Admin/BusinessSwitchController';
import {
    DropdownMenu,
    DropdownMenuContent,
    DropdownMenuItem,
    DropdownMenuLabel,
    DropdownMenuSeparator,
    DropdownMenuTrigger,
} from '@/components/ui/dropdown-menu';
import { index as businessesIndex } from '@/routes/admin/businesses';

export type BusinessSwitcherProps = {
    current: number | null;
    options: { id: number; name: string; active: boolean }[];
};

/** Work inside the given business. */
export function switchBusiness(business: number): void {
    router.post(BusinessSwitchController.url(), { business });
}

/**
 * Super Administrator only: the header menu for choosing the business to
 * work in. There is always one business selected.
 */
export default function BusinessSwitcher() {
    const switcher: BusinessSwitcherProps | null =
        usePage().props.businessSwitcher;

    if (!switcher) {
        return null;
    }

    const currentName =
        switcher.options.find((option) => option.id === switcher.current)
            ?.name ?? 'Choose a business';

    return (
        <DropdownMenu>
            <DropdownMenuTrigger
                className="flex h-9 max-w-44 items-center gap-2 rounded-full border border-surface-200 bg-white px-3 text-sm font-semibold text-ink transition-colors hover:bg-surface-100 focus-visible:ring-2 focus-visible:ring-highlight focus-visible:outline-none data-[state=open]:bg-surface-100 sm:max-w-64"
                aria-label={`Business: ${currentName}`}
            >
                <Building2 className="size-4 shrink-0 text-ink-400" />
                <span className="min-w-0 truncate">{currentName}</span>
                <ChevronDown className="size-4 shrink-0 opacity-60" />
            </DropdownMenuTrigger>

            <DropdownMenuContent
                align="end"
                className="max-h-[70vh] w-72 overflow-y-auto rounded-landing"
            >
                <DropdownMenuLabel className="text-xs font-semibold text-ink-400">
                    Work inside
                </DropdownMenuLabel>

                {switcher.options.map((business) => (
                    <DropdownMenuItem
                        key={business.id}
                        onSelect={() => switchBusiness(business.id)}
                        className="gap-2.5"
                    >
                        <Building2 className="size-4 text-ink-400" />
                        <span className="min-w-0 flex-1 truncate">
                            {business.name}
                        </span>
                        {!business.active && (
                            <span className="rounded-full bg-red-50 px-1.5 py-0.5 text-[10px] font-semibold text-red-600">
                                Suspended
                            </span>
                        )}
                        {switcher.current === business.id && (
                            <Check className="size-4" />
                        )}
                    </DropdownMenuItem>
                ))}

                <DropdownMenuSeparator />
                <DropdownMenuItem asChild className="gap-2.5">
                    <Link href={businessesIndex()}>
                        <Settings2 className="size-4 text-ink-400" />
                        Manage businesses
                    </Link>
                </DropdownMenuItem>
            </DropdownMenuContent>
        </DropdownMenu>
    );
}
