"use client";

import { useRouter } from "next/navigation";
import { toast } from "sonner";
import { PageHeader } from "@/components/shared/page-header";
import { ContractorForm } from "@/components/contractors/contractor-form";
import { useCreateContractor } from "@/lib/hooks/use-contractors";
import { useDataProvider } from "@/lib/demo/data-provider-context";
import type { ContractorFormValues } from "@/components/contractors/contractor-form";

export default function NewContractorPage() {
  const router = useRouter();
  const { basePath } = useDataProvider();
  const createContractor = useCreateContractor();

  function handleSubmit(data: ContractorFormValues) {
    createContractor.mutate(data, {
      onSuccess: (res) => {
        toast.success("Contractor created");
        router.push(`${basePath}/contractors/${res.id}`);
      },
      onError: () => {
        toast.error("Failed to create contractor");
      },
    });
  }

  return (
    <div className="space-y-6 w-full max-w-2xl mx-auto">
      <PageHeader title="New Contractor" />
      <ContractorForm onSubmit={handleSubmit} loading={createContractor.isPending} />
    </div>
  );
}
