"""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.
"""

from sqlalchemy.orm import Session

from app.domain.entities.technique import TechniqueEntity
from app.models.technique import Technique


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.
    """
    entity = TechniqueEntity.from_orm(technique)
    test_snapshots = [
        (t.state, t.detection_result) for t in technique.tests
    ]
    entity.recalculate_status(test_snapshots)
    technique.status_global = entity.status_global
