import { client } from "../utils/api";

const SIGNATURE_URL = "/api/me/signature";

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

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

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

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