"""Versioned, provider-neutral execution cost resolution."""
from __future__ import annotations

from dataclasses import dataclass
from decimal import Decimal, ROUND_CEILING
from math import ceil


@dataclass(frozen=True)
class ExecutionUsage:
    provider: str
    target: str
    runner_class: str
    duration_seconds: int


@dataclass(frozen=True)
class ExecutionPrice:
    pricing_version: str
    provider_cost_microusd: int
    credit_charge: int


def calculate_execution_price(
    usage: ExecutionUsage,
    *,
    pricing_version: str = "github-linux-2026-01",
    provider_rate_usd_per_minute: Decimal = Decimal("0.008"),
    credits_per_minute: int = 1,
) -> ExecutionPrice:
    """Price actual runner time; queued workflow time is not included.

    Monetary provider cost and customer credits are intentionally separate.  The
    integer micro-USD snapshot makes completed charges reproducible after rates
    change, while the credit schedule can include margin/rounding independently.
    """
    seconds = max(0, int(usage.duration_seconds))
    minutes = Decimal(seconds) / Decimal(60)
    micros = (minutes * provider_rate_usd_per_minute * Decimal(1_000_000)).quantize(
        Decimal("1"), rounding=ROUND_CEILING
    )
    return ExecutionPrice(
        pricing_version=pricing_version,
        provider_cost_microusd=int(micros),
        credit_charge=ceil(seconds / 60) * max(0, int(credits_per_minute)) if seconds else 0,
    )
