'use client'

import React from 'react'
import {
  Table,
  TableBody,
  TableCell,
  TableHeader,
  TableHead,
  TableRow,
} from '@/components/shadcn/ui/table'
import { TourPackageProduct } from '@/lib/types/tour-package'
import { useCurrency } from '@/lib/hooks/useCurrency'
import { useAppSelector } from '@/lib/redux-hooks'
import { capitalize } from '@/lib/utils/formatters/string'

interface PriceTableProps {
  priceType: TourPackageProduct['price_type']
  prices: TourPackageProduct['prices']
}

const PriceTable: React.FC<PriceTableProps> = ({ priceType, prices }) => {
  const { format } = useCurrency()
  const merchant = useAppSelector((state) => state.merchant)
  const currency = useAppSelector((state) => state.currency)

  function getSubLabel(min: number, max?: number) {
    if (max) {
      return `Age ${min} - ${max}`
    } else {
      return `Age ${min} or above`
    }
  }

  return (
    <div>
      <h3 className="heading-3 mt-4">Prices</h3>

      <Table className="mt-4">
        <TableHeader>
          <TableRow>
            {priceType === 'tiered' ? (
              <TableHead className="border text-sm sm:text-base">
                Total Pax
              </TableHead>
            ) : (
              <TableHead className="border text-sm sm:text-base">
                Category
              </TableHead>
            )}
            <TableHead className="border text-sm sm:text-base">Price</TableHead>
          </TableRow>
        </TableHeader>
        <TableBody>
          {prices
            .sort((priceA, priceB) => priceA.min_pax - priceB.min_pax)
            .map((price) =>
              price.category === 'adult' ? (
                <TableRow key={price.id}>
                  {/* Category */}
                  {priceType === 'flat' && (
                    <TableCell className="border text-sm sm:text-base">
                      {capitalize(price.category)}
                    </TableCell>
                  )}

                  {/* Total Pax */}
                  {priceType === 'tiered' && (
                    <TableCell className="border text-sm sm:text-base">
                      {price.pax_type === 'single'
                        ? `${price.min_pax} pax`
                        : price.pax_type === 'range' && price.max_pax === 0
                          ? `${price.min_pax} pax and above`
                          : `${price.min_pax} - ${price.max_pax} pax`}
                    </TableCell>
                  )}

                  {/* Price */}
                  <TableCell className="border">
                    <div className="line-through text-red-500 text-xs">
                      {format(price.strike_through_price, currency.code)} / pax
                    </div>
                    <span className="text-sm sm:text-base">
                      {price.price
                        ? format(price.price, currency.code) + ' / pax'
                        : 'N/A'}
                    </span>
                  </TableCell>
                </TableRow>
              ) : (
                <TableRow key={price.id}>
                  {/* Category / Total Pax */}
                  <TableCell className="border text-sm sm:text-base">
                    Child (
                    {getSubLabel(
                      merchant.passenger_age_group?.child?.min,
                      merchant.passenger_age_group?.child?.max,
                    )}
                    )
                  </TableCell>

                  {/* Price */}
                  <TableCell className="border">
                    <div className="line-through text-red-500 text-xs">
                      {format(price.strike_through_price, currency.code)} / pax
                    </div>
                    <span className="text-sm sm:text-base">
                      {price.price
                        ? format(price.price, currency.code) + ' / pax'
                        : 'N/A'}
                    </span>
                  </TableCell>
                </TableRow>
              ),
            )}
        </TableBody>
      </Table>
    </div>
  )
}

export default PriceTable
