"use client";

import type { ProfileExport } from "@/lib/profile/db";
import {
  exportProfileData,
  importProfileData,
} from "@/lib/profile/db";

export interface SyncAdapter {
  exportProfile(profileId: string): Promise<ProfileExport | null>;
  importProfile(data: ProfileExport, asNew?: boolean): Promise<unknown>;
  pushSaves(profileId: string): Promise<{ ok: boolean; message: string }>;
  pullSaves(profileId: string): Promise<{ ok: boolean; message: string }>;
}

/** Phase A / local: metadata export only; EmulatorJS blobs stay in browser IDB. */
export class LocalOnlySyncAdapter implements SyncAdapter {
  async exportProfile(profileId: string) {
    return exportProfileData(profileId);
  }

  async importProfile(data: ProfileExport, asNew = true) {
    return importProfileData(data, asNew);
  }

  async pushSaves(_profileId: string) {
    return {
      ok: false,
      message:
        "Cloud save sync ships in a later phase. Emulator saves stay in this browser.",
    };
  }

  async pullSaves(_profileId: string) {
    return {
      ok: false,
      message:
        "Cloud save sync ships in a later phase. Emulator saves stay in this browser.",
    };
  }
}

let syncAdapter: SyncAdapter = new LocalOnlySyncAdapter();

export function getSyncAdapter() {
  return syncAdapter;
}

export function setSyncAdapter(adapter: SyncAdapter) {
  syncAdapter = adapter;
}
