'use client'
import {createContext} from 'react'
import {useForm, UseFormReturn} from 'react-hook-form'
import Center from '@/components/composites/purchase/long/center'
import Right from '@/components/composites/purchase/long/right'
import {Form} from '@/components/ui/form'
import * as z from 'zod'
import {zodResolver} from '@hookform/resolvers/zod'

// 定义表单验证架构
const schema = z.object({
  type: z.enum(['1', '2']).default('2'),
  live: z.enum(['1', '4', '8', '12', '24']),
  quota: z.number().min(500, '购买数量不能少于 500 个'),
  expire: z.enum(['7', '15', '30', '90', '180', '365']),
  daily_limit: z.number().min(100, '每日限额不能少于 100 个'),
  pay_type: z.enum(['wechat', 'alipay', 'balance']),
})

// 从架构中推断类型
export type Schema = z.infer<typeof schema>

type PurchaseFormContextType = {
  form: UseFormReturn<Schema>
  onSubmit?: () => void
}

export const LongFormContext = createContext<PurchaseFormContextType | undefined>(undefined)

export default function LongForm() {
  const form = useForm<Schema>({
    resolver: zodResolver(schema),
    defaultValues: {
      type: '2', // 默认为包量套餐
      live: '1', // 小时
      quota: 500,
      expire: '30', // 天
      daily_limit: 100,
      pay_type: 'balance', // 余额支付
    },
  })

  return (
    <Form form={form} className={`bg-white rounded-lg flex flex-row`}>
      <LongFormContext.Provider value={{form}}>
        <Center/>
        <Right/>
      </LongFormContext.Provider>
    </Form>
  )
}

