"""Auto-generated Dagster job for {{ contract_id }}.

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

from dagster import (
    AssetExecutionContext,
    Definitions,
    asset,
    define_asset_job,
)

from griot_core.integrations.dagster import GriotResource


griot = GriotResource(
    registry_url="{{ registry_url | default('http://localhost:8000') }}",
    default_profile="{{ profile | default('default') }}",
    # Credentials are read from env vars: GRIOT_SA_CLIENT_ID, GRIOT_SA_SECRET
)


@asset(
    group_name="{{ contract_id | replace('-', '_') }}",
    description="Extract data for {{ contract_id }}",
)
def extract_{{ contract_id | replace('-', '_') }}(context: AssetExecutionContext):
    """Extract data from {{ source | default('source') }}.

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


@asset(
    group_name="{{ contract_id | replace('-', '_') }}",
    deps=[extract_{{ contract_id | replace('-', '_') }}],
    description="Validate {{ contract_id }} against contract",
)
def validate_{{ contract_id | replace('-', '_') }}(
    context: AssetExecutionContext,
    griot: GriotResource,
):
    """Validate extracted data against the 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")
    context.log.info(f"Validation passed: {result.row_count} rows")
    return result


@asset(
    group_name="{{ contract_id | replace('-', '_') }}",
    deps=[validate_{{ contract_id | replace('-', '_') }}],
    description="Transform data for {{ contract_id }}",
)
def transform_{{ contract_id | replace('-', '_') }}(context: AssetExecutionContext):
    """Transform data according to contract schema.

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


@asset(
    group_name="{{ contract_id | replace('-', '_') }}",
    deps=[transform_{{ contract_id | replace('-', '_') }}],
    description="Load {{ contract_id }} to {{ target | default('target') }}",
)
def load_{{ contract_id | replace('-', '_') }}(context: AssetExecutionContext):
    """Load data to {{ target | default('target') }}.

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


# --Job definition ---------------------------------------------------

{{ contract_id | replace('-', '_') }}_job = define_asset_job(
    name="{{ contract_id | replace('-', '_') }}_pipeline",
    selection=[
        extract_{{ contract_id | replace('-', '_') }},
        validate_{{ contract_id | replace('-', '_') }},
        transform_{{ contract_id | replace('-', '_') }},
        load_{{ contract_id | replace('-', '_') }},
    ],
)

defs = Definitions(
    assets=[
        extract_{{ contract_id | replace('-', '_') }},
        validate_{{ contract_id | replace('-', '_') }},
        transform_{{ contract_id | replace('-', '_') }},
        load_{{ contract_id | replace('-', '_') }},
    ],
    jobs=[{{ contract_id | replace('-', '_') }}_job],
    resources={"griot": griot},
)
