from __future__ import annotations


from prometheus_client import Counter, Histogram, Gauge

IMPORT_DURATION = Histogram(
    "calminer_import_duration_seconds",
    "Duration of import preview and commit operations",
    labelnames=("dataset", "action", "status"),
)

IMPORT_TOTAL = Counter(
    "calminer_import_total",
    "Count of import operations",
    labelnames=("dataset", "action", "status"),
)

EXPORT_DURATION = Histogram(
    "calminer_export_duration_seconds",
    "Duration of export operations",
    labelnames=("dataset", "status", "format"),
)

EXPORT_TOTAL = Counter(
    "calminer_export_total",
    "Count of export operations",
    labelnames=("dataset", "status", "format"),
)

# General performance metrics
REQUEST_DURATION = Histogram(
    "calminer_request_duration_seconds",
    "Duration of HTTP requests",
    labelnames=("method", "endpoint", "status"),
)

REQUEST_TOTAL = Counter(
    "calminer_request_total",
    "Count of HTTP requests",
    labelnames=("method", "endpoint", "status"),
)

ACTIVE_CONNECTIONS = Gauge(
    "calminer_active_connections",
    "Number of active connections",
)

DB_CONNECTIONS = Gauge(
    "calminer_db_connections",
    "Number of database connections",
)

# Business metrics
PROJECT_OPERATIONS = Counter(
    "calminer_project_operations_total",
    "Count of project operations",
    labelnames=("operation", "status"),
)

SCENARIO_OPERATIONS = Counter(
    "calminer_scenario_operations_total",
    "Count of scenario operations",
    labelnames=("operation", "status"),
)

SIMULATION_RUNS = Counter(
    "calminer_simulation_runs_total",
    "Count of Monte Carlo simulation runs",
    labelnames=("status",),
)

SIMULATION_DURATION = Histogram(
    "calminer_simulation_duration_seconds",
    "Duration of Monte Carlo simulations",
    labelnames=("status",),
)


def observe_import(action: str, dataset: str, status: str, seconds: float) -> None:
    IMPORT_TOTAL.labels(dataset=dataset, action=action, status=status).inc()
    IMPORT_DURATION.labels(dataset=dataset, action=action,
                           status=status).observe(seconds)


def observe_export(dataset: str, status: str, export_format: str, seconds: float) -> None:
    EXPORT_TOTAL.labels(dataset=dataset, status=status,
                        format=export_format).inc()
    EXPORT_DURATION.labels(dataset=dataset, status=status,
                           format=export_format).observe(seconds)


def observe_request(method: str, endpoint: str, status: int, seconds: float) -> None:
    REQUEST_TOTAL.labels(method=method, endpoint=endpoint, status=status).inc()
    REQUEST_DURATION.labels(method=method, endpoint=endpoint,
                            status=status).observe(seconds)


def observe_project_operation(operation: str, status: str = "success") -> None:
    PROJECT_OPERATIONS.labels(operation=operation, status=status).inc()


def observe_scenario_operation(operation: str, status: str = "success") -> None:
    SCENARIO_OPERATIONS.labels(operation=operation, status=status).inc()


def observe_simulation(status: str, duration_seconds: float) -> None:
    SIMULATION_RUNS.labels(status=status).inc()
    SIMULATION_DURATION.labels(status=status).observe(duration_seconds)
