import { api as client } from "../auth/http";
import { unwrap } from "@/utils/endpoint";

const SIGNATURE_URL = "/me/signature";

export interface SavedSignature {
  signerDisplayName: string;
  signatureImageUrl?: string | null;
}

export interface SaveSignaturePayload {
  signerDisplayName: string;
  signatureImageBase64: string;
}

export const signaturesService = {
  /** Returns the current user's reusable signature, or null if none saved. */
  getMySignature: async (): Promise<SavedSignature | null> => {
    const response = await client.get<SavedSignature | null>(SIGNATURE_URL);
    return (unwrap(response.data) as SavedSignature | null) ?? null;
  },

  saveMySignature: async (
    payload: SaveSignaturePayload,
  ): Promise<SavedSignature | null> => {
    const response = await client.put<SavedSignature | null>(
      SIGNATURE_URL,
      payload,
    );
    return (unwrap(response.data) as SavedSignature | null) ?? null;
  },
};
