# api/utils.py

import requests

def load_keywords():
    """Load scam-related keywords from file."""
    try:
        with open("data/scam_keywords.txt", "r", encoding="utf-8") as f:
            # Ensure keywords are converted to lowercase
            return [line.strip().lower() for line in f]
    except FileNotFoundError:
        print("File 'scam_keywords.txt' not found.")
        return []

def load_numbers():
    """Load scam phone numbers from file."""
    try:
        with open("data/scam_numbers.txt", "r", encoding="utf-8") as f:
            return [line.strip() for line in f]
    except FileNotFoundError:
        print("File 'scam_numbers.txt' not found.")
        return []

def load_domains():
    """Load known scam domains from file."""
    try:
        with open("data/scam_domains.txt", "r", encoding="utf-8") as f:
            return [line.strip().lower() for line in f]
    except FileNotFoundError:
        print("File 'scam_domains.txt' not found.")
        return []

def load_shorteners():
    """Load known URL shorteners from file."""
    try:
        with open("data/url_shorteners.txt", "r", encoding="utf-8") as f:
            return [line.strip().lower() for line in f]
    except FileNotFoundError:
        print("File 'url_shorteners.txt' not found.")
        return []

def resolve_redirect(url: str) -> str:
    """
    Resolves redirects for shortened URLs like bit.ly, tinyurl, etc.
    Returns the final URL or the original if redirect fails.
    """
    try:
        response = requests.head(url, allow_redirects=True, timeout=5)
        return response.url
    except Exception as e:
        print(f"[WARN] Failed to resolve redirect: {e}")
        return url

