'use client'

import { useActionState } from 'react'
import { createOrganization } from './actions'

const initialState = {
    success: false,
    error: '',
}

export function CreateOrgForm() {
    const [state, formAction, isPending] = useActionState(createOrganization, initialState)

    return (
        <div className="bg-white p-6 rounded-xl border shadow-sm">
            <h2 className="text-lg font-semibold text-gray-900 mb-4">Neue Innung anlegen</h2>

            {state.success && (
                <div className="mb-4 p-3 bg-green-50 text-green-700 rounded-lg text-sm">
                    Innung wurde erfolgreich angelegt!
                </div>
            )}

            {state.error && (
                <div className="mb-4 p-3 bg-red-50 text-red-700 rounded-lg text-sm">
                    {state.error}
                </div>
            )}

            <form action={formAction} className="space-y-4">
                <div>
                    <label className="block text-sm font-medium text-gray-700 mb-1">
                        Name der Innung
                    </label>
                    <input
                        type="text"
                        name="name"
                        required
                        placeholder="z.B. Tischler-Innung Berlin"
                        className="w-full px-3 py-2 border rounded-lg focus:ring-2 focus:ring-brand-500 focus:border-brand-500"
                    />
                </div>

                <div>
                    <label className="block text-sm font-medium text-gray-700 mb-1">
                        Kurzbezeichnung (Slug)
                    </label>
                    <p className="text-xs text-gray-500 mb-2">Für interne Zuordnung (nur Kleinbuchstaben, ohne Leerzeichen).</p>
                    <input
                        type="text"
                        name="slug"
                        required
                        placeholder="tischler-berlin"
                        pattern="^[a-z0-9-]+$"
                        className="w-full px-3 py-2 border rounded-lg focus:ring-2 focus:ring-brand-500 focus:border-brand-500"
                    />
                </div>

                <div>
                    <label className="block text-sm font-medium text-gray-700 mb-1">
                        Kontakt E-Mail (Optional)
                    </label>
                    <input
                        type="email"
                        name="contactEmail"
                        placeholder="info@tischler-berlin.de"
                        className="w-full px-3 py-2 border rounded-lg focus:ring-2 focus:ring-brand-500 focus:border-brand-500"
                    />
                </div>

                <button
                    type="submit"
                    disabled={isPending}
                    className="w-full bg-brand-500 text-white font-medium py-2 px-4 rounded-lg hover:bg-brand-600 transition-colors disabled:opacity-50"
                >
                    {isPending ? 'Wird angelegt...' : 'Innung anlegen'}
                </button>
            </form>
        </div>
    )
}
