import requests
from fediverse_factory import get_fediverse_service
from db_utils import get_db_connection
from config import RARITY_TO_WEIGHT

def add_character(name: str, rarity: int, weight: float, image_url: str) -> tuple[int, str]:
    """
    Adds a character to the database, uploading the image from a public URL to the Fediverse instance.

    Args:
        name (str): Character name.
        rarity (int): Character rarity (e.g., 1-5).
        weight (float): Pull weight (e.g., 0.02).
        image_url (str): Public URL of the image from the post.

    Returns:
        tuple[int, str]: Character ID and file_id.

    Raises:
        ValueError: If inputs are invalid.
        RuntimeError: If image download/upload or database operation fails.
    """

    stripped_name = name.strip()

    # Validate inputs
    if not stripped_name:
        raise ValueError('Character name cannot be empty.')
    if rarity < 1:
        raise ValueError('Rarity must be a positive integer.')
    if rarity not in RARITY_TO_WEIGHT.keys():
        raise ValueError(f'Invalid rarity: {rarity}')
    if not image_url:
        raise ValueError('Image URL must be provided.')

    try:
        # Download image
        response = requests.get(image_url, stream=True, timeout=30)
        if response.status_code != 200:
            raise RuntimeError(f"Failed to download image from {image_url}")

        # Upload to Fediverse instance
        fediverse_service = get_fediverse_service()
        try:
            uploaded_file = fediverse_service.upload_file(response.raw)
            file_id = uploaded_file.id
        except RuntimeError as e:
            raise RuntimeError(f"Failed to upload image: {e}") from e

        # Insert into database
        conn = get_db_connection()
        cur = conn.cursor()
        cur.execute(
            'INSERT INTO characters (name, rarity, weight, file_id) VALUES (?, ?, ?, ?)',
            (stripped_name, rarity, float(weight), file_id)
        )
        conn.commit()
        character_id = cur.lastrowid

        return character_id, file_id

    except Exception as e:
        raise
    finally:
        if 'conn' in locals():
            conn.close()