'use client'
import {useForm} from 'react-hook-form'
import Center from '@/components/composites/purchase/long/center'
import {Form} from '@/components/ui/form'
import * as z from 'zod'
import {zodResolver} from '@hookform/resolvers/zod'
import {ProductItem} from '@/actions/product'
import {parsePurchaseSkuList} from '../shared/sku'
import {PurchaseSidePanel} from '../shared/side-panel'

const schema = z.object({
  type: z.enum(['1', '2']).default('2'),
  live: z.string(),
  quota: z.number().min(500, '购买数量不能少于 500 个'),
  expire: z.string(),
  daily_limit: z.number().min(100, '每日限额不能少于 100 个'),
  pay_type: z.enum(['wechat', 'alipay', 'balance']),
})
export type Schema = z.infer<typeof schema>

export default function LongForm({skuList}: {skuList: ProductItem['skus']}) {
  const {priceMap, liveList, expireList} = parsePurchaseSkuList('long', skuList)

  const form = useForm<Schema>({
    resolver: zodResolver(schema),
    defaultValues: {
      type: '2', // 默认为包量套餐
      live: liveList[0], // 分钟
      expire: '0', // 天
      quota: 500,
      daily_limit: 100,
      pay_type: 'balance', // 余额支付
    },
  })

  return (
    <Form form={form} className="flex flex-col lg:flex-row gap-4">
      <Center {...{liveList, priceMap, expireList}}/>
      <PurchaseSidePanel kind="long"/>
    </Form>
  )
}
