import { cn } from '@/lib/utils';
import { type Availability, type SharedData } from '@/types';
import { router, usePage } from '@inertiajs/react';

const options: { value: Availability; label: string; dot: string }[] = [
    { value: 'online', label: 'Online', dot: 'bg-emerald-500' },
    { value: 'away', label: 'Away', dot: 'bg-amber-500' },
    { value: 'offline', label: 'Offline', dot: 'bg-zinc-400' },
];

export function AvailabilitySwitch() {
    const { auth } = usePage<SharedData>().props;
    const current = options.find((o) => o.value === auth.user.availability) ?? options[2];

    return (
        <label className="relative flex items-center gap-1.5 text-xs font-medium">
            <span className={cn('size-2 rounded-full', current.dot)} aria-hidden />
            <select
                value={current.value}
                onChange={(e) => router.patch('/availability', { availability: e.target.value }, { preserveScroll: true, preserveState: true })}
                className="cursor-pointer appearance-none bg-transparent pr-1 outline-none"
                aria-label="Your availability"
            >
                {options.map((o) => (
                    <option key={o.value} value={o.value}>
                        {o.label}
                    </option>
                ))}
            </select>
        </label>
    );
}
