'use client'

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

interface PriceTableProps {
  prices: ActivityProduct['prices']
}

const PriceTable: React.FC<PriceTableProps> = ({ 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>
      <h2 className="heading-3 mt-4">Prices</h2>

      <Table className="mt-4">
        <TableHeader>
          <TableRow>
            <TableHead className="border text-sm sm:text-base">
              Category
            </TableHead>
            <TableHead className="border text-sm sm:text-base">Price</TableHead>
          </TableRow>
        </TableHeader>
        <TableBody>
          {prices.map((price) => (
            <TableRow key={price.id}>
              <TableCell className="border text-sm sm:text-base">
                {capitalize(price.category)} (
                {getSubLabel(
                  merchant.passenger_age_group?.[price.category]?.min,
                  merchant.passenger_age_group?.[price.category]?.max,
                )}
                )
              </TableCell>
              <TableCell className="border">
                <div className="line-through text-red-500 text-xs">
                  {format(price.strike_through_price, currency.code)}
                </div>
                <span className="text-sm sm:text-base">
                  {price.price ? format(price.price, currency.code) : 'N/A'}
                </span>
              </TableCell>
            </TableRow>
          ))}
        </TableBody>
      </Table>
    </div>
  )
}

export default PriceTable
