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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
#!/usr/bin/env python3
"""
Normalize DataForSEO API responses for consistent consumption by claude-seo skills.

Provides generic helpers (extract_items, truncate_for_context, format_table) and
per-module normalizers (merchant, social, reviews, etc.).

Usage:
    python dataforseo_normalize.py <input.json> [--module merchant|social|reviews]

Can also be imported as a library:
    from dataforseo_normalize import extract_items, normalize_merchant, truncate_for_context

Original concept: Matej Marjanovic (Pro Hub Challenge)
"""

import argparse
import json
import math
import sys
from typing import Any, Optional


# ---------------------------------------------------------------------------
# Generic helpers
# ---------------------------------------------------------------------------

def extract_items(response: dict[str, Any]) -> list[dict[str, Any]]:
    """
    Extract data items from a DataForSEO response envelope.

    DataForSEO responses follow a consistent structure:
        { "tasks": [ { "result": [ { "items": [...] } ] } ] }

    This function walks the envelope and collects all items into a flat list.

    Args:
        response: Raw DataForSEO JSON response.

    Returns:
        Flat list of item dicts.
    """
    items: list[dict[str, Any]] = []
    for task in response.get("tasks", []):
        if task.get("status_code") != 20000:
            continue
        for result in task.get("result", []) or []:
            result_items = result.get("items")
            if result_items and isinstance(result_items, list):
                items.extend(result_items)
    return items


def truncate_for_context(
    data: list[dict[str, Any]],
    max_tokens: int = 4000,
    chars_per_token: float = 3.5,
) -> list[dict[str, Any]]:
    """
    Truncate a list of result items to fit within an LLM context budget.

    Uses a simple character-based estimation (avg ~3.5 chars/token for JSON).
    Items are kept in order; the list is sliced when the budget is exceeded.

    Args:
        data: List of normalized item dicts.
        max_tokens: Approximate token budget.
        chars_per_token: Average characters per token for estimation.

    Returns:
        Truncated list that fits within the budget.
    """
    max_chars = int(max_tokens * chars_per_token)
    total_chars = 0
    truncated: list[dict[str, Any]] = []

    for item in data:
        item_str = json.dumps(item, ensure_ascii=False)
        item_chars = len(item_str)
        if total_chars + item_chars > max_chars:
            break
        truncated.append(item)
        total_chars += item_chars

    return truncated


def format_markdown_table(
    items: list[dict[str, Any]],
    columns: list[str],
    headers: Optional[list[str]] = None,
    max_rows: int = 50,
) -> str:
    """
    Format a list of dicts as a Markdown table.

    Args:
        items: List of dicts to tabulate.
        columns: Dict keys to include as columns.
        headers: Human-readable column headers (defaults to column keys).
        max_rows: Maximum number of rows to include.

    Returns:
        Markdown-formatted table string.
    """
    if not items:
        return "_No data available._"

    display_headers = headers or [col.replace("_", " ").title() for col in columns]

    # Header row
    lines = [
        "| " + " | ".join(display_headers) + " |",
        "| " + " | ".join(["---"] * len(columns)) + " |",
    ]

    # Data rows
    for item in items[:max_rows]:
        row_values = []
        for col in columns:
            val = item.get(col, "")
            if val is None:
                val = "--"
            elif isinstance(val, float):
                val = f"{val:.2f}" if not val.is_integer() else f"{int(val)}"
            else:
                val = str(val)
            row_values.append(val)
        lines.append("| " + " | ".join(row_values) + " |")

    if len(items) > max_rows:
        lines.append(f"\n_...and {len(items) - max_rows} more rows (truncated)._")

    return "\n".join(lines)


def compute_statistics(values: list[float]) -> dict[str, Optional[float]]:
    """
    Compute basic descriptive statistics for a list of numeric values.

    Args:
        values: List of floats.

    Returns:
        Dict with min, max, mean, median, p25, p75, std_dev.
    """
    if not values:
        return {
            "min": None,
            "max": None,
            "mean": None,
            "median": None,
            "p25": None,
            "p75": None,
            "std_dev": None,
            "count": 0,
        }

    sorted_vals = sorted(values)
    n = len(sorted_vals)
    mean = sum(sorted_vals) / n

    def _percentile(data: list[float], p: float) -> float:
        idx = (p / 100) * (len(data) - 1)
        lower = int(math.floor(idx))
        upper = int(math.ceil(idx))
        if lower == upper:
            return data[lower]
        frac = idx - lower
        return data[lower] * (1 - frac) + data[upper] * frac

    variance = sum((x - mean) ** 2 for x in sorted_vals) / n
    std_dev = math.sqrt(variance)

    return {
        "min": round(sorted_vals[0], 2),
        "max": round(sorted_vals[-1], 2),
        "mean": round(mean, 2),
        "median": round(_percentile(sorted_vals, 50), 2),
        "p25": round(_percentile(sorted_vals, 25), 2),
        "p75": round(_percentile(sorted_vals, 75), 2),
        "std_dev": round(std_dev, 2),
        "count": n,
    }


# ---------------------------------------------------------------------------
# Merchant normalizer
# ---------------------------------------------------------------------------

def _normalize_price(raw: Any) -> Optional[float]:
    """Convert various price formats to float."""
    if raw is None:
        return None
    if isinstance(raw, (int, float)):
        return round(float(raw), 2)
    if isinstance(raw, str):
        cleaned = raw.replace("$", "").replace(",", "").replace(" ", "").strip()
        try:
            return round(float(cleaned), 2)
        except ValueError:
            return None
    return None


def _normalize_currency(raw: Any) -> str:
    """Normalize currency to ISO 4217 code."""
    if not raw:
        return "USD"
    text = str(raw).upper().strip()
    # Handle common symbols
    symbol_map = {"$": "USD", "EUR": "EUR", "GBP": "GBP", "JPY": "JPY"}
    return symbol_map.get(text, text[:3] if len(text) >= 3 else "USD")


def _normalize_availability(raw: Any) -> str:
    """Normalize availability to enum string."""
    if not raw:
        return "unknown"
    text = str(raw).lower().strip()
    if "in_stock" in text or "in stock" in text:
        return "in_stock"
    if "out_of_stock" in text or "out of stock" in text:
        return "out_of_stock"
    if "preorder" in text or "pre-order" in text or "pre_order" in text:
        return "preorder"
    return "unknown"


def _safe_float(val: Any, default: float = 0.0) -> float:
    """Safely convert a value to float."""
    if val is None:
        return default
    try:
        return float(val)
    except (ValueError, TypeError):
        return default


def _safe_int(val: Any, default: int = 0) -> int:
    """Safely convert a value to int."""
    if val is None:
        return default
    try:
        return int(val)
    except (ValueError, TypeError):
        return default


def normalize_merchant(items: list[dict[str, Any]]) -> list[dict[str, Any]]:
    """
    Normalize Google Shopping and Amazon product data.

    Handles variations in DataForSEO response format:
    - Price as string or number
    - Rating as nested dict or flat float
    - Availability as various string formats

    Args:
        items: Raw item dicts from DataForSEO Merchant API.

    Returns:
        List of normalized product dicts.
    """
    if not items:
        return []
    normalized = []
    for item in items:
        # Handle rating as dict or scalar
        rating_raw = item.get("rating")
        if isinstance(rating_raw, dict):
            rating_val = _safe_float(rating_raw.get("value") or rating_raw.get("rating_value"))
        else:
            rating_val = _safe_float(rating_raw)

        product = {
            "title": str(item.get("title", "")),
            "price": _normalize_price(item.get("price")),
            "currency": _normalize_currency(item.get("currency")),
            "seller": str(item.get("seller", item.get("seller_name", ""))),
            "rating": round(rating_val, 1),
            "reviews_count": _safe_int(item.get("reviews_count")),
            "url": str(item.get("url", "")),
            "image_url": str(item.get("image_url", item.get("marketplace_url", ""))),
            "availability": _normalize_availability(item.get("availability")),
        }

        # Amazon-specific fields
        if "asin" in item:
            product["asin"] = item["asin"]
        if "is_prime" in item:
            product["is_prime"] = bool(item["is_prime"])
        if "is_best_seller" in item:
            product["is_best_seller"] = bool(item["is_best_seller"])

        # Google-specific fields
        if "product_id" in item:
            product["product_id"] = item["product_id"]
        if "delivery_info" in item:
            product["delivery_info"] = str(item["delivery_info"])

        normalized.append(product)
    return normalized


# ---------------------------------------------------------------------------
# Social normalizer (placeholder for future seo-social skill)
# ---------------------------------------------------------------------------

def normalize_social(items: list[dict[str, Any]]) -> list[dict[str, Any]]:
    """
    Normalize social signal data from DataForSEO.

    Placeholder for future seo-social skill integration. Handles social
    media engagement metrics, share counts, and platform-specific data.

    Args:
        items: Raw item dicts from DataForSEO social endpoints.

    Returns:
        List of normalized social signal dicts.
    """
    if not items:
        return []
    normalized = []
    for item in items:
        signal = {
            "url": str(item.get("url", "")),
            "title": str(item.get("title", "")),
            "platform": str(item.get("platform", item.get("source", "unknown"))),
            "engagement_count": _safe_int(
                item.get("engagement_count", item.get("social_count", 0))
            ),
            "likes": _safe_int(item.get("likes")),
            "shares": _safe_int(item.get("shares")),
            "comments": _safe_int(item.get("comments")),
            "date": str(item.get("date", item.get("datetime", ""))),
            "sentiment": str(item.get("sentiment", "neutral")),
        }
        normalized.append(signal)
    return normalized


# ---------------------------------------------------------------------------
# Reviews normalizer (placeholder for future expansion)
# ---------------------------------------------------------------------------

def normalize_reviews(items: list[dict[str, Any]]) -> list[dict[str, Any]]:
    """
    Normalize review data from DataForSEO.

    Handles business listing reviews, product reviews, and Google Maps reviews.

    Args:
        items: Raw item dicts from DataForSEO review endpoints.

    Returns:
        List of normalized review dicts.
    """
    if not items:
        return []
    normalized = []
    for item in items:
        review = {
            "author": str(item.get("author", item.get("profile_name", "Anonymous"))),
            "rating": round(_safe_float(item.get("rating")), 1),
            "text": str(item.get("text", item.get("review_text", ""))),
            "date": str(item.get("date", item.get("time_ago", ""))),
            "source": str(item.get("source", "")),
            "verified": bool(item.get("is_verified", False)),
            "helpful_count": _safe_int(item.get("helpful_count", item.get("likes", 0))),
        }
        normalized.append(review)
    return normalized


# ---------------------------------------------------------------------------
# CLI
# ---------------------------------------------------------------------------

def main():
    parser = argparse.ArgumentParser(
        description="Normalize DataForSEO API responses for claude-seo skills"
    )
    parser.add_argument("input", help="Input JSON file (use - for stdin)")
    parser.add_argument(
        "--module",
        choices=["merchant", "social", "reviews"],
        default="merchant",
        help="Normalizer module to use (default: merchant)",
    )
    parser.add_argument(
        "--max-tokens",
        type=int,
        default=0,
        dest="max_tokens",
        help="Truncate output to fit token budget (0 = no truncation)",
    )
    parser.add_argument(
        "--table",
        action="store_true",
        help="Output as Markdown table instead of JSON",
    )
    parser.add_argument(
        "--columns",
        help="Comma-separated column names for table output",
    )
    parser.add_argument(
        "--stats",
        action="store_true",
        help="Include price/rating statistics in output",
    )

    args = parser.parse_args()

    # Read input
    if args.input == "-":
        data = json.load(sys.stdin)
    else:
        with open(args.input) as f:
            data = json.load(f)

    # Extract items from response envelope
    items = extract_items(data) if "tasks" in data else data
    if isinstance(items, dict):
        items = [items]

    # Normalize
    normalizers = {
        "merchant": normalize_merchant,
        "social": normalize_social,
        "reviews": normalize_reviews,
    }
    normalized = normalizers[args.module](items)

    # Truncate if requested
    if args.max_tokens > 0:
        normalized = truncate_for_context(normalized, max_tokens=args.max_tokens)

    # Output
    if args.table:
        columns = (
            args.columns.split(",")
            if args.columns
            else _default_columns(args.module)
        )
        print(format_markdown_table(normalized, columns))
    else:
        output: dict[str, Any] = {
            "module": args.module,
            "total_items": len(normalized),
            "items": normalized,
        }

        if args.stats and args.module == "merchant":
            prices = [p["price"] for p in normalized if p.get("price") is not None]
            ratings = [p["rating"] for p in normalized if p.get("rating", 0) > 0]
            output["price_stats"] = compute_statistics(prices)
            output["rating_stats"] = compute_statistics(ratings)

        json.dump(output, sys.stdout, indent=2)


def _default_columns(module: str) -> list[str]:
    """Return default table columns for each module."""
    defaults = {
        "merchant": ["title", "price", "currency", "seller", "rating", "reviews_count", "availability"],
        "social": ["platform", "title", "engagement_count", "likes", "shares", "date"],
        "reviews": ["author", "rating", "text", "date", "source", "verified"],
    }
    return defaults.get(module, ["title"])


if __name__ == "__main__":
    main()