from __future__ import annotations

from rights_filter.analysis.face_person_detection import HeuristicFacePersonDetector
from rights_filter.analysis.fingerprints import FingerprintService
from rights_filter.analysis.preprocessing import ImagePayload
from rights_filter.domain.records import (
    Evidence,
    EvidenceSource,
    InMemoryRightsFilterRepository,
)


class InternalAnalyzer:
    def __init__(
        self,
        repository: InMemoryRightsFilterRepository,
        fingerprint_service: FingerprintService,
        face_person_detector: HeuristicFacePersonDetector,
        similarity_threshold: float = 0.9,
    ) -> None:
        self.repository = repository
        self.fingerprint_service = fingerprint_service
        self.face_person_detector = face_person_detector
        self.similarity_threshold = similarity_threshold

    def analyze(self, submission_id: str, image: ImagePayload) -> list[Evidence]:
        evidence: list[Evidence] = []
        fingerprints = self.fingerprint_service.fingerprints_for(image.content)
        evidence.append(
            Evidence(
                source=EvidenceSource.FINGERPRINT,
                reason="Image fingerprints generated",
                confidence=1.0,
                data={
                    "submission_id": submission_id,
                    "exact": fingerprints.exact,
                    "perceptual": fingerprints.perceptual,
                },
            )
        )

        for entry in self.repository.active_knowledge_entries():
            for sample in entry.sample_fingerprints:
                similarity = self.fingerprint_service.similarity(
                    fingerprints.perceptual, sample
                )
                if similarity >= self.similarity_threshold:
                    entry_status = entry.entry_status or "confirmed"
                    reason = (
                        f"주의 후보 이미지 유사도 {similarity:.2f}"
                        if entry_status == "watchlist"
                        else f"Knowledge base image similarity {similarity:.2f}"
                    )
                    evidence.append(
                        Evidence(
                            source=EvidenceSource.FINGERPRINT,
                            reason=reason,
                            confidence=similarity,
                            data={
                                "knowledge_entry_id": entry.id,
                                "knowledge_name": entry.name,
                                "knowledge_entry_status": entry_status,
                                "source_submission_id": entry.source_submission_id,
                                "similarity": similarity,
                                "provenance": entry.provenance,
                            },
                        )
                    )

        signal = self.face_person_detector.detect(image)
        if signal.present:
            evidence.append(
                Evidence(
                    source=EvidenceSource.FACE_PERSON,
                    reason="Face/person detected",
                    confidence=0.8,
                    data={
                        "face_count": signal.face_count,
                        "person_count": signal.person_count,
                    },
                )
            )

        return evidence
