import sqlite3
import requests
import json
import time
import re
import os

# CONFIG
TMDB_API_KEY = "3861797ddcdbe4c776f039479571f7bd" # User must insert key
DB_FILE = "metadata.db"

def clean_filename(s):
    # Remove file extensions, dots, year brackets, scene tags
    s = s.replace('.', ' ').replace('_', ' ')
    s = re.sub(r'\(\d{4}\)', '', s) # (2024)
    s = re.sub(r'\[.*?\]', '', s) # [text]
    # Remove common tags
    tags = ["4k", "1080p", "720p", "h264", "h265", "hevc", "web-dl", "bluray", "x264", "ger", "eng", "multi", "german", "english"]
    for t in tags:
        s = re.sub(r'\b' + t + r'\b', '', s, flags=re.IGNORECASE)
    return s.strip()

def fetch_tmdb(title, is_series):
    search_type = "tv" if is_series else "movie"
    url = f"https://api.themoviedb.org/3/search/{search_type}?api_key={TMDB_API_KEY}&query={title}&language=de-DE"
    try:
        r = requests.get(url)
        if r.status_code == 200:
            data = r.json()
            if data['results']:
                # Return first match
                return data['results'][0]
    except Exception as e:
        print(f"Error fetching {title}: {e}")
    return None

def main():
    if not os.path.exists(DB_FILE):
        print("DB not found. Run api.php once to create it.")
        return

    conn = sqlite3.connect(DB_FILE)
    cursor = conn.cursor()

    # Genre Map (ID -> Name) - simplified, usually fetch from TMDB /genre/list
    # For now, we store genre_ids in DB or fetch names?
    # Let's map IDs to text manually or fetch once.

    print("Worker started...")

    while True:
        # Find streams without metadata
        # We join with metadata table and find nulls
        cursor.execute("""
            SELECT s.stream_hash, s.raw_name, s.type
            FROM streams s
            LEFT JOIN metadata m ON s.stream_hash = m.stream_hash
            WHERE m.stream_hash IS NULL
            LIMIT 50
        """)

        rows = cursor.fetchall()

        if not rows:
            print("No new streams to process. Sleeping...")
            time.sleep(10)
            continue

        print(f"Processing batch of {len(rows)} items...")

        for row in rows:
            hash_id, raw_name, type_str = row
            clean_name = clean_filename(raw_name)
            is_series = (type_str == 'series')

            print(f"Searching: {clean_name} ({type_str})")

            res = fetch_tmdb(clean_name, is_series)

            if res:
                title = res.get('title') if not is_series else res.get('name')
                poster = "https://image.tmdb.org/t/p/w500" + res.get('poster_path') if res.get('poster_path') else None
                rating = res.get('vote_average', 0)
                genres = [] # fetch genre names via IDs if possible, or just store IDs
                # For simplicity, we just store raw IDs or empty list if no helper
                # Ideally, call /genre/movie/list to map IDs.

                # Insert
                try:
                    cursor.execute("""
                        INSERT INTO metadata (stream_hash, tmdb_id, title, poster_path, rating, genres)
                        VALUES (?, ?, ?, ?, ?, ?)
                    """, (hash_id, res['id'], title, poster, rating, json.dumps(genres)))
                    conn.commit()
                    print(f" -> Found: {title}")
                except Exception as e:
                    print(f" -> DB Error: {e}")
            else:
                # Mark as processed but empty? Or retry later?
                # For now, maybe insert dummy record to avoid loop
                print(" -> Not found.")
                cursor.execute("INSERT INTO metadata (stream_hash, title) VALUES (?, ?)", (hash_id, clean_name))
                conn.commit()

            time.sleep(0.5) # Rate limit

    conn.close()

if __name__ == "__main__":
    main()
