import { Head, router } from '@inertiajs/react';
import { Store } from 'lucide-react';
import { motion, useReducedMotion } from 'motion/react';
import { update } from '@/actions/App/Http/Controllers/MaquisSelectionController';
import {
    Card,
    CardDescription,
    CardHeader,
    CardTitle,
} from '@/components/ui/card';

const MotionCard = motion.create(Card);

type MaquisOption = {
    id: number;
    name: string;
    city: string | null;
    logo_path: string | null;
};

type Props = {
    maquisList: MaquisOption[];
};

export default function SelectMaquis({ maquisList }: Props) {
    const shouldReduceMotion = useReducedMotion();

    const selectMaquis = (maquisId: number) => {
        router.post(update().url, { maquis_id: maquisId });
    };

    return (
        <>
            <Head title="Choisir un maquis" />

            <div className="grid gap-3">
                {maquisList.map((maquis) => (
                    <MotionCard
                        key={maquis.id}
                        role="button"
                        tabIndex={0}
                        onClick={() => selectMaquis(maquis.id)}
                        onKeyDown={(e) => {
                            if (e.key === 'Enter' || e.key === ' ') {
                                selectMaquis(maquis.id);
                            }
                        }}
                        whileHover={
                            shouldReduceMotion
                                ? undefined
                                : {
                                      y: -3,
                                      boxShadow:
                                          '0 8px 20px -8px rgb(0 0 0 / 0.18)',
                                  }
                        }
                        whileTap={shouldReduceMotion ? undefined : { y: 0 }}
                        transition={{ duration: 0.2, ease: 'easeOut' }}
                        className="hover:border-primary py-4"
                    >
                        <CardHeader className="flex-row items-center gap-3 px-4">
                            <div className="bg-muted flex size-10 shrink-0 items-center justify-center rounded-md">
                                <Store className="text-muted-foreground size-5" />
                            </div>
                            <div>
                                <CardTitle>{maquis.name}</CardTitle>
                                {maquis.city && (
                                    <CardDescription>
                                        {maquis.city}
                                    </CardDescription>
                                )}
                            </div>
                        </CardHeader>
                    </MotionCard>
                ))}
            </div>
        </>
    );
}

SelectMaquis.layout = {
    title: 'Choisissez un maquis',
    description: "Sélectionnez l'établissement que vous souhaitez gérer.",
};
