"""Auto-generated Prefect flow for {{ contract_id }}.

Generated by Griot scaffold on {{ generated_at }}.
"""
from __future__ import annotations

import os

from prefect import flow, task

from griot_core.integrations.prefect import griot_validate
from griot_core.reporting.reporter import RegistryReporter


@task(name="extract_{{ contract_id | replace('-', '_') }}")
def extract():
    """Extract data from {{ source | default('source') }}.

    TODO: Implement extraction logic.
    """
    raise NotImplementedError("Implement extraction")


@task(name="validate_{{ contract_id | replace('-', '_') }}")
def validate(data):
    """Validate data against the {{ contract_id }} contract."""
    result = griot_validate(
        contract_id="{{ contract_id }}",
        profile="{{ profile | default('default') }}",
    )
    if not result.is_valid:
        raise Exception(f"Validation failed: {result.error_count} errors")
    return result


@task(name="report_{{ contract_id | replace('-', '_') }}")
def report_to_registry(validation_result):
    """Report validation results to the Griot Registry."""
    with RegistryReporter(
        registry_url="{{ registry_url | default('http://localhost:8000') }}",
        client_id=os.environ.get("GRIOT_SA_CLIENT_ID"),
        client_secret=os.environ.get("GRIOT_SA_SECRET"),
    ) as reporter:
        reporter.report(
            report=validation_result,
            contract_id="{{ contract_id }}",
            environment="production",
        )


@task(name="transform_{{ contract_id | replace('-', '_') }}")
def transform(data):
    """Transform data according to contract schema.

    TODO: Implement transformation logic.
    """
    raise NotImplementedError("Implement transformation")


@task(name="load_{{ contract_id | replace('-', '_') }}")
def load(data):
    """Load data to {{ target | default('target') }}.

    TODO: Implement load logic.
    """
    raise NotImplementedError("Implement load to {{ target | default('target') }}")


@flow(name="{{ contract_id }}-pipeline")
def {{ contract_id | replace('-', '_') }}_pipeline():
    """End-to-end pipeline for {{ contract_id }}."""
    raw = extract()
    result = validate(raw)
    report_to_registry(result)
    transformed = transform(raw)
    load(transformed)


if __name__ == "__main__":
    {{ contract_id | replace('-', '_') }}_pipeline()
