export type BacktestTimeframe = '1m' | '15m' | '1h' | '4h';
export type BacktestDataSourceType = 'csv' | 'json' | 'replay' | 'kraken';
export type BacktestWindowClosePolicy = 'OPEN_AT_END' | 'FORCE_CLOSE';
export type BacktestIntraCandlePolicy = 'stop_loss_first' | 'take_profit_first' | 'ohlc_path';
export type BacktestTriggerTimeframe = 'off' | '1m';

export interface BacktestWarmup {
    startTimestamp: number;
    endTimestamp: number;
    candlesRequired: {
        '15m': number;
        '1h': number;
        '4h': number;
    };
    signalsIgnored: number;
}

export interface BacktestTrade {
    id: string;
    symbol: string;
    side: 'BUY' | 'SELL';
    entryTimestamp: number;
    exitTimestamp: number;
    durationMinutes: number;
    size: number;
    entryPrice: number;
    exitPrice: number;
    pnlUsd: number;
    pnlPct: number;
    entryReason: string;
    exitReason: string;
    blockedRules?: string[];
}

export interface EquityPoint {
    timestamp: number;
    equityUsd: number;
    drawdownPct: number;
    cashUsd: number;
    reservedUsd: number;
}

export interface BacktestSummary {
    netPnlUsd: number;
    winRate: number;
    maxDrawdownPct: number;
    sharpe: number;
    totalTrades: number;
}

export interface BacktestOpenPositionAtEnd {
    status: 'OPEN_AT_END';
    symbol: string;
    side: 'BUY' | 'SELL';
    entryTimestamp: number;
    size: number;
    entryPrice: number;
    markPrice: number;
    unrealizedPnlUsd: number;
    unrealizedPnlPct: number;
    reason: string;
}

export interface BacktestResult {
    mode: 'backtest';
    trades: BacktestTrade[];
    summary: BacktestSummary;
    timeline: EquityPoint[];
    window: {
        fromTimestamp: number;
        toTimestamp: number;
        fromIso: string;
        toIso: string;
        timezone: 'UTC';
        includesWarmupCandles: boolean;
        endOfWindowPolicy: BacktestWindowClosePolicy;
    };
    warmup: BacktestWarmup;
    openPositionsAtEnd: BacktestOpenPositionAtEnd[];
    assumptions: {
        fillModel: string;
        slippageModel: string;
        feeModel: string;
        latencyModel: string;
        intraCandleModel: string;
        triggerResolution: string;
        warmupEnforced: boolean;
        deterministicReplay: boolean;
        replayWindow: string;
        endOfWindowPolicy: BacktestWindowClosePolicy;
        disclaimer: string;
    };
    diagnostics: {
        blockedRuleCounts: Record<string, number>;
        entryRejections: Record<string, number>;
        lossReasons: Record<string, number>;
        connectorCalls: {
            fetchOHLCV: number;
            placeOrder: number;
            getPosition: number;
        };
    };
}

export interface BacktestRequestPayload {
    mode: 'backtest';
    profileId?: string;
    strategyConfig?: any;
    symbols: string[];
    timeframe: BacktestTimeframe;
    dateRange: {
        from: string;
        to: string;
    };
    dataSource: {
        type: BacktestDataSourceType;
        payload: any;
    };
    execution?: {
        initialCapitalUsd?: number;
        orderType?: 'market' | 'limit';
        slippageBps?: number;
        feeBps?: number;
        partialFillPct?: number;
        fillOnNextBar?: boolean;
        intraCandlePolicy?: BacktestIntraCandlePolicy;
        triggerTimeframe?: BacktestTriggerTimeframe;
        allowNegativeCash?: boolean;
        enforceWarmup?: boolean;
        forceCloseAtWindowEnd?: boolean;
    };
}
