from __future__ import annotations

from app.domain.admin import Experiment, ExperimentRelease, Goal, SdkConfig, Site, Variant


def to_site(raw: dict) -> Site:
    return Site(
        id=str(raw["id"]),
        site_key=str(raw["site_key"]),
        name=str(raw["name"]),
        primary_domain=str(raw["primary_domain"]),
        status=str(raw["status"]),
        settings=raw.get("site_settings"),
    )


def to_experiment(raw: dict) -> Experiment:
    return Experiment(
        id=str(raw["id"]),
        site_id=str(raw["site_id"]),
        experiment_key=str(raw["experiment_key"]),
        name=str(raw["name"]),
        module_type=str(raw["module_type"]),
        status=str(raw["status"]),
        start_at=raw.get("start_at"),
        end_at=raw.get("end_at"),
        targeting_config=raw.get("targeting_config"),
    )


def to_variant(raw: dict) -> Variant:
    return Variant(
        id=str(raw["id"]),
        experiment_id=str(raw["experiment_id"]),
        variant_key=str(raw.get("variant_key", "")),
        name=str(raw.get("name", "")),
        traffic_weight=int(raw.get("traffic_weight", 0)),
        content_config=raw.get("content_config"),
    )


def to_release(raw: dict) -> ExperimentRelease:
    return ExperimentRelease(
        id=str(raw["id"]),
        experiment_id=str(raw["experiment_id"]),
        version_no=int(raw["version_no"]),
        status=str(raw["status"]),
        runtime_payload=raw.get("runtime_payload"),
    )


def to_goal(raw: dict) -> Goal:
    return Goal(
        id=str(raw["id"]),
        site_id=str(raw["site_id"]),
        goal_key=str(raw["goal_key"]),
        name=str(raw["name"]),
        goal_type=str(raw["goal_type"]),
        match_rule=raw.get("match_rule"),
    )


def to_sdk_config(raw: dict) -> SdkConfig:
    return SdkConfig(
        id=str(raw["id"]),
        site_id=str(raw["site_id"]),
        sdk_key=str(raw["sdk_key"]),
        status=str(raw["status"]),
        origin_url=raw.get("origin_url"),
        cdn_url=raw.get("cdn_url"),
        sdk_config=raw.get("sdk_config"),
    )

