import { Status } from '@vue-skuilder/common';

/**
 * Common types used by UserDB implementations across different sync strategies
 */

/**
 * Result type for account creation operations
 */
interface AccountCreationResult {
    status: Status;
    error?: string;
}
/**
 * Result type for authentication operations
 */
interface AuthenticationResult {
    ok: boolean;
    error?: string;
}

/**
 * Strategy interface for handling user data synchronization
 * Different implementations handle remote sync vs local-only storage
 */
interface SyncStrategy {
    /**
     * Set up the remote database for a user
     * @param username The username to set up remote DB for
     * @returns PouchDB database instance (may be same as local for no-op)
     */
    setupRemoteDB(username: string): PouchDB.Database;
    /**
     * Get the database to use for write operations (local-first approach)
     * @param username The username to get write DB for
     * @returns PouchDB database instance for write operations
     */
    getWriteDB?(username: string): PouchDB.Database;
    /**
     * Pull the remote database into the local one and RESOLVE WHEN DONE.
     *
     * Distinct from startSync(): this is a one-shot, awaitable catch-up used to
     * populate an empty local mirror before the user object is handed out. A
     * strategy that omits it declares that it has no remote worth waiting for.
     *
     * Mechanism only — BaseUser owns the policy (whether to run it, timeout,
     * marker bookkeeping, resulting hydration state).
     *
     * @param localDB The local PouchDB instance
     * @param remoteDB The remote PouchDB instance
     * @returns Count of documents written locally
     */
    hydrate?(localDB: PouchDB.Database, remoteDB: PouchDB.Database): Promise<{
        docsWritten: number;
    }>;
    /**
     * Start synchronization between local and remote databases
     * @param localDB The local PouchDB instance
     * @param remoteDB The remote PouchDB instance
     */
    startSync(localDB: PouchDB.Database, remoteDB: PouchDB.Database): void;
    /**
     * Stop synchronization (optional - for cleanup)
     */
    stopSync?(): void;
    /**
     * Whether this strategy supports account creation
     */
    canCreateAccount(): boolean;
    /**
     * Whether this strategy supports authentication
     */
    canAuthenticate(): boolean;
    /**
     * Create a new user account (if supported)
     * @param username The username for the new account
     * @param password The password for the new account
     */
    createAccount?(username: string, password: string): Promise<AccountCreationResult>;
    /**
     * Authenticate a user (if supported)
     * @param username The username to authenticate
     * @param password The password to authenticate with
     */
    authenticate?(username: string, password: string): Promise<AuthenticationResult>;
    /**
     * Log out the current user (if supported)
     */
    logout?(): Promise<AuthenticationResult>;
    /**
     * Get the current logged-in username
     * Returns the username if logged in, or a default guest username
     */
    getCurrentUsername(): Promise<string>;
}

export type { AccountCreationResult as A, SyncStrategy as S, AuthenticationResult as a };
