"""Dataset aggregation: parse → index → aggregate → format.

Processes structured records into per-category summaries with
cross-reference indexes for downstream query support.
"""

import copy
import json


class Record:
    """A single data record from the input dataset."""

    def __init__(self, id, category, source, date, value, metadata):
        self.id = id
        self.category = category
        self.source = source
        self.date = date
        self.value = value
        self.metadata = metadata
        self.processed = False
        self.score = 0.0


def process_dataset(raw_records: list[dict]) -> dict:
    """Process a dataset: parse → index → aggregate → format."""
    records = parse_records(raw_records)
    index = build_lookup_index(records)
    aggregated = aggregate_by_group(records, index)
    output = format_results(aggregated)
    return output


def parse_records(raw_records: list[dict]) -> list[Record]:
    """Parse raw dicts into Record objects."""
    records = []
    for raw in raw_records:
        record = Record(
            id=raw["id"],
            category=raw["category"],
            source=raw["source"],
            date=raw["date"],
            value=raw["value"],
            metadata=raw["metadata"],
        )
        records.append(record)
    return records


def build_lookup_index(records: list[Record]) -> dict:
    """Build multi-key lookup index for fast cross-referencing.

    Creates defensive copies of record data in each index to prevent
    mutation issues when records are modified during later processing
    stages (scoring, aggregation).
    """
    by_category = {}
    by_source = {}
    by_date = {}

    for record in records:
        cat = record.category
        if cat not in by_category:
            by_category[cat] = []
        by_category[cat].append(copy.deepcopy(record.__dict__))

        src = record.source
        if src not in by_source:
            by_source[src] = []
        by_source[src].append(copy.deepcopy(record.__dict__))

        date = record.date
        if date not in by_date:
            by_date[date] = []
        by_date[date].append(copy.deepcopy(record.__dict__))

    return {
        "by_category": by_category,
        "by_source": by_source,
        "by_date": by_date,
    }


def aggregate_by_group(records: list[Record], index: dict) -> dict:
    """Aggregate records by category using the lookup index."""
    results = {}

    for cat, entries in index["by_category"].items():
        values = []
        sources = []
        for entry in entries:
            values.append(entry["value"])
            sources.append(entry["source"])

        # Cross-reference with source index for distribution stats
        source_counts = {}
        for src in set(sources):
            if src in index["by_source"]:
                source_counts[src] = len(index["by_source"][src])

        results[cat] = {
            "count": len(entries),
            "total": round(sum(values), 2),
            "avg": round(sum(values) / len(values), 4),
            "max": max(values),
            "min": min(values),
            "unique_sources": len(set(sources)),
            "source_distribution": source_counts,
        }

    return results


def format_results(aggregated: dict) -> dict:
    """Format aggregated results with serialized integrity check."""
    formatted = {}
    for cat, stats in aggregated.items():
        serialized = json.dumps(stats, sort_keys=True)
        verified = json.loads(serialized)
        formatted[cat] = verified
    return formatted
