"use client";

import { useState } from "react";
import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card";
import { Badge } from "@/components/ui/badge";
import { Button } from "@/components/ui/button";
import { Input } from "@/components/ui/input";
import {
  Dialog,
  DialogContent,
  DialogDescription,
  DialogHeader,
  DialogTitle,
  DialogTrigger,
} from "@/components/ui/dialog";
import {
  AlertDialog,
  AlertDialogAction,
  AlertDialogCancel,
  AlertDialogContent,
  AlertDialogDescription,
  AlertDialogFooter,
  AlertDialogHeader,
  AlertDialogTitle,
  AlertDialogTrigger,
} from "@/components/ui/alert-dialog";
import {
  Hotel,
  Calendar,
  MapPin,
  Users,
  Search,
  Filter,
  IndianRupee,
  Eye,
  FileText,
  X,
  Bed,
  Phone,
  Mail,
} from "lucide-react";
import { cn } from "@/lib/utils";
import Link from "next/link";
import { useToastEnhanced } from "@/hooks/use-toast-enhanced";

const statusTabs = [
  { id: "all", label: "All Bookings" },
  { id: "upcoming", label: "Upcoming" },
  { id: "completed", label: "Completed" },
  { id: "cancelled", label: "Cancelled" },
];

const mockBookings = [
  {
    id: "HB001",
    hotel: "Grand Hotel Mumbai",
    location: "Mumbai, Maharashtra",
    checkIn: "Dec 10, 2024",
    checkOut: "Dec 12, 2024",
    guests: "2 Adults",
    status: "completed",
    amount: 8500,
    roomType: "Deluxe Suite",
    roomCount: 1,
    phone: "+91 22 1234 5678",
    email: "info@grandhotelmumbai.com",
    address: "Marine Drive, Mumbai, Maharashtra 400020",
    amenities: [
      "Free WiFi",
      "Swimming Pool",
      "Gym",
      "Restaurant",
      "Room Service",
    ],
    bookingDate: "Nov 25, 2024",
    confirmationNumber: "GHM-2024-001",
  },
  {
    id: "HB002",
    hotel: "Pune Plaza Hotel",
    location: "Pune, Maharashtra",
    checkIn: "Dec 20, 2024",
    checkOut: "Dec 22, 2024",
    guests: "2 Adults, 1 Child",
    status: "upcoming",
    amount: 6200,
    roomType: "Family Room",
    roomCount: 1,
    phone: "+91 20 1234 5678",
    email: "info@puneplaza.com",
    address: "Koregaon Park, Pune, Maharashtra 411001",
    amenities: ["Free WiFi", "Breakfast Included", "Parking", "Kids Play Area"],
    bookingDate: "Dec 5, 2024",
    confirmationNumber: "PPH-2024-002",
  },
  {
    id: "HB003",
    hotel: "Nashik Resort",
    location: "Nashik, Maharashtra",
    checkIn: "Nov 5, 2024",
    checkOut: "Nov 7, 2024",
    guests: "1 Adult",
    status: "cancelled",
    amount: 4500,
    roomType: "Standard Room",
    roomCount: 1,
    phone: "+91 253 1234 567",
    email: "info@nashikresort.com",
    address: "Nashik-Trimbak Road, Nashik, Maharashtra 422003",
    amenities: ["Free WiFi", "Garden View", "Restaurant"],
    bookingDate: "Oct 20, 2024",
    confirmationNumber: "NR-2024-003",
  },
];

const statusConfig = {
  all: {
    badgeClass: "bg-gray-50 text-gray-600 border-gray-200",
    label: "All",
  },
  upcoming: {
    badgeClass: "bg-blue-50 text-blue-600 border-blue-200",
    label: "Upcoming",
  },
  completed: {
    badgeClass: "bg-emerald-50 text-emerald-600 border-emerald-200",
    label: "Completed",
  },
  cancelled: {
    badgeClass: "bg-red-50 text-red-600 border-red-200",
    label: "Cancelled",
  },
};

export function HotelBookingsContent() {
  const [activeStatus, setActiveStatus] = useState("all");
  const [searchQuery, setSearchQuery] = useState("");
  const toast = useToastEnhanced();

  const filteredBookings =
    activeStatus === "all"
      ? mockBookings
      : mockBookings.filter((b) => b.status === activeStatus);

  const searchedBookings = filteredBookings.filter(
    (booking) =>
      booking.id.toLowerCase().includes(searchQuery.toLowerCase()) ||
      booking.hotel.toLowerCase().includes(searchQuery.toLowerCase()) ||
      booking.location.toLowerCase().includes(searchQuery.toLowerCase())
  );

  const handleCancellation = (bookingId: string) => {
    toast.success({
      title: "Cancellation Requested",
      description: `Your cancellation request for booking ${bookingId} has been submitted.`,
    });
  };

  return (
    <div className="space-y-6">
      <header>
        <h1 className="text-3xl font-bold text-balance">Hotel Bookings</h1>
        <p className="text-muted-foreground mt-2">
          View and manage your hotel booking history
        </p>
      </header>

      <div className="flex flex-wrap gap-2">
        {statusTabs.map((status) => {
          const isActive = activeStatus === status.id;
          const count =
            status.id === "all"
              ? mockBookings.length
              : mockBookings.filter((b) => b.status === status.id).length;
          return (
            <button
              key={status.id}
              onClick={() => setActiveStatus(status.id)}
              className={cn(
                "flex items-center gap-2 px-4 py-2 rounded-lg font-medium transition-all",
                isActive
                  ? "bg-primary text-primary-foreground shadow-md"
                  : "bg-white dark:bg-gray-800 text-muted-foreground hover:text-foreground hover:bg-muted border border-border/50"
              )}
            >
              {status.label}
              <Badge variant="outline" className="ml-1">
                {count}
              </Badge>
            </button>
          );
        })}
      </div>

      <div className="flex items-center gap-2">
        <div className="relative flex-1 max-w-md">
          <Search className="absolute left-3 top-1/2 -translate-y-1/2 h-4 w-4 text-muted-foreground" />
          <Input
            placeholder="Search bookings by ID, hotel, or location..."
            value={searchQuery}
            onChange={(e) => setSearchQuery(e.target.value)}
            className="pl-9"
          />
        </div>
        <Button variant="outline" size="icon">
          <Filter className="h-4 w-4" />
        </Button>
      </div>

      <div className="grid gap-4">
        {searchedBookings.length === 0 ? (
          <Card className="shadow-md">
            <CardContent className="flex flex-col items-center justify-center py-16">
              <Hotel className="h-16 w-16 text-muted-foreground mb-4" />
              <p className="text-lg font-semibold mb-2">No bookings found</p>
              <p className="text-muted-foreground text-center">
                {searchQuery
                  ? "Try adjusting your search"
                  : "You don't have any hotel bookings yet"}
              </p>
            </CardContent>
          </Card>
        ) : (
          searchedBookings.map((booking) => {
            const config =
              statusConfig[booking.status as keyof typeof statusConfig];
            return (
              <Card
                key={booking.id}
                className="shadow-md hover:shadow-lg transition-all border-border/50"
              >
                <CardHeader>
                  <div className="flex items-start justify-between">
                    <div className="flex items-center gap-3">
                      <div className="p-3 rounded-lg bg-gradient-to-br from-purple-500 to-pink-600 shadow-sm">
                        <Hotel
                          className="h-6 w-6 text-white"
                          aria-hidden="true"
                        />
                      </div>
                      <div>
                        <CardTitle className="text-lg">
                          Booking ID: {booking.id}
                        </CardTitle>
                        <p className="text-sm text-muted-foreground mt-1">
                          {booking.hotel}
                        </p>
                      </div>
                    </div>
                    <Badge
                      variant="outline"
                      className={cn("shadow-sm", config.badgeClass)}
                    >
                      {config.label}
                    </Badge>
                  </div>
                </CardHeader>
                <CardContent>
                  <div className="grid md:grid-cols-2 lg:grid-cols-4 gap-4">
                    <div className="flex items-start gap-2">
                      <MapPin
                        className="h-5 w-5 text-muted-foreground mt-0.5 shrink-0"
                        aria-hidden="true"
                      />
                      <div>
                        <p className="text-sm font-medium">Location</p>
                        <p className="text-sm text-muted-foreground">
                          {booking.location}
                        </p>
                      </div>
                    </div>
                    <div className="flex items-start gap-2">
                      <Calendar
                        className="h-5 w-5 text-muted-foreground mt-0.5 shrink-0"
                        aria-hidden="true"
                      />
                      <div>
                        <p className="text-sm font-medium">Check In</p>
                        <p className="text-sm text-muted-foreground">
                          {booking.checkIn}
                        </p>
                      </div>
                    </div>
                    <div className="flex items-start gap-2">
                      <Calendar
                        className="h-5 w-5 text-muted-foreground mt-0.5 shrink-0"
                        aria-hidden="true"
                      />
                      <div>
                        <p className="text-sm font-medium">Check Out</p>
                        <p className="text-sm text-muted-foreground">
                          {booking.checkOut}
                        </p>
                      </div>
                    </div>
                    <div className="flex items-start gap-2">
                      <Users
                        className="h-5 w-5 text-muted-foreground mt-0.5 shrink-0"
                        aria-hidden="true"
                      />
                      <div>
                        <p className="text-sm font-medium">Guests</p>
                        <p className="text-sm text-muted-foreground">
                          {booking.guests}
                        </p>
                      </div>
                    </div>
                  </div>
                  <div className="mt-4 pt-4 border-t border-border/50 flex flex-wrap justify-between items-center gap-3">
                    <div className="flex items-center gap-2">
                      <IndianRupee className="h-5 w-5 text-amber-600" />
                      <p className="text-lg font-bold">
                        ₹{booking.amount.toLocaleString("en-IN")}
                      </p>
                    </div>
                    <div className="flex flex-wrap gap-2">
                      <Button
                        variant="outline"
                        size="sm"
                        asChild
                        className="gap-2 bg-transparent"
                      >
                        <Link href={`/user/invoices/${booking.id}`}>
                          <FileText className="h-4 w-4" />
                          Invoice
                        </Link>
                      </Button>
                      <Dialog>
                        <DialogTrigger asChild>
                          <Button
                            variant="outline"
                            size="sm"
                            className="gap-2 bg-transparent"
                          >
                            <Eye className="h-4 w-4" />
                            View Details
                          </Button>
                        </DialogTrigger>
                        <DialogContent className="max-w-2xl max-h-[85vh] overflow-y-auto">
                          <DialogHeader>
                            <DialogTitle className="text-2xl">
                              Booking Details
                            </DialogTitle>
                            <DialogDescription>
                              Complete information about your hotel booking
                            </DialogDescription>
                          </DialogHeader>
                          <div className="space-y-6">
                            <div className="grid grid-cols-2 gap-4 p-4 bg-muted/50 rounded-lg">
                              <div>
                                <p className="text-sm text-muted-foreground mb-1">
                                  Booking ID
                                </p>
                                <p className="font-semibold">{booking.id}</p>
                              </div>
                              <div>
                                <p className="text-sm text-muted-foreground mb-1">
                                  Confirmation Number
                                </p>
                                <p className="font-semibold">
                                  {booking.confirmationNumber}
                                </p>
                              </div>
                              <div>
                                <p className="text-sm text-muted-foreground mb-1">
                                  Booking Date
                                </p>
                                <p className="font-semibold">
                                  {booking.bookingDate}
                                </p>
                              </div>
                              <div>
                                <p className="text-sm text-muted-foreground mb-1">
                                  Status
                                </p>
                                <Badge
                                  variant="outline"
                                  className={config.badgeClass}
                                >
                                  {config.label}
                                </Badge>
                              </div>
                            </div>

                            <div>
                              <h3 className="font-semibold text-lg mb-3 flex items-center gap-2">
                                <Hotel className="h-5 w-5" />
                                Hotel Information
                              </h3>
                              <div className="space-y-3 p-4 border rounded-lg">
                                <div>
                                  <p className="text-sm text-muted-foreground">
                                    Hotel Name
                                  </p>
                                  <p className="font-medium">{booking.hotel}</p>
                                </div>
                                <div>
                                  <p className="text-sm text-muted-foreground">
                                    Address
                                  </p>
                                  <p className="font-medium">
                                    {booking.address}
                                  </p>
                                </div>
                                <div className="grid grid-cols-2 gap-4">
                                  <div>
                                    <p className="text-sm text-muted-foreground flex items-center gap-1">
                                      <Phone className="h-3 w-3" /> Phone
                                    </p>
                                    <p className="font-medium">
                                      {booking.phone}
                                    </p>
                                  </div>
                                  <div>
                                    <p className="text-sm text-muted-foreground flex items-center gap-1">
                                      <Mail className="h-3 w-3" /> Email
                                    </p>
                                    <p className="font-medium text-sm">
                                      {booking.email}
                                    </p>
                                  </div>
                                </div>
                              </div>
                            </div>

                            <div>
                              <h3 className="font-semibold text-lg mb-3 flex items-center gap-2">
                                <Bed className="h-5 w-5" />
                                Room Details
                              </h3>
                              <div className="space-y-3 p-4 border rounded-lg">
                                <div className="grid grid-cols-2 gap-4">
                                  <div>
                                    <p className="text-sm text-muted-foreground">
                                      Room Type
                                    </p>
                                    <p className="font-medium">
                                      {booking.roomType}
                                    </p>
                                  </div>
                                  <div>
                                    <p className="text-sm text-muted-foreground">
                                      Number of Rooms
                                    </p>
                                    <p className="font-medium">
                                      {booking.roomCount}
                                    </p>
                                  </div>
                                  <div>
                                    <p className="text-sm text-muted-foreground">
                                      Check-In
                                    </p>
                                    <p className="font-medium">
                                      {booking.checkIn}
                                    </p>
                                  </div>
                                  <div>
                                    <p className="text-sm text-muted-foreground">
                                      Check-Out
                                    </p>
                                    <p className="font-medium">
                                      {booking.checkOut}
                                    </p>
                                  </div>
                                  <div>
                                    <p className="text-sm text-muted-foreground">
                                      Guests
                                    </p>
                                    <p className="font-medium">
                                      {booking.guests}
                                    </p>
                                  </div>
                                  <div>
                                    <p className="text-sm text-muted-foreground">
                                      Total Amount
                                    </p>
                                    <p className="font-bold text-lg">
                                      ₹{booking.amount.toLocaleString("en-IN")}
                                    </p>
                                  </div>
                                </div>
                                <div>
                                  <p className="text-sm text-muted-foreground mb-2">
                                    Amenities
                                  </p>
                                  <div className="flex flex-wrap gap-2">
                                    {booking.amenities.map((amenity, index) => (
                                      <Badge key={index} variant="secondary">
                                        {amenity}
                                      </Badge>
                                    ))}
                                  </div>
                                </div>
                              </div>
                            </div>
                          </div>
                        </DialogContent>
                      </Dialog>
                      {booking.status === "upcoming" && (
                        <AlertDialog>
                          <AlertDialogTrigger asChild>
                            <Button
                              variant="outline"
                              size="sm"
                              className="gap-2 bg-transparent text-red-600 hover:text-red-700 hover:bg-red-50"
                            >
                              <X className="h-4 w-4" />
                              Cancel
                            </Button>
                          </AlertDialogTrigger>
                          <AlertDialogContent>
                            <AlertDialogHeader>
                              <AlertDialogTitle>
                                Cancel Booking
                              </AlertDialogTitle>
                              <AlertDialogDescription>
                                Are you sure you want to cancel booking{" "}
                                {booking.id}? This action cannot be undone.
                                Cancellation charges may apply as per the hotel
                                policy.
                              </AlertDialogDescription>
                            </AlertDialogHeader>
                            <AlertDialogFooter>
                              <AlertDialogCancel>
                                Keep Booking
                              </AlertDialogCancel>
                              <AlertDialogAction
                                onClick={() => handleCancellation(booking.id)}
                                className="bg-red-600 hover:bg-red-700"
                              >
                                Confirm Cancellation
                              </AlertDialogAction>
                            </AlertDialogFooter>
                          </AlertDialogContent>
                        </AlertDialog>
                      )}
                    </div>
                  </div>
                </CardContent>
              </Card>
            );
          })
        )}
      </div>
    </div>
  );
}
