from iriusrisk_drl_toolkit.models import (
    RelationRule,
    SvgObject,
    SvgRelation,
    RuleEvaluation,
)


def evaluate_rule(
    rule: RelationRule,
    source_obj: SvgObject,
    relation: SvgRelation,
    target_obj: SvgObject,
) -> RuleEvaluation:
    reasons: list[str] = []
    failed: list[str] = []

    if source_obj.id == relation.source_id:
        reasons.append(f"source id matches relation source: {source_obj.id}")
    else:
        failed.append(
            f"source object id {source_obj.id} != relation source {relation.source_id}"
        )

    if target_obj.id == relation.target_id:
        reasons.append(f"target id matches relation target: {target_obj.id}")
    else:
        failed.append(
            f"target object id {target_obj.id} != relation target {relation.target_id}"
        )

    if source_obj.type == rule.source_type:
        reasons.append(f"source type matches rule: {source_obj.type}")
    else:
        failed.append(
            f"source type {source_obj.type} != expected {rule.source_type}"
        )

    if relation.relation_type == rule.relation_type:
        reasons.append(f"relation type matches rule: {relation.relation_type}")
    else:
        failed.append(
            f"relation type {relation.relation_type} != expected {rule.relation_type}"
        )

    if target_obj.type == rule.target_type:
        reasons.append(f"target type matches rule: {target_obj.type}")
    else:
        failed.append(
            f"target type {target_obj.type} != expected {rule.target_type}"
        )

    applies = len(failed) == 0
    return RuleEvaluation(applies=applies, reasons=reasons, failed=failed)
