import { useState } from 'react'
import { useMutation } from '@tanstack/react-query'
import { useToast } from 'sonner'
import { Button } from '@/components/ui/Button'
import { Input } from '@/components/ui/Input'
import { Textarea } from '@/components/ui/Textarea'
import { Select } from '@/components/ui/Select'
import { api } from '@/lib/api'

const Support = () => {
  const { toast } = useToast()
  const [formState, setFormState] = useState({
    name: '',
    company: '',
    email: '',
    phone: '',
    issue: '',
    priority: 'medium',
  })

  const mutation = useMutation({
    mutationFn: (data) => api.post('/support', data),
    onSuccess: () => {
      toast.success('Thanks! We\'ll get back to you soon.')
      setFormState({
        name: '',
        company: '',
        email: '',
        phone: '',
        issue: '',
        priority: 'medium',
      })
    },
    onError: (error) => {
      toast.error(error.message || 'Failed to submit form. Please try again.')
    },
  })

  const handleSubmit = (e) => {
    e.preventDefault()
    mutation.mutate(formState)
  }

  const handleChange = (e) => {
    const { name, value } = e.target
    setFormState(prev => ({ ...prev, [name]: value }))
  }

  return (
    <div className="container mx-auto px-4 py-16 md:py-24">
      {/* Page Hero */}
      <section className="mb-16">
        <h1 className="text-4xl md:text-5xl font-bold text-primary-navy mb-6">Support</h1>
        <p className="text-xl text-soft-text max-w-3xl">
          Need help with your communications or infrastructure? Submit a support request and we'll get back to you promptly.
        </p>
      </section>

      {/* Support Form */}
      <div className="grid grid-cols-1 lg:grid-cols-2 gap-12">
        {/* Left - Info */}
        <div>
          <div className="mb-8">
            <h2 className="text-2xl font-bold text-primary-navy mb-4">Support Services</h2>
            <p className="text-soft-text mb-6">
              We provide comprehensive support for all our services, including 24/7 monitoring, rapid response, and dedicated support engineers.
            </p>
            <div className="space-y-4">
              <div>
                <h3 className="font-semibold text-text mb-2">Support Hours</h3>
                <p className="text-soft-text">24/7 Monitoring with rapid response SLAs</p>
              </div>
              <div>
                <h3 className="font-semibold text-text mb-2">Priority Levels</h3>
                <p className="text-soft-text">
                  Low: General inquiries (response within 24 hours)<br/>
                  Medium: Standard issues (response within 4 hours)<br/>
                  High: Critical issues (response within 1 hour)
                </p>
              </div>
            </div>
          </div>

          <div className="bg-section-alt rounded-lg p-6">
            <h3 className="font-semibold text-primary-navy mb-4">What We Support</h3>
            <ul className="space-y-3">
              {[
                '8x8 Communications Platform',
                'VoIP Phone Systems',
                'Contact Center Solutions',
                'Network Infrastructure',
                'Cloud Migration Support',
              ].map((item, index) => (
                <li key={index} className="flex items-center gap-3 text-text">
                  <svg className="h-5 w-5 text-primary-navy" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                    <path strokeLinecap="round" strokeLinejoin="round" strokeWidth="2" d="M5 13l4 4L19 7" />
                  </svg>
                  {item}
                </li>
              ))}
            </ul>
          </div>
        </div>

        {/* Right - Form */}
        <div>
          <form onSubmit={handleSubmit} className="space-y-6">
            <div>
              <label htmlFor="name" className="block text-sm font-medium text-text mb-2">
                Name <span className="text-red-600">*</span>
              </label>
              <Input
                type="text"
                id="name"
                name="name"
                value={formState.name}
                onChange={handleChange}
                required
                placeholder="Your full name"
              />
            </div>

            <div>
              <label htmlFor="company" className="block text-sm font-medium text-text mb-2">
                Company Name <span className="text-red-600">*</span>
              </label>
              <Input
                type="text"
                id="company"
                name="company"
                value={formState.company}
                onChange={handleChange}
                required
                placeholder="Your company name"
              />
            </div>

            <div>
              <label htmlFor="email" className="block text-sm font-medium text-text mb-2">
                Email <span className="text-red-600">*</span>
              </label>
              <Input
                type="email"
                id="email"
                name="email"
                value={formState.email}
                onChange={handleChange}
                required
                placeholder="your.email@example.com"
              />
            </div>

            <div>
              <label htmlFor="phone" className="block text-sm font-medium text-text mb-2">
                Phone (Optional)
              </label>
              <Input
                type="tel"
                id="phone"
                name="phone"
                value={formState.phone}
                onChange={handleChange}
                placeholder="(555) 123-4567"
              />
            </div>

            <div>
              <label htmlFor="priority" className="block text-sm font-medium text-text mb-2">
                Priority <span className="text-red-600">*</span>
              </label>
              <Select
                id="priority"
                name="priority"
                value={formState.priority}
                onChange={handleChange}
              >
                <option value="low">Low - General inquiries (24 hours)</option>
                <option value="medium">Medium - Standard issues (4 hours)</option>
                <option value="high">High - Critical issues (1 hour)</option>
              </Select>
            </div>

            <div>
              <label htmlFor="issue" className="block text-sm font-medium text-text mb-2">
                Describe Your Issue <span className="text-red-600">*</span>
              </label>
              <Textarea
                id="issue"
                name="issue"
                value={formState.issue}
                onChange={handleChange}
                required
                placeholder="Please describe your issue in detail..."
                rows={5}
              />
            </div>

            <Button
              type="submit"
              className="w-full"
              disabled={mutation.isPending}
            >
              {mutation.isPending ? 'Submitting...' : 'Submit Request'}
            </Button>
          </form>
        </div>
      </div>
    </div>
  )
}

export default Support
