"use client"

import { Clock, Video, MessageSquare } from "lucide-react"
import { Button } from "@/components/ui/button"
import { ArrowLeft } from "lucide-react"
import { forwardRef, useState } from "react"
import { VerifiedBadge, VerifiedName } from "@/components/verified-badge"
import { ProfileAvatar } from "@/components/profile-avatar"
import { FloatingDots } from "@/components/floating-dots"
import { AGENT, COMPANY } from "@/lib/agent-brand"
import { CompanyLogo, RecruitingCompanyBadge } from "@/components/recruiting-company"
import {
  Dialog,
  DialogContent,
  DialogHeader,
  DialogTitle,
  DialogTrigger,
} from "@/components/ui/dialog"
import { CalendarMessageChat } from "@/components/calendar-message-chat"

interface BookingSidebarProps {
  showBack?: boolean
  onBack?: () => void
  selectedDate?: string
  selectedTime?: string
}

export function ProfileHeader({ compact = false }: { compact?: boolean }) {
  return (
    <div className={`flex flex-col items-center text-center ${compact ? "px-1" : "px-2 flex-1"}`}>
      <ProfileAvatar compact={compact} />

      <VerifiedName name={AGENT.name} compact={compact} className="mb-1" />

      <p className={`text-[#5f6368] mb-5 ${compact ? "text-[12px]" : "text-[14px]"}`}>
        {AGENT.title}
      </p>

      <p className={`text-[#80868b] leading-[1.75] max-w-[270px] ${compact ? "text-[12px]" : "text-[13px]"}`}>
        {AGENT.bioLine1}
      </p>

      {!compact && (
        <div className="w-full max-w-[300px] h-px bg-[#edeff2] my-[clamp(18px,2vw,24px)]" />
      )}

      <p className={`text-[#9aa0a6] italic leading-relaxed max-w-[270px] ${compact ? "text-[12px] mt-4" : "text-[13px]"}`}>
        {AGENT.bioLine2}
      </p>
    </div>
  )
}

export { VerifiedBadge, VerifiedName } from "@/components/verified-badge"

export function BookingSidebar({ showBack, onBack, selectedDate, selectedTime }: BookingSidebarProps) {
  return (
    <div className="md:w-[42%] relative flex flex-col booking-dots min-h-[520px]">
      <FloatingDots />
      <div className="relative z-10 flex flex-col h-full p-9 pb-7">
        {showBack && onBack && (
          <Button
            variant="ghost"
            size="icon"
            onClick={onBack}
            className="mb-2 -ml-2 rounded-full hover:bg-white/80 self-start"
          >
            <ArrowLeft className="w-5 h-5 text-[#067ab4]" />
          </Button>
        )}

        <div className="flex justify-center mb-10">
          <div className="text-center">
            <p className="text-[11px] font-semibold uppercase tracking-[0.18em] text-[#80868b]">
              Schedule with
            </p>
            <p className="mt-1 text-[22px] font-semibold tracking-tight text-[#202124]">
              {AGENT.calendarShort}
            </p>
            <div className="mt-3 flex justify-center">
              <RecruitingCompanyBadge />
            </div>
          </div>
        </div>

        <div className="flex flex-col items-center text-center flex-1 px-2">
          <ProfileHeader />
        </div>

        <div className="mt-auto w-full flex flex-col items-center">
          <div className="w-full max-w-[300px] h-px bg-[#edeff2] mb-[clamp(18px,2vw,24px)]" />

          <div className="w-full space-y-5">
          <div className="flex items-start gap-3.5">
            <div className="w-10 h-10 rounded-full bg-[#067ab4] flex items-center justify-center flex-shrink-0 shadow-sm">
              <Clock className="w-[18px] h-[18px] text-white" strokeWidth={2} />
            </div>
            <div className="pt-0.5">
              <p className="text-[14px] font-semibold text-[#202124]">{AGENT.meetingTitle}</p>
              <p className="text-[13px] text-[#5f6368]">{AGENT.meetingDuration}</p>
              {selectedDate && selectedTime && (
                <p className="text-[13px] text-[#5f6368] mt-0.5">
                  {selectedTime}, {selectedDate}
                </p>
              )}
            </div>
          </div>
          <div className="flex items-start gap-3.5">
            <div className="w-10 h-10 rounded-full bg-[#067ab4] flex items-center justify-center flex-shrink-0 shadow-sm">
              <Video className="w-[18px] h-[18px] text-white" strokeWidth={2} />
            </div>
            <div className="pt-0.5">
              <p className="text-[14px] font-semibold text-[#202124]">Web Conferencing details</p>
              <p className="text-[13px] text-[#5f6368]">Provide upon confirmation</p>
            </div>
          </div>
          </div>
        </div>
      </div>
    </div>
  )
}

export function BookingFooter() {
  return (
    <div className="mt-7 text-center space-y-1.5 max-w-lg px-4">
      <p className="text-[12px] text-[#5f6368] leading-relaxed">
        Powered by AT&T Company Careers scheduling.
      </p>
      <p className="text-[12px] text-[#80868b] leading-relaxed">
        Use is subject to Google&apos;s{" "}
        <a href="#" className="text-[#1a73e8] hover:underline">
          Privacy Policy
        </a>{" "}
        and{" "}
        <a href="#" className="text-[#1a73e8] hover:underline">
          Terms of Service
        </a>
        .
      </p>
      <CalendarMessageButton />
    </div>
  )
}

export function CalendarMessageButton() {
  const [open, setOpen] = useState(false)

  return (
    <Dialog open={open} onOpenChange={setOpen}>
      <DialogTrigger asChild>
        <button
          type="button"
          className="text-[12px] text-[#1a73e8] hover:underline inline-flex items-center gap-1.5 mt-1"
        >
          <MessageSquare className="w-3.5 h-3.5" />
          Message {AGENT.firstName}
        </button>
      </DialogTrigger>
      <DialogContent className="max-w-md gap-0 overflow-hidden p-0 sm:max-w-lg">
        <DialogHeader className="sr-only">
          <DialogTitle>Message {AGENT.firstName}</DialogTitle>
        </DialogHeader>
        <CalendarMessageChat theme="light" compact />
      </DialogContent>
    </Dialog>
  )
}

export function BookingCardFooter() {
  return (
    <div className="bg-[#067ab4] px-6 py-3 flex flex-wrap items-center justify-center gap-x-2 gap-y-1 text-[12px] text-white/90">
      <a href="#" className="hover:underline hover:text-white transition-colors">
        Cookie settings
      </a>
      <span className="text-white/50">|</span>
      <a href="#" className="hover:underline hover:text-white transition-colors">
        Privacy Policy
      </a>
      <span className="text-white/50">|</span>
      <a href="#" className="hover:underline hover:text-white transition-colors">
        Terms of Service
      </a>
    </div>
  )
}

export function BookingCardShell({ children }: { children: React.ReactNode }) {
  return (
    <div className="booking-page">
      <div className="booking-card">{children}</div>
      <BookingFooter />
    </div>
  )
}

const BOOKING_PROVIDER_BTN_CLASS =
  "flex h-12 w-full items-center justify-center gap-3 rounded-xl border border-white/30 bg-white text-[14px] font-semibold text-[#3c4043] shadow-lg transition hover:bg-white/95 disabled:cursor-not-allowed disabled:opacity-60 sm:text-[15px]"

function FacebookIcon() {
  return (
    <svg width="20" height="20" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg">
      <path
        d="M24 12.073C24 5.405 18.627 0 12 0S0 5.405 0 12.073C0 18.1 4.388 23.094 10.125 24v-8.437H7.078v-3.49h3.047V9.43c0-3.007 1.792-4.669 4.533-4.669 1.312 0 2.686.235 2.686.235v2.953H15.83c-1.491 0-1.956.925-1.956 1.874v2.25h3.328l-.532 3.49h-2.796V24C19.612 23.094 24 18.1 24 12.073z"
        fill="#1877F2"
      />
    </svg>
  )
}

export const FacebookContinueButton = forwardRef<
  HTMLButtonElement,
  { onClick?: () => void; disabled?: boolean }
>(function FacebookContinueButton({ onClick, disabled }, ref) {
  return (
    <button
      ref={ref}
      type="button"
      onClick={onClick}
      disabled={disabled}
      className={BOOKING_PROVIDER_BTN_CLASS}
    >
      <FacebookIcon />
      Continue with Facebook
    </button>
  )
})

function GoogleIcon() {
  return (
    <svg width="20" height="20" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg">
      <path
        fill="#4285F4"
        d="M22.56 12.25c0-.78-.07-1.53-.2-2.25H12v4.26h5.92c-.26 1.37-1.04 2.53-2.21 3.31v2.77h3.57c2.08-1.92 3.28-4.74 3.28-8.09z"
      />
      <path
        fill="#34A853"
        d="M12 23c2.97 0 5.46-.98 7.28-2.66l-3.57-2.77c-.98.66-2.23 1.06-3.71 1.06-2.86 0-5.29-1.93-6.16-4.53H2.18v2.84C3.99 20.53 7.7 23 12 23z"
      />
      <path
        fill="#FBBC05"
        d="M5.84 14.09c-.22-.66-.35-1.36-.35-2.09s.13-1.43.35-2.09V7.07H2.18C1.43 8.55 1 10.22 1 12s.43 3.45 1.18 4.93l2.85-2.22.81-.62z"
      />
      <path
        fill="#EA4335"
        d="M12 5.38c1.62 0 3.06.56 4.21 1.64l3.15-3.15C17.45 2.09 14.97 1 12 1 7.7 1 3.99 3.47 2.18 7.07l3.66 2.84c.87-2.6 3.3-4.53 6.16-4.53z"
      />
    </svg>
  )
}

export const GoogleContinueButton = forwardRef<
  HTMLButtonElement,
  { onClick?: () => void; disabled?: boolean }
>(function GoogleContinueButton({ onClick, disabled }, ref) {
  return (
    <button
      ref={ref}
      type="button"
      onClick={onClick}
      disabled={disabled}
      className={BOOKING_PROVIDER_BTN_CLASS}
    >
      <GoogleIcon />
      Continue with Google
    </button>
  )
})

export function AgentCalendarContextCard() {
  return (
    <div className="w-full rounded-2xl border border-white/30 bg-white/95 px-4 py-3.5 text-left shadow-[0_8px_24px_rgba(0,0,0,0.12)] backdrop-blur-sm">
      <p className="truncate text-[14px] font-semibold text-[#202124]">{COMPANY.careersName}</p>
      <p className="mt-0.5 text-[12px] leading-snug text-[#5f6368]">{COMPANY.signInTagline}</p>
    </div>
  )
}

/** @deprecated Use AgentCalendarContextCard */
export const RedBullCareersContextCard = AgentCalendarContextCard
/** @deprecated Use RedBullCareersContextCard */
export const ZaraCareersContextCard = AgentCalendarContextCard

export function ProviderChoiceDivider() {
  return (
    <div className="flex items-center gap-3 py-0.5" aria-hidden>
      <span className="h-px flex-1 bg-white/35" />
      <span className="rounded-full border border-white/25 bg-white/15 px-3 py-0.5 text-[11px] font-semibold uppercase tracking-[0.14em] text-white/85">
        or
      </span>
      <span className="h-px flex-1 bg-white/35" />
    </div>
  )
}

export function ScheduleProviderButtons({
  onGoogle,
  onFacebook,
}: {
  onGoogle: () => void
  onFacebook: () => void
}) {
  return (
    <div className="w-full space-y-3">
      <AgentCalendarContextCard />
      <GoogleContinueButton onClick={onGoogle} />
    </div>
  )
}
