import { Page } from 'playwright';
import { WebAgent } from './agent/webAgent.js';
import { ActionEntity } from './test-flow/actionEntity.js';
import { ActionGenerationResponse } from '@loggia/common/interfaces/testStepsInterface';
import { AgentStepEvent } from 'sdk-internal';

/**
 * SandboxService Interface
 *
 * Common interface for debugger sandbox services.
 * Implemented by LocalSandboxService (standalone) and PlaywrightSandboxService (Playwright-bootstrapped).
 */

type AgentStepEventCallback = (event: AgentStepEvent) => void;
interface DebugInfo {
    sessionId: string;
    liveviewUrl: string;
    browserWsUrl: string;
}
interface RunStepResult {
    success: boolean;
    actions: ActionEntity[];
    details?: string;
}
interface SandboxService {
    getCdpEndpoint(): string | null;
    createSession(config?: {
        startingUrl?: string;
        testFilePath?: string;
    }): Promise<{
        sessionId: string;
    }>;
    executeLogin(sessionId: string): Promise<{
        status: string;
        details?: string;
    }>;
    ensureBrowser(sessionId: string): Promise<{
        page: Page;
        liveviewUrl: string;
    }>;
    startDebug(sessionId: string): Promise<DebugInfo>;
    terminateSession(sessionId: string): Promise<void>;
    cleanupAll(): Promise<void>;
    executeAction(sessionId: string, actionEntity: ActionEntity, stepId: string, options?: {
        withSelfHealing?: boolean;
        stmtUid?: string;
        executionHistory?: Array<[string, string]>;
    }): Promise<any>;
    /** Execute raw JS code and return its result. Used for IF/WHILE condition evaluation. */
    executeCode?(sessionId: string, code: string): Promise<{
        status: string;
        result?: any;
        details?: string;
    }>;
    runStep(sessionId: string, statement: string, stepId: string, onEvent: AgentStepEventCallback, executionHistory?: Array<[string, string]>): Promise<RunStepResult>;
    stopRunStep(sessionId: string): boolean;
    evaluate(sessionId: string, statement: string, executionHistory?: Array<[string, string]>): Promise<Record<string, any>>;
    generateAction(sessionId: string, statement: string, stepId: string, options?: {
        executionHistory?: Array<[string, string]>;
        usePureVision?: boolean;
        includeDebugInfo?: boolean;
    }): Promise<any>;
    takeScreenshot(sessionId: string, stepId?: string): Promise<{
        screenshot: string;
        screenshotPath?: string;
    }>;
    /** Return accumulated artifacts (screenshots) from the current session. */
    getSessionArtifacts?(sessionId: string): {
        outputDir?: string;
        screenshots: Array<{
            stepId: string;
            path: string;
        }>;
    };
    startRecorder(sessionId: string, onEvent: (event: any) => void, testIdAttributeName?: string): Promise<void>;
    stopRecorder(sessionId: string): Promise<void>;
}

/**
 * Playwright Sandbox Service
 *
 * Lightweight sandbox that wraps an externally-provided Playwright page and
 * WebAgent. Used when the debugger runs inside a Playwright test context,
 * inheriting all Playwright config features (baseURL, storageState, viewport, etc.).
 */

/**
 * A sandbox backed by a Playwright-provided page and agent.
 * Since Playwright handles browser lifecycle, auth, baseURL, etc.,
 * this service just delegates to the agent for test operations.
 */
declare class PlaywrightSandboxService implements SandboxService {
    private page;
    private agent;
    private testDir;
    private projectRoot?;
    private sessionId;
    private abortController;
    private recorderStopResolver?;
    private recorderEnabled;
    private screenshotHistory;
    private yamlFilePath?;
    private artifactsDir?;
    constructor(page: Page, agent: WebAgent, fixturesDir?: string, projectRoot?: string);
    private cdpEndpoint;
    setCdpEndpoint(url: string): void;
    getCdpEndpoint(): string | null;
    createSession(config?: {
        startingUrl?: string;
        testFilePath?: string;
    }): Promise<{
        sessionId: string;
    }>;
    executeLogin(_sessionId: string): Promise<{
        status: string;
        details?: string;
    }>;
    ensureBrowser(_sessionId: string): Promise<{
        page: Page;
        liveviewUrl: string;
    }>;
    startDebug(_sessionId: string): Promise<DebugInfo>;
    executeAction(_sessionId: string, actionEntity: ActionEntity, stepId: string, options?: {
        withSelfHealing?: boolean;
        stmtUid?: string;
        executionHistory?: Array<[string, string]>;
    }): Promise<any>;
    runStep(_sessionId: string, statement: string, stepId: string, onEvent: AgentStepEventCallback, executionHistory?: Array<[string, string]>): Promise<RunStepResult>;
    /**
     * Execute a function action with a path#export reference.
     * Dynamically imports the file and calls the named export,
     * resolving args the same way the transpiler does (page, agent, literals, variables).
     */
    private executeFunction;
    executeCode(_sessionId: string, code: string): Promise<{
        status: string;
        result?: any;
        details?: string;
    }>;
    stopRunStep(_sessionId: string): boolean;
    evaluate(_sessionId: string, statement: string, _executionHistory?: Array<[string, string]>): Promise<Record<string, any>>;
    generateAction(_sessionId: string, statement: string, stepId: string, options?: {
        executionHistory?: Array<[string, string]>;
        usePureVision?: boolean;
        includeDebugInfo?: boolean;
    }): Promise<ActionGenerationResponse>;
    takeScreenshot(_sessionId: string, stepId?: string): Promise<{
        screenshot: string;
        screenshotPath?: string;
    }>;
    getSessionArtifacts(_sessionId: string): {
        outputDir?: string;
        screenshots: Array<{
            stepId: string;
            path: string;
        }>;
    };
    startRecorder(_sessionId: string, onEvent: (event: any) => void, testIdAttributeName?: string): Promise<void>;
    stopRecorder(_sessionId: string): Promise<void>;
    terminateSession(_sessionId: string): Promise<void>;
    cleanupAll(): Promise<void>;
}

/**
 * Playwright Debugger — public entry point for `shiplightai/debugger-pw`.
 *
 * Imported by the generated debug test file inside a Playwright test context.
 * Starts an internal HTTP server with int-runner routes,
 * backed by PlaywrightSandboxService.
 */

interface PlaywrightDebugOptions {
    yamlFilePath: string;
    port: number;
    page: Page;
    agent: WebAgent;
    /** Directory containing playwright.config.ts — used to resolve the fixtures dir. */
    projectRoot?: string;
}
/**
 * Start the internal sandbox server backed by a Playwright-provided page and agent.
 */
declare function startPlaywrightDebugServer(options: PlaywrightDebugOptions): Promise<{
    url: string;
    close: () => Promise<void>;
}>;

export { type PlaywrightDebugOptions, PlaywrightSandboxService, startPlaywrightDebugServer };
