"use client";

import { useMutation } from "convex/react";
import { useCallback } from "react";
import { api } from "../../../../../convex/_generated/api";
import type { Id } from "../../../../../convex/_generated/dataModel";
import type { AddressFormData } from "./types";

/**
 * Wraps the address CRUD mutations used during checkout:
 *
 * - `addAddress` — creates a shipping address with validation status.
 * - `updateAddress` — patches an existing address (re-validation is the caller's responsibility).
 * - `setDefault` — promotes an address to the user's default.
 */
export function useAddressMutations(): {
  addAddress: (data: AddressFormData & { isValidated: boolean }) => Promise<string>;
  updateAddress: (
    id: string,
    data: Partial<AddressFormData> & { isValidated?: boolean },
  ) => Promise<void>;
  setDefault: (id: string) => Promise<void>;
  markValidated: (id: string, isValidated: boolean) => Promise<void>;
} {
  const addMutation = useMutation(api.addresses.add);
  const updateMutation = useMutation(api.addresses.update);
  const setDefaultMutation = useMutation(api.addresses.setDefault);
  const markValidatedMutation = useMutation(api.addresses.markValidated);

  const addAddress = useCallback(
    async (data: AddressFormData & { isValidated: boolean }): Promise<string> => {
      const id = await addMutation({
        type: "shipping",
        firstName: data.firstName,
        lastName: data.lastName,
        phone: data.phone,
        addressLine1: data.addressLine1,
        additionalInformation: data.additionalInformation,
        city: data.city,
        postalCode: data.postalCode,
        country: data.country,
        isDefault: false,
        isValidated: data.isValidated,
      });
      return id;
    },
    [addMutation],
  );

  const updateAddress = useCallback(
    async (
      id: string,
      data: Partial<AddressFormData> & { isValidated?: boolean },
    ): Promise<void> => {
      await updateMutation({
        id: id as Id<"addresses">,
        ...(data.firstName !== undefined && { firstName: data.firstName }),
        ...(data.lastName !== undefined && { lastName: data.lastName }),
        ...(data.phone !== undefined && { phone: data.phone }),
        ...(data.addressLine1 !== undefined && { addressLine1: data.addressLine1 }),
        ...(data.additionalInformation !== undefined && { additionalInformation: data.additionalInformation }),
        ...(data.city !== undefined && { city: data.city }),
        ...(data.postalCode !== undefined && { postalCode: data.postalCode }),
        ...(data.country !== undefined && { country: data.country }),
        ...(data.isValidated !== undefined && { isValidated: data.isValidated }),
      });
    },
    [updateMutation],
  );

  const setDefault = useCallback(
    async (id: string): Promise<void> => {
      await setDefaultMutation({ id: id as Id<"addresses"> });
    },
    [setDefaultMutation],
  );

  const markValidated = useCallback(
    async (id: string, isValidated: boolean): Promise<void> => {
      await markValidatedMutation({ id: id as Id<"addresses">, isValidated });
    },
    [markValidatedMutation],
  );

  return { addAddress, updateAddress, setDefault, markValidated };
}
