import { useForm } from 'react-hook-form'
import { zodResolver } from '@hookform/resolvers/zod'
import { ProcessorLinkCreateSchema } from '@lunarfront/shared/schemas'
import type { ProcessorLinkCreateInput } from '@lunarfront/shared/schemas'
import { Button } from '@/components/ui/button'
import { Input } from '@/components/ui/input'
import { Label } from '@/components/ui/label'
import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from '@/components/ui/select'

interface ProcessorLinkFormProps {
  accountId: string
  onSubmit: (data: Record<string, unknown>) => void
  loading?: boolean
}

export function ProcessorLinkForm({ accountId, onSubmit, loading }: ProcessorLinkFormProps) {
  const {
    register,
    handleSubmit,
    setValue,
    watch,
    formState: { errors },
  } = useForm<ProcessorLinkCreateInput>({
    resolver: zodResolver(ProcessorLinkCreateSchema),
    defaultValues: {
      accountId,
      processor: 'stripe',
      processorCustomerId: '',
    },
  })

  const processor = watch('processor')

  return (
    <form onSubmit={handleSubmit(onSubmit)} className="space-y-4">
      <div className="space-y-2">
        <Label>Processor</Label>
        <Select
          value={processor}
          onValueChange={(v) => setValue('processor', v as 'stripe' | 'global_payments')}
        >
          <SelectTrigger>
            <SelectValue />
          </SelectTrigger>
          <SelectContent>
            <SelectItem value="stripe">Stripe</SelectItem>
            <SelectItem value="global_payments">Global Payments</SelectItem>
          </SelectContent>
        </Select>
      </div>
      <div className="space-y-2">
        <Label htmlFor="customerId">Customer ID *</Label>
        <Input id="customerId" {...register('processorCustomerId')} placeholder="cus_..." />
        {errors.processorCustomerId && (
          <p className="text-sm text-destructive">{errors.processorCustomerId.message}</p>
        )}
      </div>
      <Button type="submit" disabled={loading}>
        {loading ? 'Saving...' : 'Add Processor Link'}
      </Button>
    </form>
  )
}
