"""Service for recalculating the global status of a Technique.

Delegates entirely to :meth:`TechniqueEntity.recalculate_status`
so that the business rules live in a single place (the domain entity).

This thin adapter converts ORM objects into the format the entity
expects, then writes the result back onto the ORM model.

The function mutates the technique but does **not** commit.
The caller is responsible for committing the session.
"""

# Import Session from sqlalchemy.orm
from sqlalchemy.orm import Session

# Import TechniqueEntity from app.domain.entities.technique
from app.domain.entities.technique import TechniqueEntity

# Import Technique from app.models.technique
from app.models.technique import Technique


# Define function recalculate_technique_status
def recalculate_technique_status(db: Session, technique: Technique) -> None:
    """Recompute ``technique.status_global`` from its tests.

    ``db`` is accepted for backward compatibility but is not used
    directly — test data comes from the ORM relationship.
    """
    # Assign entity = TechniqueEntity.from_orm(technique)
    entity = TechniqueEntity.from_orm(technique)
    # Assign test_snapshots = [
    test_snapshots = [
        (t.state, t.detection_result) for t in technique.tests
    ]
    # Call entity.recalculate_status()
    entity.recalculate_status(test_snapshots)
    # Assign technique.status_global = entity.status_global
    technique.status_global = entity.status_global
