import { describe, it, expect } from 'vitest'
import {
  FormFieldSchema,
  FormSchemaSchema,
  validateFormSchema,
  validateFormField
} from './schemas'
import type { FormField, FormSchema } from './types'

describe('Forms Module - Validation Schemas', () => {
  describe('FormFieldSchema', () => {
    it('validates a valid text field', () => {
      const field: FormField = {
        name: 'email',
        label: 'Email Address',
        type: 'text',
        required: true,
        placeholder: 'you@example.com'
      }

      expect(() => FormFieldSchema.parse(field)).not.toThrow()
    })

    it('validates a valid select field with options', () => {
      const field: FormField = {
        name: 'priority',
        label: 'Priority',
        type: 'select',
        options: [
          { label: 'Low', value: 'low' },
          { label: 'High', value: 'high' }
        ]
      }

      expect(() => FormFieldSchema.parse(field)).not.toThrow()
    })

    it('rejects select field without options', () => {
      const field = {
        name: 'choice',
        label: 'Choose',
        type: 'select'
      }

      expect(() => FormFieldSchema.parse(field)).toThrow()
    })
  })

  describe('FormSchemaSchema', () => {
    it('validates a valid form schema', () => {
      const schema: FormSchema = {
        title: 'User Form',
        description: 'Please fill out this form',
        fields: [
          { name: 'name', label: 'Name', type: 'text', required: true },
          { name: 'age', label: 'Age', type: 'number', min: 0 }
        ]
      }

      expect(() => FormSchemaSchema.parse(schema)).not.toThrow()
    })

    it('rejects form with empty fields array', () => {
      const schema = {
        fields: []
      }

      expect(() => FormSchemaSchema.parse(schema)).toThrow()
    })
  })

  describe('validateFormSchema helper', () => {
    it('validates valid form schema', () => {
      const schema = {
        fields: [
          { name: 'field', label: 'Field', type: 'text' as const }
        ]
      }

      expect(() => validateFormSchema(schema)).not.toThrow()
    })

    it('rejects invalid form schema', () => {
      const schema = {
        fields: []
      }

      expect(() => validateFormSchema(schema)).toThrow()
    })
  })

  describe('validateFormField helper', () => {
    it('validates valid field', () => {
      const field = {
        name: 'email',
        label: 'Email',
        type: 'text' as const
      }

      expect(() => validateFormField(field)).not.toThrow()
    })

    it('rejects invalid field', () => {
      const field = {
        name: '',
        label: 'Email',
        type: 'text'
      }

      expect(() => validateFormField(field)).toThrow()
    })
  })
})
