import geoip2.database
from user_agents import parse
from django.db.models import Count
from antifroud.models import UserActivityLog

# Геолокация по IP
def get_geolocation(ip):
    try:
        with geoip2.database.Reader('GeoLite2-City.mmdb') as reader:
            response = reader.city(ip)
            return {
                'city': response.city.name,
                'country': response.country.name,
                'latitude': response.location.latitude,
                'longitude': response.location.longitude,
            }
    except Exception:
        return None

# Анализ user-agent
def get_user_agent_details(user_agent_string):
    user_agent = parse(user_agent_string)
    return {
        'device': user_agent.device.family,
        'os': user_agent.os.family,
        'browser': user_agent.browser.family,
    }

# Обработка логов
def analyze_logs():
    logs = UserActivityLog.objects.all()
    for log in logs:
        # Геолокация
        geo = get_geolocation(log.ip)
        if geo:
            log.location = f"{geo['city']}, {geo['country']}"

        # User Agent
        ua_details = get_user_agent_details(log.UAString)
        log.device = ua_details['device']
        log.platform = ua_details['os']
        log.agent = ua_details['browser']

        # Сохранение обновлённой записи
        log.save()

# Генерация статистики
def generate_statistics():
    statistics = {}

    # Уникальные IP
    unique_ips = UserActivityLog.objects.values('ip').distinct().count()
    statistics['unique_ips'] = unique_ips

    # Популярные страницы
    popular_pages = UserActivityLog.objects.values('page_url').annotate(count=Count('page_url')).order_by('-count')[:10]
    statistics['popular_pages'] = [{"url": page['page_url'], "count": page['count']} for page in popular_pages]

    # Типы устройств
    devices = UserActivityLog.objects.values('device').annotate(count=Count('device')).order_by('-count')
    statistics['devices'] = [{"device": device['device'], "count": device['count']} for device in devices]

    return statistics
