"use client";

import { useState } from "react";
import { motion } from "framer-motion";
import { Mail, MessageSquare, Clock, Globe, Briefcase, Sparkles, Send, Loader2, CheckCircle, AlertTriangle } from "lucide-react";

import { Badge } from "@/components/ui/badge";
import { Button } from "@/components/ui/button";
import { Input } from "@/components/ui/input";
import { Label } from "@/components/ui/label";
import { Textarea } from "@/components/ui/textarea";
import {
  Select,
  SelectContent,
  SelectItem,
  SelectTrigger,
  SelectValue,
} from "@/components/ui/select";
import { publicApi } from "@/lib/api";

const contactInfo = [
  {
    icon: Mail,
    label: "Email",
    value: "support@reelscutter.com",
    subtext: "For support & general inquiries",
    color: "text-violet-400",
    bg: "bg-violet-500/10",
    border: "border-violet-500/20",
  },
  {
    icon: MessageSquare,
    label: "Enterprise",
    value: "enterprise@reelscutter.com",
    subtext: "For Pro Max & custom integrations",
    color: "text-cyan-400",
    bg: "bg-cyan-500/10",
    border: "border-cyan-500/20",
  },
  {
    icon: Clock,
    label: "Response Time",
    value: "Under 24 hours",
    subtext: "Monday – Friday (IST)",
    color: "text-emerald-400",
    bg: "bg-emerald-500/10",
    border: "border-emerald-500/20",
  },
];

export default function ContactPage() {
  const [name, setName] = useState("");
  const [email, setEmail] = useState("");
  const [subject, setSubject] = useState("");
  const [message, setMessage] = useState("");
  const [statusMessage, setStatusMessage] = useState("");
  const [error, setError] = useState("");
  const [submitting, setSubmitting] = useState(false);

  const handleSubmit = async (event: React.FormEvent) => {
    event.preventDefault();
    setError("");
    setStatusMessage("");
    if (!subject) {
      setError("Please select a subject");
      return;
    }
    setSubmitting(true);

    try {
      const response = await publicApi.submitContact({ name, email, subject, message });
      setStatusMessage(response.message);
      setName("");
      setEmail("");
      setSubject("");
      setMessage("");
    } catch (err: unknown) {
      setError(err instanceof Error ? err.message : "Could not send message");
    } finally {
      setSubmitting(false);
    }
  };

  return (
    <div className="min-h-screen pt-24 pb-16 px-4">
      <div className="fixed inset-0 bg-radial-violet pointer-events-none" />
      <div className="fixed inset-0 bg-grid pointer-events-none opacity-40" />

      <div className="container mx-auto max-w-5xl relative">
        {/* Header */}
        <motion.div
          initial={{ opacity: 0, y: 20 }}
          animate={{ opacity: 1, y: 0 }}
          transition={{ duration: 0.5 }}
          className="text-center mb-14"
        >
          <Badge className="bg-violet-500/15 text-violet-300 border-violet-500/30 mb-4">
            <Sparkles className="w-3.5 h-3.5 mr-1.5" />
            Contact
          </Badge>
          <h1 className="text-4xl md:text-5xl font-black mb-4">
            Get in <span className="gradient-text">Touch</span>
          </h1>
          <p className="text-zinc-400 max-w-lg mx-auto">
            Questions about pricing, integrations, or enterprise deals? We usually respond within a
            business day.
          </p>
        </motion.div>

        <div className="grid md:grid-cols-5 gap-8">
          {/* Left: info */}
          <div className="md:col-span-2 space-y-4">
            {contactInfo.map((info, i) => (
              <motion.div
                key={i}
                initial={{ opacity: 0, x: -20 }}
                animate={{ opacity: 1, x: 0 }}
                transition={{ duration: 0.4, delay: i * 0.1 }}
                className={`glass-card p-5 border ${info.border}`}
              >
                <div className={`w-10 h-10 rounded-xl ${info.bg} border ${info.border} flex items-center justify-center mb-3`}>
                  <info.icon className={`w-5 h-5 ${info.color}`} />
                </div>
                <p className="text-xs text-zinc-500 mb-0.5">{info.label}</p>
                <p className="text-sm font-semibold text-white">{info.value}</p>
                <p className="text-xs text-zinc-500 mt-1">{info.subtext}</p>
              </motion.div>
            ))}

            {/* Social */}
            <motion.div
              initial={{ opacity: 0, x: -20 }}
              animate={{ opacity: 1, x: 0 }}
              transition={{ duration: 0.4, delay: 0.3 }}
              className="glass-card p-5"
            >
              <p className="text-xs text-zinc-500 mb-3">Follow us</p>
              <div className="flex gap-3">
                <a
                  href="#"
                  className="w-9 h-9 rounded-lg bg-white/5 border border-white/8 flex items-center justify-center text-zinc-400 hover:text-white hover:border-violet-500/50 hover:bg-violet-500/10 transition-all"
                >
                  <Globe className="w-4 h-4" />
                </a>
                <a
                  href="#"
                  className="w-9 h-9 rounded-lg bg-white/5 border border-white/8 flex items-center justify-center text-zinc-400 hover:text-white hover:border-violet-500/50 hover:bg-violet-500/10 transition-all"
                >
                  <Briefcase className="w-4 h-4" />
                </a>
              </div>
            </motion.div>

            {/* Enterprise highlight */}
            <motion.div
              initial={{ opacity: 0, x: -20 }}
              animate={{ opacity: 1, x: 0 }}
              transition={{ duration: 0.4, delay: 0.4 }}
              className="glass-card p-5 border border-cyan-500/30 bg-cyan-500/5"
            >
              <Badge className="bg-cyan-500/15 text-cyan-300 border-cyan-500/30 mb-3 text-xs">
                Pro Max / Enterprise
              </Badge>
              <p className="text-sm text-zinc-300 leading-relaxed">
                Need unlimited processing, API access, and a dedicated account manager? Select
                <strong className="text-cyan-300"> Enterprise Inquiry</strong> in the form and our
                sales team will reach out with custom pricing.
              </p>
            </motion.div>
          </div>

          {/* Right: form */}
          <motion.div
            initial={{ opacity: 0, x: 20 }}
            animate={{ opacity: 1, x: 0 }}
            transition={{ duration: 0.5, delay: 0.1 }}
            className="md:col-span-3 glass-card p-8"
          >
            <h2 className="text-xl font-bold text-white mb-6">Send a Message</h2>
            <form onSubmit={handleSubmit} className="space-y-5">
              {statusMessage && (
                <div className="flex items-center gap-2 text-sm text-emerald-400 p-3 rounded-lg bg-emerald-500/10 border border-emerald-500/20">
                  <CheckCircle className="w-4 h-4 flex-shrink-0" />
                  {statusMessage}
                </div>
              )}
              {error && (
                <div className="flex items-center gap-2 text-sm text-red-400 p-3 rounded-lg bg-red-500/10 border border-red-500/20">
                  <AlertTriangle className="w-4 h-4 flex-shrink-0" />
                  {error}
                </div>
              )}
              <div className="grid sm:grid-cols-2 gap-4">
                <div className="space-y-2">
                  <Label htmlFor="name" className="text-zinc-300 text-sm">
                    Name
                  </Label>
                  <Input
                    id="name"
                    placeholder="Your full name"
                    required
                    value={name}
                    onChange={(event) => setName(event.target.value)}
                    className="bg-white/5 border-white/10 text-white placeholder:text-zinc-600 focus:border-violet-500/50"
                  />
                </div>
                <div className="space-y-2">
                  <Label htmlFor="email" className="text-zinc-300 text-sm">
                    Email
                  </Label>
                  <Input
                    id="email"
                    type="email"
                    placeholder="you@example.com"
                    required
                    value={email}
                    onChange={(event) => setEmail(event.target.value)}
                    className="bg-white/5 border-white/10 text-white placeholder:text-zinc-600 focus:border-violet-500/50"
                  />
                </div>
              </div>

              <div className="space-y-2">
                <Label className="text-zinc-300 text-sm">Subject</Label>
                <Select value={subject} onValueChange={(value) => setSubject(value ?? "")}>
                  <SelectTrigger className="bg-white/5 border-white/10 text-white focus:border-violet-500/50">
                    <SelectValue placeholder="Select a subject" />
                  </SelectTrigger>
                  <SelectContent className="bg-zinc-900 border-white/10">
                    <SelectItem value="support">Support</SelectItem>
                    <SelectItem value="sales">Sales</SelectItem>
                    <SelectItem value="enterprise">Enterprise Inquiry</SelectItem>
                    <SelectItem value="partnership">Partnership</SelectItem>
                    <SelectItem value="other">Other</SelectItem>
                  </SelectContent>
                </Select>
              </div>

              <div className="space-y-2">
                <Label htmlFor="message" className="text-zinc-300 text-sm">
                  Message
                </Label>
                <Textarea
                  id="message"
                  placeholder="Tell us how we can help..."
                  rows={5}
                  required
                  value={message}
                  onChange={(event) => setMessage(event.target.value)}
                  className="bg-white/5 border-white/10 text-white placeholder:text-zinc-600 focus:border-violet-500/50 resize-none"
                />
              </div>

              <Button
                type="submit"
                size="lg"
                disabled={submitting}
                className="w-full bg-gradient-to-r from-violet-600 to-violet-500 hover:from-violet-500 hover:to-violet-400 text-white border-0 shadow-lg shadow-violet-900/30"
              >
                {submitting ? <Loader2 className="w-4 h-4 mr-2 animate-spin" /> : <Send className="w-4 h-4 mr-2" />}
                {submitting ? "Sending..." : "Send Message"}
              </Button>

              <p className="text-xs text-zinc-600 text-center">
                We typically respond within 24 hours on business days.
              </p>
            </form>
          </motion.div>
        </div>
      </div>
    </div>
  );
}
