"""
==================================================
Smart Attendance AI
Embedding Service
==================================================
Converts raw InsightFace face objects into
EmbeddingResult objects.
==================================================
"""

from typing import List

from core.embedding_result import EmbeddingResult


class EmbeddingService:

    @staticmethod
    def extract(
        raw_faces: List
    ) -> List[EmbeddingResult]:
        """
        Convert raw InsightFace faces into
        EmbeddingResult objects.
        """

        results = []

        for face in raw_faces:

            confidence = float(face.det_score)

            bbox = [
                int(value)
                for value in face.bbox
            ]

            width = bbox[2] - bbox[0]
            height = bbox[3] - bbox[1]

            landmarks = []

            if hasattr(face, "kps") and face.kps is not None:

                landmarks = [

                    [
                        float(point[0]),
                        float(point[1])
                    ]

                    for point in face.kps

                ]

            embedding = []

            if hasattr(face, "embedding") and face.embedding is not None:

                embedding = face.embedding.tolist()

            results.append(

                EmbeddingResult(

                    bbox=bbox,

                    confidence=confidence,

                    face_width=width,

                    face_height=height,

                    landmarks=landmarks,

                    embedding=embedding,

                    embedding_dimension=len(embedding)

                )

            )

        return results