'use client'

import ProductCard from '@/components/card/ProductCard'
import ProductCardSkeleton from '@/components/skeletons/ProductCardSkeleton'
import { useAppSelector } from '@/lib/redux-hooks'
import React, { useEffect, useState } from 'react'
import Carousel from '@/components/Carousel'
import { ArrowUpRight } from 'lucide-react'
import * as TourPackageService from '@/lib/services/tour-package-service'
import { TourPackageProduct } from '@/lib/types/tour-package'
import Link from 'next/link'

const PackageList: React.FC = () => {
  const merchant = useAppSelector((state) => state.merchant)
  const [loading, setLoading] = useState(true)
  const [tourPackageProducts, setTourPackageProducts] = useState<
    TourPackageProduct[]
  >([])

  useEffect(() => {
    if (!merchant?.id) return

    const fetchData = async () => {
      try {
        const tourPackageProducts =
          await TourPackageService.getTourPackageProducts(merchant.id)

        setTourPackageProducts(tourPackageProducts)
        setLoading(false)
      } catch (e) {
        console.error(e)
      }
    }

    fetchData()
  }, [merchant])

  return (
    <section className="max-w-7xl w-full px-5 sm:px-10 py-10 mx-auto">
      <div className="flex lg:items-start lg:flex-row flex-col justify-between mb-10">
        <div>
          <h2 className="text-xl lg:text-2xl font-semibold">
            Special Packages
          </h2>
          <p className="text-slate-500 mt-2 max-w-2xl">
            Discover the ultimate convenience with our all-inclusive packages,
            covering everything from fast boats to transport and more. Enjoy a
            seamless travel experience tailored just for you!
          </p>
        </div>

        <Link
          href={`/${merchant?.username}/packages`}
          className="text-base font-semibold text-blue-800 mt-2 lg:mt-0"
        >
          View All
          <ArrowUpRight className="w-4 h-4 inline-block ml-1" />
        </Link>
      </div>

      <Carousel
        items={tourPackageProducts}
        renderItem={(tourPackageProduct: TourPackageProduct) => (
          <ProductCard
            product={{
              name: tourPackageProduct.name,
              detail_url: `/${merchant?.username}/packages/${tourPackageProduct.slug}`,
              featured_image: tourPackageProduct.featured_image,
              min_price: tourPackageProduct.min_price,
              rating: tourPackageProduct.rating,
              review_count: tourPackageProduct.review_count,
              badge: tourPackageProduct.badge,
              units_sold: tourPackageProduct.units_sold,
            }}
          />
        )}
        loading={loading}
        renderSkeleton={() => <ProductCardSkeleton />}
        itemWidth={280}
        gap={24}
        autoPlayInterval={3000}
      />
    </section>
  )
}

export default PackageList
