#!/usr/bin/env python3
"""
Upload files to file.io (simple file sharing)
"""

import requests
import sys
from pathlib import Path

def upload_to_fileio(files):
    """
    Upload files to file.io

    Args:
        files: List of file paths
    Returns:
        List of download URLs
    """
    total_size = sum(Path(f).stat().st_size for f in files)
    print(f"📤 Uploading {len(files)} files to file.io...")
    print(f"📦 Total size: {total_size / (1024*1024):.1f} MB")

    urls = []

    for file_path in files:
        path = Path(file_path)

        try:
            # Upload to file.io
            with open(file_path, 'rb') as f:
                files = {'file': (path.name, f)}
                data = {'expires': '7d'}  # 7 days expiry

                print(f"\n📄 Uploading: {path.name} ({path.stat().st_size / (1024*1024):.1f} MB)...")

                response = requests.post(
                    'https://file.io',
                    files=files,
                    data=data,
                    timeout=3600
                )

            if response.status_code != 200:
                print(f"❌ Upload failed: {response.status_code}")
                print(f"Response: {response.text}")
                continue

            result = response.json()
            download_url = result.get('link', '')
            if download_url:
                print(f"✅ {path.name}: {download_url}")
                urls.append(download_url)
            else:
                print(f"❌ No download URL in response: {result}")

        except Exception as e:
            print(f"❌ Error uploading {path.name}: {e}")

    return urls


def main():
    if len(sys.argv) < 2:
        print("Usage: fileio_uploader.py <file1> [file2] [file3] ...")
        sys.exit(1)

    files = sys.argv[1:]

    # Check if all files exist
    for f in files:
        if not Path(f).exists():
            print(f"❌ File not found: {f}")
            sys.exit(1)

    urls = upload_to_fileio(files)

    if urls:
        print(f"\n{'='*60}")
        print(f"✅ Successfully uploaded {len(urls)} file(s)")
        print(f"{'='*60}")
        print(f"\n📋 Download links (available for 7 days):\n")
        for i, url in enumerate(urls, 1):
            print(f"{i}. {url}")
        print(f"\n💡 Note: Each file has its own link")


if __name__ == "__main__":
    main()
