from sregym.conductor.oracles.llm_as_a_judge.llm_as_a_judge_oracle import LLMAsAJudgeOracle
from sregym.conductor.oracles.mitigation import MitigationOracle
from sregym.conductor.problems.base import Problem
from sregym.generators.fault.inject_virtual import VirtualizationFaultInjector
from sregym.service.apps.astronomy_shop import AstronomyShop
from sregym.service.apps.hotel_reservation import HotelReservation
from sregym.service.apps.social_network import SocialNetwork
from sregym.service.kubectl import KubeCtl
from sregym.utils.decorators import mark_fault_injected


class LivenessProbeMisconfiguration(Problem):
    def __init__(self, app_name="social_network", faulty_service="user-service"):
        self.app_name = app_name
        self.faulty_service = faulty_service

        if app_name == "social_network":
            app = SocialNetwork()
            app.create_workload(duration=30)

        elif app_name == "hotel_reservation":
            app = HotelReservation()
            app.create_workload(duration=30)

        elif app_name == "astronomy_shop":
            app = AstronomyShop()
            app.create_workload()

        else:
            raise ValueError(f"Unsupported app name: {app_name}")

        super().__init__(app=app)

        self.kubectl = KubeCtl()
        self.injector = VirtualizationFaultInjector(namespace=self.app.namespace)
        self.root_cause = self.build_structured_root_cause(
            component=self.faulty_service,
            namespace=self.namespace,
            description=(
                f"The deployment `{self.faulty_service}` has a misconfigured liveness probe that checks a non-existent "
                "health endpoint (`/healthz` on port `8080`), causing Kubernetes to repeatedly kill and restart pods. "
                "Pods enter recurrent restart loops with unstable availability and shortened uptime between crashes. "
                "Users experience intermittent request failures and latency spikes as endpoints churn during restarts."
            ),
        )
        self.diagnosis_oracle = LLMAsAJudgeOracle(problem=self, expected=self.root_cause)

        self.mitigation_oracle = MitigationOracle(problem=self)

    @mark_fault_injected
    def inject_fault(self):
        print("== Fault Injection ==")
        self.injector._inject(
            fault_type="liveness_probe_misconfiguration",
            microservices=[self.faulty_service],
        )
        print(f"Service: {self.faulty_service} | Namespace: {self.namespace}\n")

    @mark_fault_injected
    def recover_fault(self):
        print("== Fault Recovery ==")
        self.injector._recover(
            fault_type="liveness_probe_misconfiguration",
            microservices=[self.faulty_service],
        )
        print(f"Service: {self.faulty_service} | Namespace: {self.namespace}\n")
