import { OwnerStayBadge } from '@/components/owner-stay-badge';
import { Card, CardContent } from '@/components/ui/card';
import { useTranslation } from '@/hooks/use-translation';
import type { LogementShowStay, LogementShowToday, LogementShowTodayKind } from '@/types/logement-show';
import { Link } from '@inertiajs/react';
import { BedDouble, LogIn, LogOut, type LucideIcon } from 'lucide-react';
import { NS, fill, stayMeta, weekdayDayMonth } from '../format';
import { CopyInstructionsButton } from './copy-instructions-button';
import { SectionTitle } from './detail-card';

const EVENTS: Record<LogementShowTodayKind, { key: string; icon: LucideIcon }> = {
    arrival: { key: 'today_arrival', icon: LogIn },
    departure: { key: 'today_departure', icon: LogOut },
    in_stay: { key: 'today_in_stay', icon: BedDouble },
};

/** Nom du voyageur, cliquable vers la fiche du séjour quand l'utilisateur y a accès. */
export function StayName({ stay, children }: { stay: LogementShowStay; children: string }) {
    return stay.url ? (
        <Link href={stay.url} className="hover:underline">
            {children}
        </Link>
    ) : (
        <>{children}</>
    );
}

export function StayOwnerBadge() {
    const { t } = useTranslation();
    return <OwnerStayBadge size="sm" label={t(NS, 'owner_stay')} title={t(NS, 'owner_stay_title')} />;
}

/** Logement sans instructions d'entrée : rien à copier, on le dit et on mène à l'étape qui les porte. */
export function MissingInstructionsNotice({ addHref }: { addHref: string | null }) {
    const { t } = useTranslation();
    return (
        <p className="text-muted-foreground mt-3 text-xs">
            {t(NS, 'instructions_missing')}{' '}
            {addHref && (
                <Link href={addHref} className="text-primary dark:text-secondary font-medium hover:underline">
                    {t(NS, 'instructions_add')}
                </Link>
            )}
        </p>
    );
}

interface TodayCardProps {
    today: LogementShowToday;
    hasInstructions: boolean;
    /** Étape « Instructions d'entrée » de l'assistant ; null sans droit de modification. */
    addInstructionsHref: string | null;
}

/** Toujours affichée : « aucun mouvement » est une information, pas un vide. */
export function TodayCard({ today, hasInstructions, addInstructionsHref }: TodayCardProps) {
    const { t, locale } = useTranslation();

    return (
        <Card data-tour="logement-show-today" className="border-secondary/40 bg-secondary/10 gap-0 shadow-none">
            <CardContent>
                <SectionTitle className="dark:text-secondary mb-3 text-emerald-800">
                    {fill(t(NS, 'today_title'), { date: weekdayDayMonth(today.date_iso, locale) })}
                </SectionTitle>
                {today.events.length === 0 ? (
                    <p className="text-muted-foreground text-sm">{t(NS, 'today_empty')}</p>
                ) : (
                    <ul className="space-y-3">
                        {today.events.map(({ kind, stay }) => {
                            const { key, icon: Icon } = EVENTS[kind];
                            return (
                                <li key={`${kind}-${stay.id}`} className="flex items-start gap-2.5">
                                    <Icon className="text-foreground/70 mt-0.5 h-4 w-4 shrink-0" aria-hidden="true" />
                                    <div className="min-w-0">
                                        <p className="flex flex-wrap items-center gap-1.5 text-base font-semibold">
                                            <StayName stay={stay}>{fill(t(NS, key), { name: stay.guest_name })}</StayName>
                                            {stay.is_owner && <StayOwnerBadge />}
                                        </p>
                                        <p className="text-foreground/80 mt-0.5 text-xs">
                                            {stayMeta(stay, locale, t, { arrivalTime: kind === 'arrival' })}
                                        </p>
                                        {kind !== 'departure' && (
                                            <div className="mt-2">
                                                <CopyInstructionsButton stay={stay} />
                                            </div>
                                        )}
                                    </div>
                                </li>
                            );
                        })}
                    </ul>
                )}
                {!hasInstructions && today.events.some(({ kind }) => kind !== 'departure') && (
                    <MissingInstructionsNotice addHref={addInstructionsHref} />
                )}
            </CardContent>
        </Card>
    );
}
