'use client'

import { useState } from 'react'
import { useRouter } from 'next/navigation'
import {
  ArrowLeft,
  FileText,
  Building2,
  MapPin,
  Users,
  Loader2,
  AlertCircle,
} from 'lucide-react'

const TRIBUNAIS = [
  { value: 'STF', label: 'STF - Supremo Tribunal Federal' },
  { value: 'STJ', label: 'STJ - Superior Tribunal de Justiça' },
  { value: 'TST', label: 'TST - Tribunal Superior do Trabalho' },
  { value: 'TSE', label: 'TSE - Tribunal Superior Eleitoral' },
  { value: 'STM', label: 'STM - Superior Tribunal Militar' },
  { value: 'TRF1', label: 'TRF1 - 1ª Região' },
  { value: 'TRF2', label: 'TRF2 - 2ª Região (RJ, ES)' },
  { value: 'TRF3', label: 'TRF3 - 3ª Região (SP, MS)' },
  { value: 'TRF4', label: 'TRF4 - 4ª Região (RS, PR, SC)' },
  { value: 'TRF5', label: 'TRF5 - 5ª Região (PE, AL, CE, PB, RN, SE)' },
  { value: 'TRF6', label: 'TRF6 - 6ª Região (MG)' },
  { value: 'TJSP', label: 'TJSP - São Paulo' },
  { value: 'TJRJ', label: 'TJRJ - Rio de Janeiro' },
  { value: 'TJMG', label: 'TJMG - Minas Gerais' },
  { value: 'TJRS', label: 'TJRS - Rio Grande do Sul' },
  { value: 'TJPR', label: 'TJPR - Paraná' },
  { value: 'TJSC', label: 'TJSC - Santa Catarina' },
  { value: 'TJBA', label: 'TJBA - Bahia' },
  { value: 'TJPE', label: 'TJPE - Pernambuco' },
  { value: 'TJCE', label: 'TJCE - Ceará' },
  { value: 'TJDF', label: 'TJDF - Distrito Federal' },
  { value: 'TJGO', label: 'TJGO - Goiás' },
  { value: 'TJMA', label: 'TJMA - Maranhão' },
  { value: 'TJPA', label: 'TJPA - Pará' },
  { value: 'TJMT', label: 'TJMT - Mato Grosso' },
  { value: 'TJMS', label: 'TJMS - Mato Grosso do Sul' },
  { value: 'TJES', label: 'TJES - Espírito Santo' },
  { value: 'TJAL', label: 'TJAL - Alagoas' },
  { value: 'TJAM', label: 'TJAM - Amazonas' },
  { value: 'TJPB', label: 'TJPB - Paraíba' },
  { value: 'TJPI', label: 'TJPI - Piauí' },
  { value: 'TJRN', label: 'TJRN - Rio Grande do Norte' },
  { value: 'TJSE', label: 'TJSE - Sergipe' },
  { value: 'TJTO', label: 'TJTO - Tocantins' },
  { value: 'TJAC', label: 'TJAC - Acre' },
  { value: 'TJAP', label: 'TJAP - Amapá' },
  { value: 'TJRO', label: 'TJRO - Rondônia' },
  { value: 'TJRR', label: 'TJRR - Roraima' },
]

export default function NovoProcessoPage() {
  const router = useRouter()
  const [saving, setSaving] = useState(false)
  const [error, setError] = useState<string | null>(null)
  
  const [form, setForm] = useState({
    numeroProcesso: '',
    tribunal: '',
    vara: '',
    comarca: '',
    parteAutora: '',
    parteRe: '',
  })

  // Máscara CNJ: 0000000-00.0000.0.00.0000
  function formatCNJ(value: string): string {
    const digits = value.replace(/\D/g, '').slice(0, 20)
    
    if (digits.length <= 7) return digits
    if (digits.length <= 9) return `${digits.slice(0, 7)}-${digits.slice(7)}`
    if (digits.length <= 13) return `${digits.slice(0, 7)}-${digits.slice(7, 9)}.${digits.slice(9)}`
    if (digits.length <= 14) return `${digits.slice(0, 7)}-${digits.slice(7, 9)}.${digits.slice(9, 13)}.${digits.slice(13)}`
    if (digits.length <= 16) return `${digits.slice(0, 7)}-${digits.slice(7, 9)}.${digits.slice(9, 13)}.${digits.slice(13, 14)}.${digits.slice(14)}`
    return `${digits.slice(0, 7)}-${digits.slice(7, 9)}.${digits.slice(9, 13)}.${digits.slice(13, 14)}.${digits.slice(14, 16)}.${digits.slice(16)}`
  }

  function handleNumeroChange(e: React.ChangeEvent<HTMLInputElement>) {
    const formatted = formatCNJ(e.target.value)
    setForm(f => ({ ...f, numeroProcesso: formatted }))
  }

  async function handleSubmit(e: React.FormEvent) {
    e.preventDefault()
    setError(null)
    
    if (!form.numeroProcesso || !form.tribunal) {
      setError('Número do processo e tribunal são obrigatórios')
      return
    }

    // Valida formato CNJ
    const cnjRegex = /^\d{7}-\d{2}\.\d{4}\.\d{1}\.\d{2}\.\d{4}$/
    if (!cnjRegex.test(form.numeroProcesso)) {
      setError('Número do processo inválido. Complete o formato CNJ: 0000000-00.0000.0.00.0000')
      return
    }

    setSaving(true)
    try {
      const res = await fetch('/api/processos', {
        method: 'POST',
        headers: { 'Content-Type': 'application/json' },
        body: JSON.stringify(form),
      })

      const data = await res.json()

      if (!res.ok) {
        setError(data.error || 'Erro ao cadastrar processo')
        return
      }

      router.push('/dashboard/publicacoes')
    } catch (e) {
      setError('Erro de conexão')
    } finally {
      setSaving(false)
    }
  }

  return (
    <div className="max-w-2xl mx-auto">
      {/* Header */}
      <div className="mb-8">
        <button
          onClick={() => router.back()}
          className="flex items-center gap-2 text-sm text-zinc-400 hover:text-white transition-colors mb-4"
        >
          <ArrowLeft className="h-4 w-4" />
          Voltar
        </button>
        <h1 className="text-2xl font-bold text-white flex items-center gap-3">
          <div className="flex h-10 w-10 items-center justify-center rounded-xl bg-indigo-600/20">
            <FileText className="h-5 w-5 text-indigo-400" />
          </div>
          Adicionar Processo para Monitorar
        </h1>
        <p className="mt-2 text-sm text-zinc-500">
          Cadastre um processo para acompanhar publicações nos Diários Oficiais
        </p>
      </div>

      {/* Form */}
      <form onSubmit={handleSubmit} className="space-y-6">
        {error && (
          <div className="flex items-center gap-2 rounded-lg border border-red-500/30 bg-red-500/10 px-4 py-3 text-sm text-red-400">
            <AlertCircle className="h-4 w-4 flex-shrink-0" />
            {error}
          </div>
        )}

        {/* Número do Processo */}
        <div className="rounded-xl border border-white/10 bg-white/[0.02] p-6">
          <h3 className="text-sm font-semibold text-white flex items-center gap-2 mb-4">
            <FileText className="h-4 w-4 text-indigo-400" />
            Dados do Processo
          </h3>
          
          <div className="space-y-4">
            <div>
              <label className="block text-sm font-medium text-zinc-400 mb-1">
                Número do Processo (CNJ) *
              </label>
              <input
                type="text"
                value={form.numeroProcesso}
                onChange={handleNumeroChange}
                placeholder="0000000-00.0000.0.00.0000"
                className="w-full rounded-lg border border-white/10 bg-white/5 px-4 py-3 font-mono text-white outline-none focus:border-indigo-500/40 placeholder:text-zinc-600"
              />
              <p className="mt-1 text-xs text-zinc-600">
                Formato: NNNNNNN-DD.AAAA.J.TR.OOOO
              </p>
            </div>

            <div>
              <label className="block text-sm font-medium text-zinc-400 mb-1">
                Tribunal *
              </label>
              <select
                value={form.tribunal}
                onChange={e => setForm(f => ({ ...f, tribunal: e.target.value }))}
                className="w-full rounded-lg border border-white/10 bg-white/5 px-4 py-3 text-white outline-none focus:border-indigo-500/40 [&>option]:bg-zinc-900"
              >
                <option value="">Selecione o tribunal</option>
                {TRIBUNAIS.map(t => (
                  <option key={t.value} value={t.value}>{t.label}</option>
                ))}
              </select>
            </div>
          </div>
        </div>

        {/* Localização */}
        <div className="rounded-xl border border-white/10 bg-white/[0.02] p-6">
          <h3 className="text-sm font-semibold text-white flex items-center gap-2 mb-4">
            <MapPin className="h-4 w-4 text-green-400" />
            Localização (opcional)
          </h3>
          
          <div className="grid gap-4 sm:grid-cols-2">
            <div>
              <label className="block text-sm font-medium text-zinc-400 mb-1">Vara</label>
              <input
                type="text"
                value={form.vara}
                onChange={e => setForm(f => ({ ...f, vara: e.target.value }))}
                placeholder="Ex: 1ª Vara Cível"
                className="w-full rounded-lg border border-white/10 bg-white/5 px-4 py-2.5 text-white outline-none focus:border-indigo-500/40 placeholder:text-zinc-600"
              />
            </div>
            <div>
              <label className="block text-sm font-medium text-zinc-400 mb-1">Comarca</label>
              <input
                type="text"
                value={form.comarca}
                onChange={e => setForm(f => ({ ...f, comarca: e.target.value }))}
                placeholder="Ex: São Paulo"
                className="w-full rounded-lg border border-white/10 bg-white/5 px-4 py-2.5 text-white outline-none focus:border-indigo-500/40 placeholder:text-zinc-600"
              />
            </div>
          </div>
        </div>

        {/* Partes */}
        <div className="rounded-xl border border-white/10 bg-white/[0.02] p-6">
          <h3 className="text-sm font-semibold text-white flex items-center gap-2 mb-4">
            <Users className="h-4 w-4 text-purple-400" />
            Partes (opcional)
          </h3>
          
          <div className="grid gap-4 sm:grid-cols-2">
            <div>
              <label className="block text-sm font-medium text-zinc-400 mb-1">Parte Autora</label>
              <input
                type="text"
                value={form.parteAutora}
                onChange={e => setForm(f => ({ ...f, parteAutora: e.target.value }))}
                placeholder="Nome do autor"
                className="w-full rounded-lg border border-white/10 bg-white/5 px-4 py-2.5 text-white outline-none focus:border-indigo-500/40 placeholder:text-zinc-600"
              />
            </div>
            <div>
              <label className="block text-sm font-medium text-zinc-400 mb-1">Parte Ré</label>
              <input
                type="text"
                value={form.parteRe}
                onChange={e => setForm(f => ({ ...f, parteRe: e.target.value }))}
                placeholder="Nome do réu"
                className="w-full rounded-lg border border-white/10 bg-white/5 px-4 py-2.5 text-white outline-none focus:border-indigo-500/40 placeholder:text-zinc-600"
              />
            </div>
          </div>
        </div>

        {/* Submit */}
        <div className="flex justify-end gap-3">
          <button
            type="button"
            onClick={() => router.back()}
            className="rounded-xl border border-white/10 px-6 py-2.5 text-sm font-medium text-zinc-400 hover:bg-white/5"
          >
            Cancelar
          </button>
          <button
            type="submit"
            disabled={saving}
            className="flex items-center gap-2 rounded-xl bg-indigo-600 px-6 py-2.5 text-sm font-semibold text-white hover:bg-indigo-500 disabled:opacity-50"
          >
            {saving && <Loader2 className="h-4 w-4 animate-spin" />}
            Adicionar Processo
          </button>
        </div>
      </form>
    </div>
  )
}
