import React from 'react';
import { AlertTriangle, Clock3, RefreshCcw, Search, ShieldCheck, Undo2 } from 'lucide-react';
import { supabase } from '../lib/supabaseClient';
import { tradingRuntime } from '../lib/runtime';

interface ReconciliationBackfillAuditRow {
    id: number;
    batch_id: string;
    profile_id: string;
    symbol: string;
    trade_id: string;
    exchange_order_id?: string | null;
    exchange_client_order_id?: string | null;
    backfill_order_id?: string | null;
    filled_qty?: number | null;
    filled_price?: number | null;
    filled_at?: string | null;
    dry_run: boolean;
    decision: string;
    reason?: string | null;
    metadata?: Record<string, unknown> | null;
    applied_at?: string | null;
    reverted_at?: string | null;
    created_at: string;
}

interface ReconciliationBackfillBatchSummary {
    batchId: string;
    firstSeenAt: string;
    lastSeenAt: string;
    profileIds: string[];
    symbols: string[];
    totalRows: number;
    byDecision: Record<string, number>;
    dryRunRows: number;
    appliedRows: number;
    revertedRows: number;
}

interface ReconciliationAuditResponse {
    success: boolean;
    rows?: ReconciliationBackfillAuditRow[];
    pagination?: {
        limit?: number;
        offset?: number;
        totalCount?: number;
        hasMore?: boolean;
    };
    error?: string;
}

interface ReconciliationBatchesResponse {
    success: boolean;
    batches?: ReconciliationBackfillBatchSummary[];
    error?: string;
}

interface ReconciliationFilters {
    profileId: string;
    symbol: string;
    batchId: string;
    decision: string;
    days: number;
}

const PAGE_LIMIT = 50;
const BATCH_LIMIT = 20;
const MANUAL_REVIEW_LIMIT = 200;
const MANUAL_REVIEW_REASON = 'missing_fill_evidence_for_large_remainder';
const DEFAULT_FILTERS: ReconciliationFilters = {
    profileId: '',
    symbol: '',
    batchId: '',
    decision: '',
    days: 7
};
const DEFAULT_DECISIONS = ['APPLIED', 'DRY_RUN', 'NO_GO', 'SKIP_EXISTING', 'PENDING_APPLY'];
const DAY_OPTIONS = [1, 3, 7, 14, 30];

const parseResponseError = async (response: Response): Promise<string> => {
    const payload = (await response.json().catch(() => null)) as { error?: string; message?: string } | null;
    return payload?.error || payload?.message || `HTTP ${response.status}`;
};

const formatDateTime = (value?: string | null): string => {
    if (!value) return '-';
    const parsed = Date.parse(value);
    if (!Number.isFinite(parsed)) return '-';
    return new Date(parsed).toLocaleString();
};

const formatNumber = (value?: number | null): string => {
    if (typeof value !== 'number' || !Number.isFinite(value)) return '-';
    return value.toLocaleString(undefined, { maximumFractionDigits: 8 });
};

const shortId = (value: string): string => {
    if (!value) return '-';
    return value.length > 16 ? `${value.slice(0, 8)}...${value.slice(-6)}` : value;
};

const compactTag = (value?: string): string => {
    const token = String(value || '').trim();
    if (!token) return '-';
    return token.length > 24 ? `${token.slice(0, 12)}...${token.slice(-8)}` : token;
};

const safeCount = (value: unknown): number => {
    const parsed = Number(value);
    return Number.isFinite(parsed) ? parsed : 0;
};

const buildQueryParams = (
    filters: ReconciliationFilters,
    limit: number,
    offset?: number,
    decisionOverride?: string
): URLSearchParams => {
    const params = new URLSearchParams();
    params.set('limit', String(limit));
    params.set('days', String(filters.days));
    if (typeof offset === 'number') {
        params.set('offset', String(offset));
    }

    const profileId = filters.profileId.trim();
    const symbol = filters.symbol.trim().toUpperCase();
    const batchId = filters.batchId.trim();
    const decision = decisionOverride ?? filters.decision.trim();

    if (profileId) params.set('profileId', profileId);
    if (symbol) params.set('symbol', symbol);
    if (batchId) params.set('batchId', batchId);
    if (decision) params.set('decision', decision);
    return params;
};

const readMetadataNumber = (metadata: Record<string, unknown> | null | undefined, key: string): number | null => {
    if (!metadata) return null;
    const parsed = Number(metadata[key]);
    return Number.isFinite(parsed) ? parsed : null;
};

export const ReconciliationAuditPanel = () => {
    const [draftFilters, setDraftFilters] = React.useState<ReconciliationFilters>(DEFAULT_FILTERS);
    const [filters, setFilters] = React.useState<ReconciliationFilters>(DEFAULT_FILTERS);
    const [offset, setOffset] = React.useState(0);
    const [rows, setRows] = React.useState<ReconciliationBackfillAuditRow[]>([]);
    const [batches, setBatches] = React.useState<ReconciliationBackfillBatchSummary[]>([]);
    const [manualReviewRows, setManualReviewRows] = React.useState<ReconciliationBackfillAuditRow[]>([]);
    const [totalCount, setTotalCount] = React.useState(0);
    const [isLoading, setIsLoading] = React.useState(false);
    const [error, setError] = React.useState<string | null>(null);
    const [lastLoadedAt, setLastLoadedAt] = React.useState<number | null>(null);
    const [isReverting, setIsReverting] = React.useState<string | null>(null);

    const loadAuditData = React.useCallback(async () => {
        setIsLoading(true);
        setError(null);
        try {
            const { data: sessionData } = await supabase.auth.getSession();
            const accessToken = sessionData.session?.access_token;
            if (!accessToken) {
                throw new Error('Not authenticated');
            }

            const apiUrl = tradingRuntime.tradingApiUrl;
            const auditParams = buildQueryParams(filters, PAGE_LIMIT, offset);
            const batchParams = buildQueryParams(filters, BATCH_LIMIT);
            const manualReviewParams = buildQueryParams(filters, MANUAL_REVIEW_LIMIT, 0, 'NO_GO');

            const [auditResponse, batchResponse, manualReviewResponse] = await Promise.all([
                fetch(`${apiUrl}/api/reconciliation/backfill/audit?${auditParams.toString()}`, {
                    headers: { Authorization: `Bearer ${accessToken}` }
                }),
                fetch(`${apiUrl}/api/reconciliation/backfill/batches?${batchParams.toString()}`, {
                    headers: { Authorization: `Bearer ${accessToken}` }
                }),
                fetch(`${apiUrl}/api/reconciliation/backfill/audit?${manualReviewParams.toString()}`, {
                    headers: { Authorization: `Bearer ${accessToken}` }
                })
            ]);

            if (!auditResponse.ok) {
                throw new Error(await parseResponseError(auditResponse));
            }
            if (!batchResponse.ok) {
                throw new Error(await parseResponseError(batchResponse));
            }
            if (!manualReviewResponse.ok) {
                throw new Error(await parseResponseError(manualReviewResponse));
            }

            const auditPayload = (await auditResponse.json()) as ReconciliationAuditResponse;
            const batchPayload = (await batchResponse.json()) as ReconciliationBatchesResponse;
            const manualReviewPayload = (await manualReviewResponse.json()) as ReconciliationAuditResponse;

            if (!auditPayload.success) {
                throw new Error(auditPayload.error || 'Failed to load reconciliation audit rows');
            }
            if (!batchPayload.success) {
                throw new Error(batchPayload.error || 'Failed to load reconciliation audit batches');
            }
            if (!manualReviewPayload.success) {
                throw new Error(manualReviewPayload.error || 'Failed to load manual review rows');
            }

            setRows(Array.isArray(auditPayload.rows) ? auditPayload.rows : []);
            setBatches(Array.isArray(batchPayload.batches) ? batchPayload.batches : []);
            setManualReviewRows(
                (Array.isArray(manualReviewPayload.rows) ? manualReviewPayload.rows : [])
                    .filter((row) => String(row.reason || '').trim() === MANUAL_REVIEW_REASON)
            );
            setTotalCount(Math.max(0, safeCount(auditPayload.pagination?.totalCount)));
            setLastLoadedAt(Date.now());
        } catch (err) {
            const message = err instanceof Error ? err.message : 'Failed to load reconciliation audit';
            setError(message);
            setRows([]);
            setBatches([]);
            setManualReviewRows([]);
            setTotalCount(0);
        } finally {
            setIsLoading(false);
        }
    }, [filters, offset]);

    React.useEffect(() => {
        void loadAuditData();
    }, [loadAuditData]);

    const decisionOptions = React.useMemo(() => {
        const values = new Set<string>(DEFAULT_DECISIONS);
        for (const row of rows) {
            if (row.decision) values.add(row.decision);
        }
        for (const batch of batches) {
            for (const decision of Object.keys(batch.byDecision || {})) {
                if (decision) values.add(decision);
            }
        }
        return Array.from(values).sort((a, b) => a.localeCompare(b));
    }, [rows, batches]);

    const aggregateDecisionCounts = React.useMemo(() => {
        const counts: Record<string, number> = {};
        for (const batch of batches) {
            for (const [decision, count] of Object.entries(batch.byDecision || {})) {
                counts[decision] = (counts[decision] || 0) + safeCount(count);
            }
        }
        return counts;
    }, [batches]);

    const hasPrevPage = offset > 0;
    const hasNextPage = offset + PAGE_LIMIT < totalCount;
    const pageStart = totalCount === 0 ? 0 : offset + 1;
    const pageEnd = totalCount === 0 ? 0 : Math.min(offset + rows.length, totalCount);

    const handleRevertBatch = async (batchId: string) => {
        if (!confirm(`Are you sure you want to revert batch ${batchId}? This performs status-only rollback for generated BFILL rows (non-destructive).`)) return;
        setIsReverting(batchId);
        setError(null);
        try {
            const { data: sessionData } = await supabase.auth.getSession();
            const accessToken = sessionData.session?.access_token;
            if (!accessToken) throw new Error('Not authenticated');

            const apiUrl = tradingRuntime.tradingApiUrl;

            const response = await fetch(`${apiUrl}/api/admin/revert-backfill-batch`, {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/json',
                    Authorization: `Bearer ${accessToken}`
                },
                body: JSON.stringify({ batchId })
            });

            if (!response.ok) {
                throw new Error(await parseResponseError(response));
            }

            const payload = await response.json();
            if (!payload.success) throw new Error(payload.error || 'Revert failed');

            void loadAuditData();
        } catch (err: any) {
            setError(err.message || 'Failed to revert batch');
        } finally {
            setIsReverting(null);
        }
    };

    const handleApplyFilters = () => {
        setOffset(0);
        setFilters({
            profileId: draftFilters.profileId.trim(),
            symbol: draftFilters.symbol.trim().toUpperCase(),
            batchId: draftFilters.batchId.trim(),
            decision: draftFilters.decision.trim(),
            days: draftFilters.days
        });
    };

    const handleResetFilters = () => {
        setDraftFilters(DEFAULT_FILTERS);
        setFilters(DEFAULT_FILTERS);
        setOffset(0);
    };

    return (
        <div className="space-y-6">
            <section className="bg-[#12131a] border border-white/[0.04] rounded-2xl p-5 space-y-4">
                <div className="flex items-center justify-between gap-3">
                    <div className="flex items-center gap-2">
                        <ShieldCheck size={14} className="text-cyan-400" />
                        <h3 className="text-xs font-bold text-zinc-300 uppercase tracking-wider">Reconciliation EXIT Backfill Audit</h3>
                    </div>
                    <button
                        onClick={() => { void loadAuditData(); }}
                        disabled={isLoading}
                        className={`inline-flex items-center gap-1.5 px-3 py-1.5 rounded-lg text-[10px] font-bold uppercase tracking-wider border transition-colors ${isLoading
                            ? 'bg-zinc-800 border-zinc-700 text-zinc-500 cursor-not-allowed'
                            : 'bg-cyan-500/10 border-cyan-500/20 text-cyan-400 hover:bg-cyan-500/20'
                            }`}
                    >
                        <RefreshCcw size={11} className={isLoading ? 'animate-spin' : ''} />
                        Refresh
                    </button>
                </div>

                <div className="grid grid-cols-2 md:grid-cols-4 gap-3">
                    <div className="px-3 py-2 rounded-lg bg-[#0e0f14] border border-white/[0.04]">
                        <p className="text-[9px] text-zinc-500 uppercase tracking-wider">Rows (Filter Scope)</p>
                        <p className="text-sm font-mono text-white mt-1">{totalCount}</p>
                    </div>
                    <div className="px-3 py-2 rounded-lg bg-[#0e0f14] border border-white/[0.04]">
                        <p className="text-[9px] text-zinc-500 uppercase tracking-wider">Batches (Latest)</p>
                        <p className="text-sm font-mono text-white mt-1">{batches.length}</p>
                    </div>
                    <div className="px-3 py-2 rounded-lg bg-[#0e0f14] border border-white/[0.04]">
                        <p className="text-[9px] text-zinc-500 uppercase tracking-wider">Applied</p>
                        <p className="text-sm font-mono text-emerald-400 mt-1">{aggregateDecisionCounts.APPLIED || 0}</p>
                    </div>
                    <div className="px-3 py-2 rounded-lg bg-[#0e0f14] border border-white/[0.04]">
                        <p className="text-[9px] text-zinc-500 uppercase tracking-wider">No-Go</p>
                        <p className="text-sm font-mono text-orange-400 mt-1">{aggregateDecisionCounts.NO_GO || 0}</p>
                    </div>
                </div>

                <p className="text-[10px] text-zinc-500 flex items-center gap-1.5">
                    <Clock3 size={11} />
                    Last refresh: {lastLoadedAt ? new Date(lastLoadedAt).toLocaleString() : 'Not loaded yet'}
                </p>
            </section>

            <section className="bg-[#12131a] border border-white/[0.04] rounded-2xl p-5 space-y-4">
                <div className="flex items-center gap-2">
                    <Search size={13} className="text-zinc-400" />
                    <h3 className="text-xs font-bold text-zinc-300 uppercase tracking-wider">Filters</h3>
                </div>

                <div className="grid grid-cols-1 md:grid-cols-2 xl:grid-cols-5 gap-3">
                    <input
                        value={draftFilters.profileId}
                        onChange={(event) => setDraftFilters((prev) => ({ ...prev, profileId: event.target.value }))}
                        placeholder="Profile ID"
                        className="px-3 py-2 rounded-lg bg-[#0e0f14] border border-white/[0.06] text-xs text-white placeholder:text-zinc-600 focus:outline-none focus:border-cyan-500/40"
                    />
                    <input
                        value={draftFilters.symbol}
                        onChange={(event) => setDraftFilters((prev) => ({ ...prev, symbol: event.target.value }))}
                        placeholder="Symbol (e.g. SOL/USDT)"
                        className="px-3 py-2 rounded-lg bg-[#0e0f14] border border-white/[0.06] text-xs text-white placeholder:text-zinc-600 focus:outline-none focus:border-cyan-500/40"
                    />
                    <input
                        value={draftFilters.batchId}
                        onChange={(event) => setDraftFilters((prev) => ({ ...prev, batchId: event.target.value }))}
                        placeholder="Batch ID"
                        className="px-3 py-2 rounded-lg bg-[#0e0f14] border border-white/[0.06] text-xs text-white placeholder:text-zinc-600 focus:outline-none focus:border-cyan-500/40"
                    />
                    <select
                        value={draftFilters.decision}
                        onChange={(event) => setDraftFilters((prev) => ({ ...prev, decision: event.target.value }))}
                        className="px-3 py-2 rounded-lg bg-[#0e0f14] border border-white/[0.06] text-xs text-white focus:outline-none focus:border-cyan-500/40"
                    >
                        <option value="">All Decisions</option>
                        {decisionOptions.map((decision) => (
                            <option key={decision} value={decision}>{decision}</option>
                        ))}
                    </select>
                    <select
                        value={String(draftFilters.days)}
                        onChange={(event) => setDraftFilters((prev) => ({ ...prev, days: Number.parseInt(event.target.value, 10) || 7 }))}
                        className="px-3 py-2 rounded-lg bg-[#0e0f14] border border-white/[0.06] text-xs text-white focus:outline-none focus:border-cyan-500/40"
                    >
                        {DAY_OPTIONS.map((days) => (
                            <option key={days} value={String(days)}>{days} day{days === 1 ? '' : 's'}</option>
                        ))}
                    </select>
                </div>

                <div className="flex gap-2">
                    <button
                        onClick={handleApplyFilters}
                        disabled={isLoading}
                        className="px-3 py-2 rounded-lg bg-cyan-500/10 border border-cyan-500/20 text-cyan-400 text-[10px] font-bold uppercase tracking-wider hover:bg-cyan-500/20 disabled:opacity-50"
                    >
                        Apply Filters
                    </button>
                    <button
                        onClick={handleResetFilters}
                        disabled={isLoading}
                        className="px-3 py-2 rounded-lg bg-zinc-800 border border-zinc-700 text-zinc-300 text-[10px] font-bold uppercase tracking-wider hover:bg-zinc-700 disabled:opacity-50"
                    >
                        Reset
                    </button>
                </div>
            </section>

            {error && (
                <div className="flex items-start gap-2 p-4 rounded-xl bg-red-500/[0.06] border border-red-500/20">
                    <AlertTriangle size={14} className="text-red-400 shrink-0 mt-0.5" />
                    <p className="text-xs text-red-400">{error}</p>
                </div>
            )}

            <section className="bg-[#12131a] border border-white/[0.04] rounded-2xl overflow-hidden">
                <div className="px-5 py-3 border-b border-white/[0.04] flex items-center justify-between">
                    <h3 className="text-xs font-bold text-zinc-300 uppercase tracking-wider">Manual Review Queue</h3>
                    <span className="text-[10px] text-orange-400 font-mono">
                        {manualReviewRows.length} large-remainder case{manualReviewRows.length === 1 ? '' : 's'}
                    </span>
                </div>
                <div className="overflow-x-auto">
                    <table className="w-full text-left text-xs">
                        <thead className="bg-[#0e0f14] text-zinc-500 uppercase tracking-wider text-[10px]">
                            <tr>
                                <th className="px-4 py-2">Time</th>
                                <th className="px-4 py-2">Profile</th>
                                <th className="px-4 py-2">Symbol</th>
                                <th className="px-4 py-2">Trade</th>
                                <th className="px-4 py-2">Remaining</th>
                                <th className="px-4 py-2">Open Qty</th>
                                <th className="px-4 py-2">Dust Threshold</th>
                                <th className="px-4 py-2">Evidence</th>
                                <th className="px-4 py-2">Batch</th>
                            </tr>
                        </thead>
                        <tbody className="divide-y divide-white/[0.03]">
                            {manualReviewRows.length === 0 ? (
                                <tr>
                                    <td colSpan={9} className="px-4 py-8 text-center text-zinc-600">
                                        {isLoading ? 'Loading manual review queue...' : 'No large remainder manual-review cases for selected filters'}
                                    </td>
                                </tr>
                            ) : (
                                manualReviewRows.map((row) => {
                                    const metadata = row.metadata || null;
                                    const remaining = readMetadataNumber(metadata, 'remaining');
                                    const openQty = readMetadataNumber(metadata, 'openQty');
                                    const dustThreshold = readMetadataNumber(metadata, 'dustThreshold');
                                    const evidenceRows = readMetadataNumber(metadata, 'evidenceRows');
                                    const evidenceRowsUsed = readMetadataNumber(metadata, 'evidenceRowsUsed');
                                    const unmatchedEvidenceCount = readMetadataNumber(metadata, 'unmatchedEvidenceCount');

                                    return (
                                        <tr key={`manual-${row.id}`} className="hover:bg-white/[0.01]">
                                            <td className="px-4 py-2 text-zinc-400 font-mono">{formatDateTime(row.created_at)}</td>
                                            <td className="px-4 py-2 text-zinc-300 font-mono">{shortId(row.profile_id)}</td>
                                            <td className="px-4 py-2 text-zinc-300">{row.symbol || '-'}</td>
                                            <td className="px-4 py-2 text-zinc-400 font-mono">{row.trade_id || '-'}</td>
                                            <td className="px-4 py-2 text-orange-300 font-mono">{formatNumber(remaining)}</td>
                                            <td className="px-4 py-2 text-zinc-400 font-mono">{formatNumber(openQty)}</td>
                                            <td className="px-4 py-2 text-zinc-400 font-mono">{formatNumber(dustThreshold)}</td>
                                            <td className="px-4 py-2 text-zinc-400 font-mono">
                                                {formatNumber(evidenceRowsUsed)}/{formatNumber(evidenceRows)}
                                                <span className="text-[10px] text-zinc-600 ml-1">unmatched:{formatNumber(unmatchedEvidenceCount)}</span>
                                            </td>
                                            <td className="px-4 py-2 text-zinc-500 font-mono">{shortId(row.batch_id || '')}</td>
                                        </tr>
                                    );
                                })
                            )}
                        </tbody>
                    </table>
                </div>
            </section>

            <section className="bg-[#12131a] border border-white/[0.04] rounded-2xl overflow-hidden">
                <div className="px-5 py-3 border-b border-white/[0.04]">
                    <h3 className="text-xs font-bold text-zinc-300 uppercase tracking-wider">Batch Summary</h3>
                </div>
                <div className="overflow-x-auto">
                    <table className="w-full text-left text-xs">
                        <thead className="bg-[#0e0f14] text-zinc-500 uppercase tracking-wider text-[10px]">
                            <tr>
                                <th className="px-4 py-2">Batch</th>
                                <th className="px-4 py-2">Window</th>
                                <th className="px-4 py-2">Rows</th>
                                <th className="px-4 py-2">Decisions</th>
                                <th className="px-4 py-2">Profiles</th>
                                <th className="px-4 py-2">Symbols</th>
                                <th className="px-4 py-2 text-right">Actions</th>
                            </tr>
                        </thead>
                        <tbody className="divide-y divide-white/[0.03]">
                            {batches.length === 0 ? (
                                <tr>
                                    <td colSpan={7} className="px-4 py-8 text-center text-zinc-600">
                                        {isLoading ? 'Loading batch summaries...' : 'No backfill batches found for selected filters'}
                                    </td>
                                </tr>
                            ) : (
                                batches.map((batch) => (
                                    <tr key={batch.batchId} className="hover:bg-white/[0.01]">
                                        <td className="px-4 py-2 font-mono text-zinc-300">{shortId(batch.batchId)}</td>
                                        <td className="px-4 py-2 text-zinc-400">
                                            <div>{formatDateTime(batch.firstSeenAt)}</div>
                                            <div className="text-[10px] text-zinc-600">to {formatDateTime(batch.lastSeenAt)}</div>
                                        </td>
                                        <td className="px-4 py-2 text-white font-mono">{batch.totalRows}</td>
                                        <td className="px-4 py-2">
                                            <div className="flex flex-wrap gap-1">
                                                {Object.entries(batch.byDecision || {}).map(([decision, count]) => (
                                                    <span
                                                        key={`${batch.batchId}-${decision}`}
                                                        className="px-1.5 py-0.5 rounded bg-white/[0.04] text-zinc-300 text-[10px] font-mono"
                                                    >
                                                        {decision}:{count}
                                                    </span>
                                                ))}
                                            </div>
                                        </td>
                                        <td className="px-4 py-2 text-zinc-400">{batch.profileIds.map(shortId).join(', ') || '-'}</td>
                                        <td className="px-4 py-2 text-zinc-400">{batch.symbols.join(', ') || '-'}</td>
                                        <td className="px-4 py-2 text-right">
                                            {batch.appliedRows > 0 && (
                                                <button
                                                    onClick={() => handleRevertBatch(batch.batchId)}
                                                    disabled={isReverting === batch.batchId || isLoading}
                                                    title="Revert APPLIED BFILL rows in this batch via status-only rollback"
                                                    className="inline-flex items-center gap-1.5 px-2 py-1 rounded bg-red-500/10 border border-red-500/20 text-red-400 text-[10px] font-bold uppercase hover:bg-red-500/20 disabled:opacity-50"
                                                >
                                                    <Undo2 size={10} className={isReverting === batch.batchId ? 'animate-spin' : ''} />
                                                    Revert Batch
                                                </button>
                                            )}
                                        </td>
                                    </tr>
                                ))
                            )}
                        </tbody>
                    </table>
                </div>
            </section>

            <section className="bg-[#12131a] border border-white/[0.04] rounded-2xl overflow-hidden">
                <div className="px-5 py-3 border-b border-white/[0.04] flex items-center justify-between">
                    <h3 className="text-xs font-bold text-zinc-300 uppercase tracking-wider">Audit Rows</h3>
                    <span className="text-[10px] text-zinc-500 font-mono">
                        Showing {pageStart}-{pageEnd} of {totalCount}
                    </span>
                </div>
                <div className="overflow-x-auto">
                    <table className="w-full text-left text-xs">
                        <thead className="bg-[#0e0f14] text-zinc-500 uppercase tracking-wider text-[10px]">
                            <tr>
                                <th className="px-4 py-2">Time</th>
                                <th className="px-4 py-2">Decision</th>
                                <th className="px-4 py-2">Profile</th>
                                <th className="px-4 py-2">Symbol</th>
                                <th className="px-4 py-2">Trade</th>
                                <th className="px-4 py-2">Qty / Price</th>
                                <th className="px-4 py-2">Reason</th>
                                <th className="px-4 py-2">Exchange Tag</th>
                                <th className="px-4 py-2">Order IDs</th>
                            </tr>
                        </thead>
                        <tbody className="divide-y divide-white/[0.03]">
                            {rows.length === 0 ? (
                                <tr>
                                    <td colSpan={9} className="px-4 py-10 text-center text-zinc-600">
                                        {isLoading ? 'Loading audit rows...' : 'No audit rows for selected filters'}
                                    </td>
                                </tr>
                            ) : (
                                rows.map((row) => {
                                    const metadata = row.metadata || {};
                                    const matchedBy = typeof metadata.matchedBy === 'string' ? metadata.matchedBy : null;
                                    const exchangeSubTag = typeof metadata.exchangeSubTag === 'string'
                                        ? metadata.exchangeSubTag
                                        : '';
                                    return (
                                        <tr key={row.id} className="hover:bg-white/[0.01]">
                                            <td className="px-4 py-2 text-zinc-400 font-mono">{formatDateTime(row.created_at)}</td>
                                            <td className="px-4 py-2">
                                                <span className={`px-1.5 py-0.5 rounded text-[10px] font-bold ${row.decision === 'APPLIED'
                                                    ? 'bg-emerald-500/10 text-emerald-400'
                                                    : row.decision === 'NO_GO'
                                                        ? 'bg-orange-500/10 text-orange-400'
                                                        : 'bg-zinc-700/40 text-zinc-300'
                                                    }`}
                                                >
                                                    {row.decision}
                                                </span>
                                                {row.dry_run && <span className="ml-1 text-[10px] text-zinc-500">DRY</span>}
                                            </td>
                                            <td className="px-4 py-2 text-zinc-300 font-mono">{shortId(row.profile_id)}</td>
                                            <td className="px-4 py-2 text-zinc-300">{row.symbol || '-'}</td>
                                            <td className="px-4 py-2 text-zinc-400 font-mono">{row.trade_id || '-'}</td>
                                            <td className="px-4 py-2 text-zinc-400 font-mono">
                                                <div>{formatNumber(row.filled_qty)}</div>
                                                <div className="text-[10px] text-zinc-600">{formatNumber(row.filled_price)}</div>
                                            </td>
                                            <td className="px-4 py-2 text-zinc-400">
                                                <div>{row.reason || '-'}</div>
                                                {matchedBy && <div className="text-[10px] text-zinc-600">matchedBy: {matchedBy}</div>}
                                            </td>
                                            <td className="px-4 py-2 text-zinc-500 font-mono" title={exchangeSubTag || ''}>
                                                {compactTag(exchangeSubTag)}
                                            </td>
                                            <td className="px-4 py-2 text-zinc-500 font-mono">
                                                <div>bf: {shortId(row.backfill_order_id || '')}</div>
                                                <div className="text-[10px]">ex: {shortId(row.exchange_order_id || '')}</div>
                                            </td>
                                        </tr>
                                    );
                                })
                            )}
                        </tbody>
                    </table>
                </div>

                <div className="px-5 py-3 border-t border-white/[0.04] flex items-center justify-end gap-2">
                    <button
                        onClick={() => setOffset((prev) => Math.max(0, prev - PAGE_LIMIT))}
                        disabled={!hasPrevPage || isLoading}
                        className="px-3 py-1.5 rounded-lg bg-zinc-800 border border-zinc-700 text-[10px] text-zinc-300 uppercase tracking-wider disabled:opacity-40"
                    >
                        Previous
                    </button>
                    <button
                        onClick={() => setOffset((prev) => prev + PAGE_LIMIT)}
                        disabled={!hasNextPage || isLoading}
                        className="px-3 py-1.5 rounded-lg bg-zinc-800 border border-zinc-700 text-[10px] text-zinc-300 uppercase tracking-wider disabled:opacity-40"
                    >
                        Next
                    </button>
                </div>
            </section>
        </div>
    );
};
