# /opt/docker/dev/service_finder/backend/app/services/matching_service.py
from typing import List, Dict, Any
from app.services.config_service import config

class MatchingService:
    @staticmethod
    async def rank_services(services: List[Dict[str, Any]], org_id: int = None) -> List[Dict[str, Any]]:
        """ Szolgáltatók rangsorolása dinamikus Sentinel paraméterek alapján. """
        
        # JAVÍTVA: Hierarchikus paraméterek lekérése
        w_dist = float(await config.get_setting('weight_distance', org_id=org_id, default=0.5))
        w_rate = float(await config.get_setting('weight_rating', org_id=org_id, default=0.5))
        b_gold = float(await config.get_setting('bonus_gold_service', org_id=org_id, default=500))
        
        ranked_list = []
        for s in services:
            # Távolság pont (közelebb = több pont)
            dist = s.get('distance', 1.0)
            p_dist = 100 / (dist + 1)
            
            # Értékelés pont (0-5 csillag -> 0-100 pont)
            p_rate = s.get('rating', 0.0) * 20
            
            # Bónusz a kiemelt (Gold) partnereknek
            tier_bonus = b_gold if s.get('tier') == 'gold' else 0
            
            # Összesített pontszám
            total_score = (p_dist * w_dist) + (p_rate * w_rate) + tier_bonus
            
            s['total_score'] = round(total_score, 2)
            ranked_list.append(s)
            
        return sorted(ranked_list, key=lambda x: x['total_score'], reverse=True)

matching_service = MatchingService()