"""Core record processing: validate, transform, and deduplicate."""

import copy


def process_records(records: list[dict], config: dict) -> list[dict]:
    """Process records: validate against config, transform, and deduplicate.

    Each record is validated against the config schema, transformed with
    computed fields, then the full result set is deduplicated before return.
    """
    validated = []
    for record in records:
        result = _validate_and_transform(record, config)
        if result is not None:
            validated.append(result)

    return _deduplicate(validated)


def _validate_and_transform(record: dict, config: dict) -> dict | None:
    """Validate record against config and add computed fields.

    Returns transformed record or None if validation fails.
    """
    # Defensive copy to avoid mutating caller's config
    cfg = copy.deepcopy(config)
    required = cfg.get("required_fields", [])
    defaults = cfg.get("defaults", {})

    for field in required:
        if field not in record:
            return None

    # Build output with schema defaults + record values
    output = {}
    for key, default in defaults.items():
        output[key] = record.get(key, default)

    # Copy remaining record fields
    for key, value in record.items():
        if key not in output:
            output[key] = value

    # Add computed fields
    output["_source_hash"] = hex(hash(record.get("source", "")) & 0xFFFFFFFF)
    return output


def _is_duplicate(record: dict, existing: list[dict]) -> bool:
    """Check if a record duplicates any existing record."""
    for other in existing:
        if _fields_match(record, other):
            return True
    return False


def _fields_match(a: dict, b: dict) -> bool:
    """Compare two records on all non-computed fields."""
    for key in a:
        if key.startswith("_"):
            continue
        if a[key] != b.get(key):
            return False
    for key in b:
        if key.startswith("_"):
            continue
        if key not in a:
            return False
    return True


def _deduplicate(records: list[dict]) -> list[dict]:
    """Remove duplicate records, keeping first occurrence.

    Two records are considered duplicates if all non-computed fields match.
    """
    unique = []
    for record in records:
        if not _is_duplicate(record, unique):
            unique.append(record)
    return unique
