from __future__ import annotations

from typing import Any

from app.schemas.common import ApiModel


class RuntimeVariantCandidateInput(ApiModel):
    id: str
    variant_key: str
    traffic_weight: int
    content_config: dict[str, Any] | list[Any] | None = None


class RuntimeExperimentInput(ApiModel):
    experiment_id: str
    experiment_key: str
    status: str
    site_key: str | None = None
    assignment_salt: str | None = None
    release_id: str | None = None
    release_version: int | None = None
    payload: dict[str, Any] | list[Any] | None = None
    variants: list[RuntimeVariantCandidateInput] = []


class RuntimeBootstrapRequest(ApiModel):
    site_id: str | None = None
    site_key: str | None = None
    url: str
    visitor_id: str
    user_agent: str | None = None


class RuntimeBootstrapResponse(ApiModel):
    site_id: str | None = None
    site_key: str | None = None
    url: str
    visitor_id: str
    candidate_experiments: list[RuntimeExperimentInput] = []


class RuntimeAssignRequest(ApiModel):
    visitor_id: str
    experiment: RuntimeExperimentInput


class RuntimeAssignResponse(ApiModel):
    experiment_id: str
    experiment_key: str
    variant_id: str
    variant_key: str
    bucket: int
    reason: str


class RuntimePayloadRequest(ApiModel):
    visitor_id: str
    experiment: RuntimeExperimentInput


class RuntimePayloadResponse(ApiModel):
    experiment_id: str
    experiment_key: str
    release_id: str | None = None
    release_version: int | None = None
    assigned_variant_id: str | None = None
    assigned_variant_key: str | None = None
    payload: dict[str, Any] | list[Any] | None = None


class RuntimeEventRequest(ApiModel):
    site_id: str | None = None
    site_key: str | None = None
    experiment_id: str | None = None
    experiment_key: str | None = None
    variant_id: str | None = None
    variant_key: str | None = None
    visitor_id: str
    event_name: str
    payload: dict[str, Any] | list[Any] | None = None


class RuntimeEventResponse(ApiModel):
    accepted: bool
    event_name: str
