'use client'

import { useState, type FormEvent } from 'react'
import { Clock, Mail, MessageCircle, Send } from 'lucide-react'

import Button from '@/components/ui/Button'
import Input from '@/components/ui/Input'
import { useVulaContent } from '@/hooks/useVulaContent'

const WHATSAPP_NUMBER = '27825550147'
const EMAIL = 'hello@ikhayahome.co.za'

export default function KhulumaNathiSection() {
  const { items, loading } = useVulaContent('business_hours')
  const [name, setName] = useState('')
  const [email, setEmail] = useState('')
  const [message, setMessage] = useState('')
  const [sent, setSent] = useState(false)

  const handleSubmit = (e: FormEvent) => {
    e.preventDefault()
    setSent(true)
  }

  if (loading) {
    return (
      <div className="flex justify-center py-16">
        <div className="w-8 h-8 rounded-full border-2 border-current border-t-transparent animate-spin" />
      </div>
    )
  }

  if (items.length === 0) return null

  return (
    <section id="contact" data-vula-section="khulumanathi" data-vula-cms="business_hours" className="bg-[#f9f5f4] py-20 px-4">
      <div className="mx-auto grid max-w-6xl gap-12 lg:grid-cols-2 lg:items-start">
        <div>
          <p className="text-sm font-semibold uppercase tracking-widest text-[#8b5a3c]">Khuluma nathi</p>
          <h2 className="mt-2 font-display text-4xl leading-tight text-[#21140d]">Tell us about your space &mdash; we&rsquo;re listening.</h2>
          <p className="mt-4 mb-8 text-[#5b463a]">Need help choosing cushion colours or ceramic finishes? Our Durban studio replies within one working day.</p>
          <div className="space-y-4">
            <a href={`https://wa.me/${WHATSAPP_NUMBER}`} target="_blank" rel="noreferrer" className="flex items-center gap-4 rounded-2xl border border-[#e2d9d4] bg-white p-4 transition-all duration-200 ease-out hover:border-[#6b8e23] hover:shadow-md">
              <MessageCircle className="h-6 w-6 text-[#6b8e23]" />
              <span>
                <span className="block text-sm text-[#8b5a3c]">WhatsApp us</span>
                <span className="font-semibold text-[#21140d]">Chat to the studio</span>
              </span>
            </a>
            <a href={`mailto:${EMAIL}`} className="flex items-center gap-4 rounded-2xl border border-[#e2d9d4] bg-white p-4 transition-all duration-200 ease-out hover:border-[#ea580c] hover:shadow-md">
              <Mail className="h-6 w-6 text-[#ea580c]" />
              <span>
                <span className="block text-sm text-[#8b5a3c]">Email us</span>
                <span className="font-semibold text-[#21140d]">{EMAIL}</span>
              </span>
            </a>
          </div>
          <div className="mt-8 rounded-2xl border border-[#e2d9d4] bg-white/70 p-6">
            <h3 className="mb-4 flex items-center gap-2 font-display text-xl text-[#21140d]"><Clock className="h-5 w-5 text-[#ea580c]" /> Trading hours</h3>
            <ul className="divide-y divide-[#e2d9d4]">
              {items.map((item) => {
                const d = item.data as { day_of_week?: string; open_time?: string; close_time?: string; is_closed?: boolean; note?: string }
                return (
                  <li key={item.id} className="flex items-center justify-between py-2 text-sm">
                    <span className="text-[#21140d]">{d.day_of_week ?? '—'}</span>
                    <span className="font-medium text-[#6b8e23]">{d.is_closed ? 'Closed' : `${d.open_time ?? '—'} – ${d.close_time ?? '—'}`}</span>
                  </li>
                )
              })}
            </ul>
            <p className="mt-3 text-xs text-[#8b5a3c]">Open on public holidays &mdash; call ahead to confirm.</p>
          </div>
        </div>
        <div className="rounded-2xl border border-[#e2d9d4] bg-white p-6 shadow-lg sm:p-8">
          <h3 className="font-display text-2xl text-[#21140d]">Send us a note</h3>
          <p className="mt-1 mb-6 text-sm text-[#5b463a]">No question is too small.</p>
          <form onSubmit={handleSubmit} className="space-y-4">
            <Input value={name} onChange={(e) => setName(e.target.value)} placeholder="Your name" required />
            <Input type="email" value={email} onChange={(e) => setEmail(e.target.value)} placeholder="Email address" required />
            <textarea value={message} onChange={(e) => setMessage(e.target.value)} placeholder="Tell us about your space..." required rows={4} className="w-full rounded-xl border border-[#e2d9d4] bg-[#f9f5f4] px-4 py-3 text-[#21140d] outline-none transition-all duration-200 ease-out focus:ring-2 focus:ring-[#ea580c]" />
            <Button type="submit" variant="default" size="lg" className="w-full">Send message <Send className="ml-2 h-4 w-4" /></Button>
            {sent && <p className="text-sm font-semibold text-[#6b8e23]">Thank you &mdash; we&rsquo;ll be in touch soon.</p>}
          </form>
        </div>
      </div>
    </section>
  )
}