import * as _electric_sql_pglite from '@electric-sql/pglite';
import * as drizzle_orm_pglite from 'drizzle-orm/pglite';
import * as schema from '@playcademy/data/tables';
import * as _hono_node_server from '@hono/node-server';
import { TimebackOrgType, TimebackUserRole } from '@playcademy/types/user';

/**
 * Configuration Type Definitions
 */

type TimebackMode = 'local' | 'remote' | 'disabled';
/**
 * Organization configuration input for sandbox.
 * All fields optional since they can be derived from defaults.
 */
interface TimebackOrganizationInput {
    id?: string;
    name?: string;
    type?: TimebackOrgType;
}
/**
 * Course enrollment overrides.
 * - `'mock'` or omit: Use mock ID
 * - Real string: Use that course ID
 * - `null` or `false`: Exclude from enrollment
 */
type CourseEnrollmentOverrides = Record<string, 'mock' | string | null | false>;
/**
 * Timeback configuration input for sandbox.
 * All fields optional since they can be derived from defaults.
 */
interface TimebackConfig {
    mode: TimebackMode;
    onerosterApiUrl?: string;
    caliperApiUrl?: string;
    clientId?: string;
    clientSecret?: string;
    authUrl?: string;
    courseId?: string;
    timebackId?: string;
    organization?: TimebackOrganizationInput;
    role?: TimebackUserRole;
    courseOverrides?: CourseEnrollmentOverrides;
}

/**
 * Project information types
 */
/**
 * TimeBack course configuration from playcademy.config.json
 */
interface TimebackCourseConfig {
    subject: string;
    grade: number;
    courseId?: string;
    totalXp?: number | null;
    masterableUnits?: number | null;
}
/**
 * Information about the current game project
 * Used for seeding and display
 */
interface ProjectInfo {
    gameId?: string;
    slug: string;
    displayName: string;
    version: string;
    description?: string;
    /** TimeBack courses from playcademy.config.json integrations.timeback.courses */
    timebackCourses?: TimebackCourseConfig[];
}

/**
 * Server configuration types
 */

/**
 * Logger interface for embedding the sandbox in other tools (e.g., Vite plugin)
 *
 * Allows external tools to capture and display sandbox logs in their own format.
 */
interface PluginLogger {
    /** Log informational messages */
    info: (msg: string) => void;
    /** Log warning messages */
    warn: (msg: string) => void;
    /** Log error messages */
    error: (msg: string) => void;
}
/**
 * Configuration options for starting the Playcademy sandbox server
 *
 * The sandbox provides a local development environment that simulates the
 * Playcademy platform, including API endpoints, authentication, and database.
 *
 * @example
 * ```ts
 * import { startServer } from '@playcademy/sandbox'
 *
 * await startServer({
 *   port: 8788,
 *   seed: true,
 *   logLevel: 'debug'
 * })
 * ```
 */
interface ServerOptions {
    /**
     * Port number for the sandbox server.
     *
     * The sandbox will listen on this port for HTTP requests.
     *
     * @example
     * ```ts
     * {
     *   port: 8788
     * }
     * ```
     */
    port: number;
    /**
     * Project information for context.
     *
     * Provides metadata about the game being developed. Usually provided
     * automatically by the Vite plugin.
     */
    project?: ProjectInfo;
    /**
     * Enable verbose logging.
     *
     * Shows detailed information about all operations. Equivalent to
     * setting `logLevel: 'debug'`.
     *
     * @default false
     * @example
     * ```ts
     * {
     *   verbose: true // Show all debug output
     * }
     * ```
     */
    verbose?: boolean;
    /**
     * Run in quiet mode (minimal logging).
     *
     * Suppresses most log output except errors. Useful when embedding
     * the sandbox in other tools.
     *
     * @default false
     * @example
     * ```ts
     * {
     *   quiet: true // Suppress most logs
     * }
     * ```
     */
    quiet?: boolean;
    /**
     * Seed the database with demo data on startup.
     *
     * Creates demo users, games, achievements, and other platform data
     * for testing. Recommended for development.
     *
     * @default true
     * @example
     * ```ts
     * {
     *   seed: false // Don't seed demo data
     * }
     * ```
     */
    seed?: boolean;
    /**
     * Use an in-memory database instead of a file.
     *
     * The database only exists in RAM and is lost when the server stops.
     * Faster but non-persistent. Useful for testing.
     *
     * @default false
     * @example
     * ```ts
     * {
     *   memoryOnly: true // Database in RAM only
     * }
     * ```
     */
    memoryOnly?: boolean;
    /**
     * Recreate the database on each server start.
     *
     * Drops and recreates the database with fresh seed data every time
     * the server starts. Ensures a clean state for development.
     *
     * **Warning**: All existing data will be lost on each restart.
     *
     * @default false
     * @example
     * ```ts
     * {
     *   recreateDb: true // Fresh database every restart
     * }
     * ```
     */
    recreateDb?: boolean;
    /**
     * Custom path for the database file.
     *
     * Specifies where the SQLite database file should be stored.
     * If not provided, defaults to a path based on node_modules location.
     *
     * Special value `':memory:'` creates an in-memory database
     * (equivalent to `memoryOnly: true`).
     *
     * @example
     * ```ts
     * {
     *   databasePath: './my-sandbox.db'
     * }
     * ```
     *
     * @example
     * ```ts
     * {
     *   databasePath: ':memory:' // In-memory database
     * }
     * ```
     */
    databasePath?: string;
    /**
     * Log level for the sandbox server.
     *
     * Controls the verbosity of server logs:
     * - `'debug'`: Very detailed logs (all operations)
     * - `'info'`: Standard operational logs (default)
     * - `'warn'`: Warnings only
     * - `'error'`: Errors only
     *
     * @default 'info'
     * @example
     * ```ts
     * {
     *   logLevel: 'debug' // Show all debug information
     * }
     * ```
     */
    logLevel?: 'debug' | 'info' | 'warn' | 'error';
    /**
     * Timeback (time travel) integration configuration.
     *
     * Timeback allows you to simulate time-based game mechanics during
     * development (e.g., daily rewards, energy refills).
     *
     * @example
     * ```ts
     * {
     *   timeback: {
     *     enabled: true,
     *     port: 3210
     *   }
     * }
     * ```
     */
    timeback?: Partial<TimebackConfig>;
    /**
     * Custom logger for capturing sandbox output.
     *
     * Allows external tools (like the Vite plugin) to capture and display
     * sandbox logs in their own format. If not provided, logs go to console.
     *
     * @example
     * ```ts
     * {
     *   logger: {
     *     info: (msg) => console.log('[SANDBOX]', msg),
     *     warn: (msg) => console.warn('[SANDBOX]', msg),
     *     error: (msg) => console.error('[SANDBOX]', msg)
     *   }
     * }
     * ```
     */
    logger?: PluginLogger;
}

declare const version: string;
/**
 * Start the sandbox server
 *
 * @param port - Port to listen on
 * @param project - Optional project information for seeding
 * @param options - Server configuration options
 * @returns Server instance with stop method
 */
declare function startServer(port: number, project?: ProjectInfo, options?: Omit<ServerOptions, 'port' | 'project'>): Promise<{
    main: _hono_node_server.ServerType;
    db: drizzle_orm_pglite.PgliteDatabase<typeof schema> & {
        $client: _electric_sql_pglite.PGlite;
    };
    gameId: string | undefined;
    timebackMode: "local" | "mock" | "remote" | null;
    setRole: (role: TimebackUserRole) => void;
    reset: () => Promise<void>;
    stop: () => Promise<void>;
}>;

export { startServer, version };
