'use client'

import {
  Badge,
  Button,
  HelperText,
  Label,
  Modal,
  ModalBody,
  ModalHeader,
  TextInput,
} from 'flowbite-react'
import { UseFormReturn } from 'react-hook-form'

import formTheme from '@/lib/themes/form'
import buttonTheme from '@/lib/themes/button'
import modalTheme from '@/lib/themes/modal'
import { FollowUpRatingFormValues } from '../schema'

interface ModalFormProps {
  isOpen: boolean
  type: 'create' | 'edit'
  isProcessing: boolean
  form: UseFormReturn<FollowUpRatingFormValues>
  onClose: () => void
  onSubmit: (formValues: FollowUpRatingFormValues) => Promise<void>
}

export default function ModalForm({
  isOpen,
  type,
  isProcessing,
  form,
  onClose,
  onSubmit,
}: ModalFormProps) {
  const label = form.watch('label')
  const textColor = form.watch('text_color')
  const backgroundColor = form.watch('background_color')

  return (
    <Modal show={isOpen} size="xl" onClose={onClose} theme={modalTheme.modal}>
      <ModalHeader>
        {type === 'create' ? 'Create Rating' : 'Edit Rating'}
      </ModalHeader>
      <ModalBody>
        <form onSubmit={form.handleSubmit(onSubmit)} className="mt-4">
          <div className="space-y-6">
            {/* Label */}
            <div>
              <div className="mb-2 block">
                <Label htmlFor="label" theme={formTheme.label}>
                  Label <span className="text-red-500">*</span>
                </Label>
              </div>
              <TextInput
                id="label"
                type="text"
                placeholder="Enter rating label (e.g., On Track)"
                theme={formTheme.textInput}
                {...form.register('label')}
              />
              <HelperText color="failure">
                {form.formState.errors.label?.message}
              </HelperText>
            </div>

            {/* Text Color */}
            <div>
              <div className="mb-2 block">
                <Label htmlFor="text_color" theme={formTheme.label}>
                  Text Color <span className="text-red-500">*</span>
                </Label>
              </div>

              <div className="flex items-stretch gap-2">
                <input
                  type="color"
                  id="text_color_picker"
                  aria-label="Pick text color"
                  className="h-10 w-14 cursor-pointer rounded border border-gray-300 bg-white p-1"
                  {...form.register('text_color')}
                />

                <div className="w-full">
                  <TextInput
                    id="text_color"
                    type="text"
                    readOnly
                    theme={formTheme.textInput}
                    className="w-full"
                    placeholder="Text Color"
                    value={textColor || ''}
                  />
                  <HelperText color="failure">
                    {form.formState.errors.text_color?.message}
                  </HelperText>
                </div>
              </div>
            </div>

            {/* Background Color */}
            <div>
              <div className="mb-2 block">
                <Label htmlFor="background_color" theme={formTheme.label}>
                  Background Color <span className="text-red-500">*</span>
                </Label>
              </div>

              <div className="flex items-stretch gap-2">
                <input
                  type="color"
                  id="background_color_picker"
                  aria-label="Pick background color"
                  className="h-10 w-14 cursor-pointer rounded border border-gray-300 bg-white p-1"
                  {...form.register('background_color')}
                />

                <div className="w-full">
                  <TextInput
                    id="background_color"
                    type="text"
                    readOnly
                    theme={formTheme.textInput}
                    className="w-full"
                    placeholder="Background Color"
                    value={backgroundColor || ''}
                  />
                  <HelperText color="failure">
                    {form.formState.errors.background_color?.message}
                  </HelperText>
                </div>
              </div>
            </div>

            {/* Preview */}
            <div className="w-full h-44 bg-gray-100 border border-gray-300 rounded flex items-center justify-center">
              <Badge
                className="w-fit rounded-full"
                style={{
                  backgroundColor: backgroundColor || '#000000',
                  color: textColor || '#ffffff',
                }}
              >
                {label || 'Rating label'}
              </Badge>
            </div>

            <div className="w-full flex justify-end">
              <Button
                type="submit"
                theme={buttonTheme.button}
                disabled={isProcessing}
              >
                {isProcessing ? 'Submitting…' : 'Submit'}
              </Button>
            </div>
          </div>
        </form>
      </ModalBody>
    </Modal>
  )
}
