from ..queries import query_data_as_dict, query_data, derive_timestamp

def fetch_filtered_transactions_paginated(
    sol_amount, 
    operator=">",
    timestamp_operator=None, 
    limit=200, 
    offset=0,
    years=0,
    months=0,
    weeks=0,
    days=0,
    hours=0,
    minutes=0,
    seconds=0,
    timestamp=None,
    *args,
    **kwargs
):
    # Build the base query and include a LEFT JOIN to get tcn counts per pair.
    base_query = f"""
        FROM 
            transactions t
        JOIN 
            pairs p ON t.pair_id = p.id
        LEFT JOIN 
            metadata m ON p.meta_id = m.id
        LEFT JOIN (
            SELECT 
                pair_id, 
                SUM(jsonb_array_length(tcns)) AS tcn_count
            FROM 
                transactions
            GROUP BY 
                pair_id
        ) tcns_count ON p.id = tcns_count.pair_id
        WHERE 
            t.signature IN (
                SELECT signature FROM pairs WHERE signature IS NOT NULL
            )
        AND 
            t.program_id = p.program_id
        AND 
            EXISTS (
                SELECT 1
                FROM jsonb_array_elements(t.tcns) AS elem
                WHERE (elem ->> 'sol_amount')::numeric {operator} %s
    """
    params = [sol_amount]

    ts = derive_timestamp(
        years=years,
        months=months,
        weeks=weeks,
        days=days,
        hours=hours,
        minutes=minutes,
        seconds=seconds,
        timestamp=timestamp
    )
    
    if ts and timestamp_operator:
        base_query += f" AND (elem ->> 'timestamp')::bigint {timestamp_operator} %s"
        params.append(int(ts))
    
    # Close the EXISTS clause.
    base_query += ")"
    
    # Build the final data query including the tcn_count column.
    data_query = f"""
        SELECT
            p.*, 
            m.*,
            t.*,
            COALESCE(tcns_count.tcn_count, 0) AS tcn_count
        {base_query}
        ORDER BY t.updated_at DESC
        LIMIT %s OFFSET %s;
    """
    data_params = params + [limit, offset]
    transactions = query_data_as_dict(data_query, data_params)
    
    return transactions
