import time
from logging import Logger
from typing import override

from test_automation_suite.checks.check import Check
from test_automation_suite.core.context import Context
from test_automation_suite.core.task import Step, StepResult
from test_automation_suite.utility.cmd import stress_cpu
from test_automation_suite.utility.tccd import TCCD


class CheckTccdCpuTemperature(Check):
    stress_time: int = 40
    """Duration how long CPU gets stressed in seconds."""
    sleep_time: int = 25
    """Wait time to call DBus after the stress."""

    @override
    def steps(self) -> list[Step]:
        return [*super().steps(), self.step_check_cpu]

    def step_check_cpu(self, _context: Context, logger: Logger) -> StepResult:
        stress = stress_cpu(self.stress_time)
        time.sleep(self.sleep_time)
        tccd_data = TCCD.get_fan_data()
        _ = stress.wait()

        cpu_temp = tccd_data.cpu.temperature
        cpu_fan_speed = tccd_data.cpu.speed
        success = cpu_temp > 0 and cpu_fan_speed > 0

        return StepResult(
            success=success,
            harvest_data={
                "cpu_temperature": cpu_temp,
                "cpu_fan_speed": cpu_fan_speed,
            },
        )
