import { useNavigate } from 'react-router-dom'
import { toast } from 'sonner'
import {
  Card,
  CardContent,
  CardDescription,
  CardHeader,
  CardTitle,
} from '@/components/ui/card'
import { MedioForm } from '../components/MedioForm'
import { useCreateMedio } from '../hooks/useCreateMedio'
import type { MedioFormValues } from '../components/MedioForm'

export function CreateMedioPage() {
  const navigate = useNavigate()
  const { mutate, isPending, error } = useCreateMedio()

  function handleSubmit(values: MedioFormValues) {
    mutate(
      {
        codigo: values.codigo,
        nombre: values.nombre,
        tipo: values.tipo,
        plataformaEmpresaId: values.plataformaEmpresaId as number | null,
      },
      {
        onSuccess: () => {
          toast.success('Medio creado correctamente')
          void navigate('/admin/medios')
        },
      },
    )
  }

  return (
    <div className="flex justify-center py-8">
      <Card className="w-full max-w-lg">
        <CardHeader className="space-y-1">
          <CardTitle className="text-xl">Crear Medio</CardTitle>
          <CardDescription>
            Completá los datos para registrar un nuevo medio en el sistema.
          </CardDescription>
        </CardHeader>
        <CardContent>
          <MedioForm isPending={isPending} error={error} onSubmit={handleSubmit} />
        </CardContent>
      </Card>
    </div>
  )
}
