import { useRef } from 'react'
import { Modal } from '@/components/common/Modal'
import { cn } from '@/lib/utils'
import type { SessionOutcome } from '@/types'

interface SessionOutcomeModalProps {
  isOpen: boolean
  onClose: () => void
  onSubmit: (data: { outcome: SessionOutcome; outcome_notes?: string; next_steps?: string }) => Promise<void>
  isSubmitting?: boolean
}

const OUTCOME_OPTIONS: Array<{ value: SessionOutcome; label: string; description: string }> = [
  { value: 'resolved', label: 'Resolved', description: 'Issue fully resolved in this session.' },
  { value: 'workaround', label: 'Workaround', description: 'Temporary fix applied, root cause remains.' },
  { value: 'escalated', label: 'Escalated', description: 'Handed off to another engineer/team.' },
  { value: 'unresolved', label: 'Unresolved', description: 'No fix or workaround identified yet.' },
]

export function SessionOutcomeModal({
  isOpen,
  onClose,
  onSubmit,
  isSubmitting = false,
}: SessionOutcomeModalProps) {
  const formRef = useRef<HTMLFormElement | null>(null)

  const handleSubmit = async () => {
    if (!formRef.current) return
    const formData = new FormData(formRef.current)
    const outcome = (formData.get('session-outcome') as SessionOutcome | null) ?? 'resolved'
    const outcomeNotes = ((formData.get('outcome-notes') as string | null) ?? '').trim()
    const nextSteps = ((formData.get('next-steps') as string | null) ?? '').trim()

    await onSubmit({
      outcome,
      outcome_notes: outcomeNotes || undefined,
      next_steps: nextSteps || undefined,
    })
  }

  return (
    <Modal
      isOpen={isOpen}
      onClose={onClose}
      title="Session Outcome"
      footer={(
        <div className="flex justify-end gap-2">
          <button
            type="button"
            onClick={onClose}
            disabled={isSubmitting}
            className={cn(
              'rounded-md border border-border px-4 py-2 text-sm font-medium text-muted-foreground',
              'hover:bg-accent hover:text-foreground disabled:opacity-50'
            )}
          >
            Cancel
          </button>
          <button
            type="button"
            onClick={handleSubmit}
            disabled={isSubmitting}
            className={cn(
              'rounded-md bg-gradient-brand text-white shadow-lg shadow-primary/20 px-4 py-2 text-sm font-medium',
              'hover:opacity-90 disabled:opacity-50'
            )}
          >
            {isSubmitting ? 'Completing...' : 'Complete Session'}
          </button>
        </div>
      )}
    >
      <form key={String(isOpen)} ref={formRef} className="space-y-4">
        <p className="text-sm text-muted-foreground">
          Select the session outcome before completion.
        </p>
        <div className="space-y-2">
          {OUTCOME_OPTIONS.map((option) => (
            <label
              key={option.value}
              className={cn(
                'block cursor-pointer rounded-lg border border-border p-3 transition-colors',
                'hover:bg-accent/50'
              )}
            >
              <div className="flex items-start gap-3">
                <input
                  type="radio"
                  name="session-outcome"
                  value={option.value}
                  defaultChecked={option.value === 'resolved'}
                  className="mt-1 h-4 w-4"
                />
                <div>
                  <p className="text-sm font-medium text-foreground">{option.label}</p>
                  <p className="text-xs text-muted-foreground">{option.description}</p>
                </div>
              </div>
            </label>
          ))}
        </div>

        <div>
          <label className="block text-sm font-medium text-foreground">Outcome Notes (optional)</label>
          <textarea
            name="outcome-notes"
            defaultValue=""
            rows={3}
            placeholder="Add context for this outcome..."
            className={cn(
              'mt-1 block w-full rounded-md border border-border bg-card px-3 py-2',
              'text-sm text-foreground placeholder:text-muted-foreground',
              'focus:border-primary focus:outline-none focus:ring-1 focus:ring-primary/20'
            )}
          />
        </div>

        <div>
          <label className="block text-sm font-medium text-foreground">Next Steps / Follow-Up (optional)</label>
          <textarea
            name="next-steps"
            defaultValue=""
            rows={3}
            placeholder="Actions to take after this session..."
            className={cn(
              'mt-1 block w-full rounded-md border border-border bg-card px-3 py-2',
              'text-sm text-foreground placeholder:text-muted-foreground',
              'focus:border-primary focus:outline-none focus:ring-1 focus:ring-primary/20'
            )}
          />
        </div>
      </form>
    </Modal>
  )
}
