import { apiGet, apiPost } from './client';
import type { Identity, IdentityPhotoListItem } from '@/types';

export async function fetchIdentities(deviceId: string): Promise<Identity[]> {
  try {
    return await apiGet<Identity[]>(`/my/identities?deviceId=${encodeURIComponent(deviceId)}`);
  } catch (err) {
    throw err instanceof Error ? err : new Error('fetchIdentities failed');
  }
}

export async function renameIdentity(id: string, name: string | null): Promise<void> {
  try {
    await apiPost(`/my/identities/${encodeURIComponent(id)}/rename`, { name });
  } catch (err) {
    throw err instanceof Error ? err : new Error('renameIdentity failed');
  }
}

export async function mergeIdentities(targetId: string, sourceId: string): Promise<void> {
  try {
    await apiPost(`/my/identities/merge`, { targetId, sourceId });
  } catch (err) {
    throw err instanceof Error ? err : new Error('mergeIdentities failed');
  }
}

export async function fetchIdentityPhotos(id: string, limit = 100): Promise<IdentityPhotoListItem[]> {
  try {
    return await apiGet<IdentityPhotoListItem[]>(
      `/my/identities/${encodeURIComponent(id)}/photos?limit=${limit}`,
    );
  } catch (err) {
    throw err instanceof Error ? err : new Error('fetchIdentityPhotos failed');
  }
}
