"use client";

import { useMemo, useState } from "react";
import { Mail, Phone } from "lucide-react";

import { Button } from "@/components/ui/button";
import {
  Dialog,
  DialogContent,
  DialogDescription,
  DialogFooter,
  DialogHeader,
  DialogTitle,
} from "@/components/ui/dialog";

type GuideContactActionsProps = {
  email: string;
  name: string;
  phone: string;
};

function getTelHref(phone: string) {
  return `tel:${phone.replace(/[^\d+]/g, "")}`;
}

function canUsePhoneDialer() {
  const userAgent = navigator.userAgent.toLowerCase();
  const isMobileDevice = /android|iphone|ipad|ipod|mobile|windows phone/.test(
    userAgent,
  );
  const hasTouchScreen = navigator.maxTouchPoints > 1;

  return isMobileDevice && hasTouchScreen;
}

export function GuideContactActions({
  email,
  name,
  phone,
}: GuideContactActionsProps) {
  const [isCallDialogOpen, setIsCallDialogOpen] = useState(false);
  const telHref = useMemo(() => getTelHref(phone), [phone]);
  const mailHref = useMemo(
    () =>
      `mailto:${email}?subject=${encodeURIComponent(
        "Inquiry Regarding Your Services",
      )}`,
    [email],
  );

  const handleContactGuide = () => {
    if (canUsePhoneDialer()) {
      window.location.href = telHref;
      return;
    }

    setIsCallDialogOpen(true);
  };

  return (
    <>
      <div className="pt-4 space-y-3">
        <Button
          type="button"
          onClick={handleContactGuide}
          className="h-12 w-full rounded-full bg-gradient-to-r from-[var(--theme-secondary)] to-[var(--theme-accent)] text-base font-semibold text-white shadow-xl shadow-[var(--theme-secondary)]/25 hover:from-[var(--theme-secondary)] hover:to-[var(--theme-accent-dark)]"
        >
          <Phone className="h-4 w-4" />
          Contact Guide
        </Button>
        <Button
          asChild
          variant="outline"
          className="h-12 w-full rounded-full border border-[var(--theme-secondary)]/30 bg-white px-6 text-base font-semibold text-foreground shadow-md hover:bg-[var(--theme-surface-strong)] hover:text-[var(--theme-accent)]"
        >
          <a href={mailHref}>
            <Mail className="h-4 w-4" />
            Send Inquiry
          </a>
        </Button>
      </div>

      <Dialog open={isCallDialogOpen} onOpenChange={setIsCallDialogOpen}>
        <DialogContent>
          <DialogHeader>
            <DialogTitle>Calling app not available</DialogTitle>
            <DialogDescription>
              Your browser does not seem to have a calling app or calling option
              available. Please call {name} manually using this number.
            </DialogDescription>
          </DialogHeader>

          <div className="rounded-2xl border border-[var(--theme-secondary)]/20 bg-[var(--theme-surface-strong)] px-4 py-3 text-center text-lg font-bold text-foreground">
            {phone || "Phone number not available"}
          </div>

          <DialogFooter>
            <Button
              type="button"
              variant="outline"
              onClick={() => setIsCallDialogOpen(false)}
            >
              Close
            </Button>
            {phone && (
              <Button asChild>
                <a href={telHref}>Try Calling Anyway</a>
              </Button>
            )}
          </DialogFooter>
        </DialogContent>
      </Dialog>
    </>
  );
}
