import style from '../styles/information.module.css'

import React from 'react'
import dynamic from 'next/dynamic'
import Link from 'next/link'
import { useQuery } from 'react-query'

import { IProductDetail } from '~/types/product'
import { IProductVariantSLA } from '~/types/productVariant'
import { createSlug } from '~/libs/slug'
import { getVariantSLA } from '~/services/productVariant'
import { formatToShortText } from '~/libs/formatNumber'

const Skeleton = dynamic(() => import('@chakra-ui/react').then((mod) => mod.Skeleton))

type Props = {
  product: IProductDetail
}

const Information = ({ product }: Props) => {
  const querySLA = useQuery<IProductVariantSLA>({
    queryKey: ['variant-sla', product.variants[0].id],
    queryFn: () => getVariantSLA(product.variants[0].id),
    enabled: product.variant_total === 1
  })

  const sla = querySLA?.data

  return (
    <div className={style['wrapper']}>
      <div className={style['row']}>
        <div className={style['label']}>SKU Number</div>
        <div className={style['value']}>SKU-{product.id}</div>
      </div>
      <div className={style['row']}>
        <div className={style['label']}>Manufacture</div>
        <div className={style['value']}>
          {!!product.manufacture.name ? (
            <Link
              href={createSlug('/shop/brands/', product.manufacture.name, product.manufacture.id.toString())}
              className='text-danger-500 hover:underline'
            >
              {product.manufacture.name}
            </Link>
          ) : '-'}
        </div>
      </div>
      <div className={style['row']}>
        <div className={style['label']}>Terjual</div>
        <div className={style['value']}>{product.qty_sold > 0 ? formatToShortText(product.qty_sold) : '-'}</div>
      </div>
    </div>
  )
}

export default Information