"use client"

import { useState, useEffect } from "react"
import { useRouter } from "next/navigation"

export default function CreateWorkerPage() {
  const router = useRouter()
  const [loading, setLoading] = useState(false)
  const [teams, setTeams] = useState<any[]>([])
  const [formData, setFormData] = useState({
    empNo: "",
    firstName: "",
    surname: "",
    email: "",
    phone: "",
    jobPosition: "Blow Moulder Level 1",
    teamId: "",
    status: "active"
  })

  useEffect(() => {
    fetch("/api/teams")
      .then(r => r.json())
      .then(setTeams)
  }, [])

  const handleSubmit = async (e: React.FormEvent) => {
    e.preventDefault()
    setLoading(true)

    const response = await fetch("/api/admin/workers", {
      method: "POST",
      headers: { "Content-Type": "application/json" },
      body: JSON.stringify(formData)
    })

    if (response.ok) {
      router.push("/admin/workers")
    } else {
      alert("Error creating worker")
      setLoading(false)
    }
  }

  return (
    <div className="min-h-screen bg-gray-50 p-8">
      <div className="max-w-2xl mx-auto">
        <h1 className="text-3xl font-bold text-gray-800 mb-6">Add New Worker</h1>

        <form onSubmit={handleSubmit} className="bg-white p-6 rounded-xl shadow-sm border border-gray-200 space-y-4">
          <div>
            <label className="block text-sm font-medium mb-2">Employee Number *</label>
            <input
              type="text"
              value={formData.empNo}
              onChange={(e) => setFormData({...formData, empNo: e.target.value})}
              className="w-full px-4 py-2 border rounded-lg"
              required
            />
          </div>

          <div className="grid grid-cols-2 gap-4">
            <div>
              <label className="block text-sm font-medium mb-2">First Name *</label>
              <input
                type="text"
                value={formData.firstName}
                onChange={(e) => setFormData({...formData, firstName: e.target.value})}
                className="w-full px-4 py-2 border rounded-lg"
                required
              />
            </div>
            <div>
              <label className="block text-sm font-medium mb-2">Surname *</label>
              <input
                type="text"
                value={formData.surname}
                onChange={(e) => setFormData({...formData, surname: e.target.value})}
                className="w-full px-4 py-2 border rounded-lg"
                required
              />
            </div>
          </div>

          <div>
            <label className="block text-sm font-medium mb-2">Email</label>
            <input
              type="email"
              value={formData.email}
              onChange={(e) => setFormData({...formData, email: e.target.value})}
              className="w-full px-4 py-2 border rounded-lg"
            />
          </div>

          <div>
            <label className="block text-sm font-medium mb-2">Phone</label>
            <input
              type="text"
              value={formData.phone}
              onChange={(e) => setFormData({...formData, phone: e.target.value})}
              className="w-full px-4 py-2 border rounded-lg"
            />
          </div>

          <div>
            <label className="block text-sm font-medium mb-2">Job Position *</label>
            <select
              value={formData.jobPosition}
              onChange={(e) => setFormData({...formData, jobPosition: e.target.value})}
              className="w-full px-4 py-2 border rounded-lg"
              required
            >
              <option value="Blow Moulder Level 1">Blow Moulder Level 1 (Operator)</option>
              <option value="Blow Moulder Level 2">Blow Moulder Level 2 (Supervisor)</option>
              <option value="Engineer">Engineer</option>
            </select>
          </div>

          <div>
            <label className="block text-sm font-medium mb-2">Team</label>
            <select
              value={formData.teamId}
              onChange={(e) => setFormData({...formData, teamId: e.target.value})}
              className="w-full px-4 py-2 border rounded-lg"
            >
              <option value="">No Team</option>
              {teams.map((team: any) => (
                <option key={team.id} value={team.id}>
                  {team.name}
                </option>
              ))}
            </select>
          </div>

          <div>
            <label className="block text-sm font-medium mb-2">Status *</label>
            <select
              value={formData.status}
              onChange={(e) => setFormData({...formData, status: e.target.value})}
              className="w-full px-4 py-2 border rounded-lg"
              required
            >
              <option value="active">Active</option>
              <option value="inactive">Inactive</option>
            </select>
          </div>

          <div className="flex gap-4 pt-4">
            <button
              type="submit"
              disabled={loading}
              className="flex-1 bg-blue-600 text-white py-3 rounded-lg font-medium hover:bg-blue-700 transition-colors disabled:opacity-50"
            >
              {loading ? "Creating..." : "Create Worker"}
            </button>
            <button
              type="button"
              onClick={() => router.back()}
              className="px-6 py-3 border border-gray-300 rounded-lg hover:bg-gray-50"
            >
              Cancel
            </button>
          </div>
        </form>
      </div>
    </div>
  )
}
