import requests
import config
from fediverse_factory import get_fediverse_service
import db_utils

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

    Args:
        name (str): Card name.
        rarity (int): Card 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]: Card 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('Card name cannot be empty.')
    if rarity < 1:
        raise ValueError('Rarity must be a positive integer.')
    if rarity not in config.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(config.INSTANCE_TYPE)
        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 using db_utils function
        card_id = db_utils.insert_card(
            stripped_name,
            rarity,
            file_id
        )
        
        return card_id, file_id

    except Exception as e:
        raise