"use client";

import type React from "react";
import Link from "next/link";
import {
  ArrowLeft,
  MapPin,
  Phone,
  Mail,
  Clock,
  Send,
  Facebook,
  Instagram,
} from "lucide-react";
import { FaWhatsapp } from "react-icons/fa";
import { Button } from "@/components/ui/button";
import { submitContactUsAction } from "@/features/contact-us/actions/contact-us-actions";
import { Card, CardContent } from "@/components/ui/card";
import { Input } from "@/components/ui/input";
import { Textarea } from "@/components/ui/textarea";
import { Label } from "@/components/ui/label";
import { useState } from "react";
import { useToastEnhanced } from "@/hooks/use-toast";
import { FOOTER } from "@/lib/strings/footer";
import { CONTACT_US_PAGE } from "@/lib/strings/contact-us-page";

export default function ContactUsPage() {
  const { success, error: showError } = useToastEnhanced();
  const [isSubmitting, setIsSubmitting] = useState(false);
  const [formData, setFormData] = useState({
    name: "",
    email: "",
    phone: "",
    subject: "",
    message: "",
  });

  const handleInputChange = (
    e: React.ChangeEvent<HTMLInputElement | HTMLTextAreaElement>
  ) => {
    const { name, value } = e.target;
    setFormData((prev) => ({ ...prev, [name]: value }));
  };

  const handleSubmit = async (e: React.FormEvent) => {
    e.preventDefault();
    setIsSubmitting(true);

    try {
      const result = await submitContactUsAction({
        full_name: formData.name,
        email: formData.email,
        contact: formData.phone,
        subject: formData.subject,
        message: formData.message,
      });

      if (result.success) {
        success({
          title: CONTACT_US_PAGE.TOAST_SUCCESS_TITLE,
          description: CONTACT_US_PAGE.TOAST_SUCCESS_DESCRIPTION,
        });
        setFormData({
          name: "",
          email: "",
          phone: "",
          subject: "",
          message: "",
        });
      } else {
        showError({
          title: CONTACT_US_PAGE.TOAST_SUBMISSION_FAILED_TITLE,
          description:
            result.error || CONTACT_US_PAGE.TOAST_SUBMISSION_FAILED_FALLBACK,
        });
      }
    } catch (err) {
      showError({
        title: CONTACT_US_PAGE.TOAST_ERROR_TITLE,
        description: CONTACT_US_PAGE.TOAST_UNEXPECTED_ERROR,
      });
    } finally {
      setIsSubmitting(false);
    }
  };

  const contactInfo = [
    {
      icon: MapPin,
      title: FOOTER.CONTACT_CARD_VISIT_TITLE,
      content: FOOTER.ADDRESS_LINE_1,
      subContent: FOOTER.ADDRESS_LINE_2,
    },
    {
      icon: Phone,
      title: FOOTER.CONTACT_CARD_CALL_TITLE,
      content: FOOTER.PHONE_PRIMARY,
      subContent: FOOTER.PHONE_SECONDARY,
    },
    {
      icon: Mail,
      title: FOOTER.CONTACT_CARD_EMAIL_TITLE,
      content: FOOTER.EMAIL_INFO,
      subContent: FOOTER.EMAIL_BOOKING,
    },
    {
      icon: Clock,
      title: FOOTER.CONTACT_CARD_HOURS_TITLE,
      content: FOOTER.WORKING_HOURS_LINE_1,
      subContent: FOOTER.WORKING_HOURS_LINE_2,
    },
  ];

  const socialLinks = [
    {
      icon: Facebook,
      href: FOOTER.SOCIAL_FACEBOOK_HREF,
      label: FOOTER.SOCIAL_FACEBOOK_LABEL,
    },
    {
      icon: Instagram,
      href: FOOTER.SOCIAL_INSTAGRAM_HREF,
      label: FOOTER.SOCIAL_INSTAGRAM_LABEL,
    },
    {
      icon: FaWhatsapp,
      href: FOOTER.SOCIAL_WHATSAPP_HREF,
      label: FOOTER.SOCIAL_WHATSAPP_LABEL,
    },
  ];

  return (
    <main className="min-h-screen bg-gradient-to-br from-background via-background to-muted/20">
      {/* Hero Section */}
      <section className="relative py-16 md:py-20 bg-gradient-to-br from-secondary-foreground/5 via-secondary-foreground/10 to-secondary-foreground/10">
        <div className="mx-auto max-w-7xl px-4 md:px-6 lg:px-8">
          <Link href="/">
            <Button variant="ghost" size="sm" className="mb-8">
              <ArrowLeft className="mr-2 h-4 w-4" />
              {CONTACT_US_PAGE.BACK_TO_HOME}
            </Button>
          </Link>

          <div className="text-center max-w-3xl mx-auto">
            <h1 className="text-4xl md:text-5xl lg:text-6xl font-bold mb-6 text-balance">
              {CONTACT_US_PAGE.HERO_TITLE_BEFORE}
              <span className="frontend-text-gradient">
                {CONTACT_US_PAGE.HERO_TITLE_ACCENT}
              </span>
            </h1>
            <p className="text-lg md:text-xl text-foreground/80 leading-relaxed">
              {CONTACT_US_PAGE.HERO_SUBTITLE}
            </p>
          </div>
        </div>
      </section>

      {/* Contact Cards Section */}
      <section className="py-12 -mt-8">
        <div className="mx-auto max-w-7xl px-4 md:px-6 lg:px-8">
          <div className="grid sm:grid-cols-2 lg:grid-cols-4 gap-6">
            {contactInfo.map((info, index) => {
              const Icon = info.icon;
              return (
                <Card
                  key={index}
                  className="border-0 shadow-xl hover:shadow-2xl transition-all duration-300 rounded-2xl group"
                >
                  <CardContent className="p-6 text-center">
                    <div className="h-14 w-14 rounded-xl bg-gradient-to-br from-primary/20 to-primary/10 flex items-center justify-center mx-auto mb-4 group-hover:scale-110 transition-transform">
                      <Icon className="h-7 w-7 text-primary" />
                    </div>
                    <h3 className="font-bold text-lg mb-2">{info.title}</h3>
                    <p className="text-foreground font-medium mb-1">
                      {info.content}
                    </p>
                    <p className="text-sm text-muted-foreground">
                      {info.subContent}
                    </p>
                  </CardContent>
                </Card>
              );
            })}
          </div>
        </div>
      </section>

      {/* Form and Map Section */}
      <section className="py-16 md:py-20">
        <div className="mx-auto max-w-7xl px-4 md:px-6 lg:px-8">
          <div className="space-y-8">
            <div className="grid items-stretch gap-8 lg:grid-cols-2">
              {/* Contact Form */}
              <Card className="h-full border-0 shadow-xl rounded-2xl overflow-hidden">
                <CardContent className="p-8 md:p-10">
                  <h2 className="text-3xl font-bold mb-2">
                    {CONTACT_US_PAGE.FORM_SECTION_TITLE}
                  </h2>
                  <p className="text-muted-foreground mb-8">
                    {CONTACT_US_PAGE.FORM_SECTION_INTRO}
                  </p>
                  <p className="text-muted-foreground mb-8">
                    {CONTACT_US_PAGE.FORM_SECTION_SECOND_LINE}
                  </p>

                  <form onSubmit={handleSubmit} className="space-y-6">
                    <div className="grid sm:grid-cols-2 gap-6">
                      <div className="space-y-2">
                        <Label htmlFor="name">
                          {CONTACT_US_PAGE.LABEL_FULL_NAME}
                        </Label>
                        <Input
                          id="name"
                          name="name"
                          placeholder={CONTACT_US_PAGE.PLACEHOLDER_FULL_NAME}
                          value={formData.name}
                          onChange={handleInputChange}
                          required
                          className="h-12"
                        />
                      </div>
                      <div className="space-y-2">
                        <Label htmlFor="email">
                          {CONTACT_US_PAGE.LABEL_EMAIL}
                        </Label>
                        <Input
                          id="email"
                          name="email"
                          type="email"
                          placeholder={CONTACT_US_PAGE.PLACEHOLDER_EMAIL}
                          value={formData.email}
                          onChange={handleInputChange}
                          required
                          className="h-12"
                        />
                      </div>
                    </div>

                    <div className="grid sm:grid-cols-2 gap-6">
                      <div className="space-y-2">
                        <Label htmlFor="phone">
                          {CONTACT_US_PAGE.LABEL_PHONE}
                        </Label>
                        <Input
                          id="phone"
                          name="phone"
                          type="tel"
                          placeholder={CONTACT_US_PAGE.PLACEHOLDER_PHONE}
                          value={formData.phone}
                          onChange={handleInputChange}
                          className="h-12"
                        />
                      </div>
                      <div className="space-y-2">
                        <Label htmlFor="subject">
                          {CONTACT_US_PAGE.LABEL_SUBJECT}
                        </Label>
                        <Input
                          id="subject"
                          name="subject"
                          placeholder={CONTACT_US_PAGE.PLACEHOLDER_SUBJECT}
                          value={formData.subject}
                          onChange={handleInputChange}
                          required
                          className="h-12"
                        />
                      </div>
                    </div>

                    <div className="space-y-2">
                      <Label htmlFor="message">
                        {CONTACT_US_PAGE.LABEL_MESSAGE}
                      </Label>
                      <Textarea
                        id="message"
                        name="message"
                        placeholder={CONTACT_US_PAGE.PLACEHOLDER_MESSAGE}
                        value={formData.message}
                        onChange={handleInputChange}
                        required
                        rows={6}
                        className="resize-none"
                      />
                    </div>

                    <Button
                      type="submit"
                      size="lg"
                      disabled={isSubmitting}
                      className="w-full frontend-btn shadow-lg hover:shadow-xl transition-all"
                    >
                      {isSubmitting ? (
                        CONTACT_US_PAGE.SUBMIT_SENDING
                      ) : (
                        <>
                          {CONTACT_US_PAGE.SUBMIT_SEND_MESSAGE}
                          <Send className="ml-2 h-5 w-5" />
                        </>
                      )}
                    </Button>
                  </form>
                </CardContent>
              </Card>

              {/* Map */}
              <Card className="h-full shadow-2xl rounded-3xl overflow-hidden group border-0">
                <div className="relative h-full min-h-[620px] bg-muted">
                  <div className="absolute inset-0 bg-secondary-foreground/10 group-hover:bg-transparent transition-colors duration-500 pointer-events-none z-10" />
                  <iframe
                    src="https://www.google.com/maps/embed?pb=!1m18!1m12!1m3!1d877.6835100976912!2d77.32819615507817!3d28.366889353375154!2m3!1f0!2f0!3f0!3m2!1i1024!2i768!4f13.1!3m3!1m2!1s0x17053e5de4af321%3A0xd55848b0dac0f92e!2sAmeera%20Holidays!5e0!3m2!1sen!2sin!4v1773495409785!5m2!1sen!2sin%22"
                    width="100%"
                    height="100%"
                    style={{ border: 0 }}
                    allowFullScreen
                    loading="lazy"
                    referrerPolicy="no-referrer-when-downgrade"
                    title={CONTACT_US_PAGE.MAP_IFRAME_TITLE}
                    className="absolute inset-0 z-0 h-full w-full"
                  />
                  <div className="absolute bottom-4 left-4 z-20">
                    <div className="bg-white/95 backdrop-blur-md px-4 py-2 rounded-2xl shadow-xl border border-white/20">
                      <p className="text-[10px] font-bold text-primary uppercase tracking-[0.2em] mb-1">
                        {CONTACT_US_PAGE.MAP_OVERLAY_LABEL}
                      </p>
                      <p className="text-xs font-bold text-gray-800">
                        {CONTACT_US_PAGE.MAP_OVERLAY_SUBTITLE}
                      </p>
                    </div>
                  </div>
                </div>
              </Card>
            </div>

            <div className="grid gap-8 lg:grid-cols-2">
              {/* Social Links */}
              <Card className="border-0 shadow-xl rounded-2xl">
                <CardContent className="p-8">
                  <h3 className="text-2xl font-bold mb-2">
                    {CONTACT_US_PAGE.SOCIAL_SECTION_TITLE}
                  </h3>
                  <p className="text-muted-foreground mb-6">
                    {CONTACT_US_PAGE.SOCIAL_SECTION_DESCRIPTION}
                  </p>

                  <div className="flex gap-4">
                    {socialLinks.map((social, index) => {
                      const Icon = social.icon;
                      return (
                        <a
                          key={index}
                          href={social.href}
                          target="_blank"
                          rel="noopener noreferrer"
                          aria-label={social.label}
                          className="h-12 w-12 rounded-xl bg-gradient-to-br from-primary/20 to-primary/10 flex items-center justify-center hover:scale-110 transition-transform shadow-md hover:shadow-lg"
                        >
                          <Icon className="h-5 w-5 text-primary" />
                        </a>
                      );
                    })}
                  </div>
                </CardContent>
              </Card>

              {/* Quick Support */}
              <Card className="border-0 shadow-xl rounded-2xl bg-gradient-to-br from-secondary-foreground/5 via-secondary-foreground/10 to-secondary-foreground/10">
                <CardContent className="p-8">
                  <h3 className="text-2xl font-bold mb-2">
                    {CONTACT_US_PAGE.SUPPORT_CARD_TITLE}
                  </h3>
                  <p className="text-foreground/80 mb-6">
                    {CONTACT_US_PAGE.SUPPORT_CARD_DESCRIPTION}
                  </p>
                  <div className="flex flex-col sm:flex-row gap-4">
                    <a href="tel:+918505999770" className="flex-1">
                      <Button
                        size="lg"
                        className="w-full frontend-btn shadow-lg hover:shadow-xl transition-all"
                      >
                        <Phone className="mr-2 h-5 w-5" />
                        {CONTACT_US_PAGE.SUPPORT_CALL_NOW}
                      </Button>
                    </a>
                    <a href="mailto:info@ameeraholidays.com" className="flex-1">
                      <Button
                        size="lg"
                        variant="outline"
                        className="w-full bg-secondary-foreground/20 "
                      >
                        <Mail className="mr-2 h-5 w-5" />
                        {CONTACT_US_PAGE.SUPPORT_EMAIL_US}
                      </Button>
                    </a>
                  </div>
                </CardContent>
              </Card>
            </div>
          </div>
        </div>
      </section>
    </main>
  );
}
