'use client';

import { useCustomerQuery } from '@/hooks/api/customers';
import { usePushWithData } from '@/hooks/url';
import { Button } from '@repo/ui/components/ui/button';
import {
  Drawer,
  DrawerClose,
  DrawerContent,
  DrawerDescription,
  DrawerFooter,
  DrawerHeader,
  DrawerTitle,
  DrawerTrigger,
} from '@repo/ui/components/ui/drawer';

type QuickAppointmentProps = {
  readonly telegramId: number;
};

export function QuickAppointment({ telegramId }: Readonly<QuickAppointmentProps>) {
  const push = usePushWithData();

  const { data: { customer: profile } = {} } = useCustomerQuery({ telegramId });
  const { data: { customer: currentUser } = {} } = useCustomerQuery();

  const handleBookAsClient = () => {
    push('/orders/add', {
      client: currentUser,
      slot: { master: profile },
    });
  };

  const handleBookAsMaster = () => {
    push('/orders/add', {
      client: profile,
      slot: { master: currentUser },
    });
  };

  if (!telegramId) return null;

  return (
    <Drawer>
      <DrawerTrigger asChild>
        <div className="mt-4 flex justify-center">
          <Button className="px-6 py-2 text-sm font-semibold" size="sm">
            Быстрая запись
          </Button>
        </div>
      </DrawerTrigger>
      <DrawerContent>
        <div className="mx-auto w-full max-w-sm">
          <DrawerHeader>
            <DrawerTitle>Быстрая запись</DrawerTitle>
            <DrawerDescription>Выберите действие</DrawerDescription>
          </DrawerHeader>
          <div className="p-4 pt-0">
            <div className="flex flex-col gap-3">
              <DrawerClose asChild>
                <Button
                  className="w-full text-sm"
                  disabled={!handleBookAsClient}
                  onClick={handleBookAsClient}
                  size="sm"
                >
                  Записаться к мастеру {profile?.name}
                </Button>
              </DrawerClose>
              <DrawerClose asChild>
                <Button
                  className="w-full text-sm"
                  disabled={!handleBookAsMaster}
                  onClick={handleBookAsMaster}
                  size="sm"
                  variant="secondary"
                >
                  Записать клиента к себе
                </Button>
              </DrawerClose>
            </div>
          </div>
          <DrawerFooter>
            <DrawerClose asChild>
              <Button variant="outline">Отмена</Button>
            </DrawerClose>
          </DrawerFooter>
        </div>
      </DrawerContent>
    </Drawer>
  );
}
