#!/usr/bin/env python3
"""Upload a file to Sophnet OSS and print the signed download URL."""

import argparse
import sys
import os

def main():
    parser = argparse.ArgumentParser(description="Upload a file to Sophnet OSS")
    parser.add_argument("--file", required=True, help="Path to the file to upload")
    parser.add_argument("--timeout", type=int, default=120, help="Request timeout in seconds (default: 120)")
    args = parser.parse_args()

    file_path = os.path.abspath(os.path.expanduser(args.file))

    if not os.path.isfile(file_path):
        print(f"FILE_PATH={file_path}")
        print("UPLOAD_STATUS=failed")
        print(f"ERROR=file_not_found: {file_path}")
        sys.exit(1)

    try:
        import sophnet_tools
    except ImportError:
        print(f"FILE_PATH={file_path}")
        print("UPLOAD_STATUS=failed")
        print("ERROR=sophnet-tools package not installed")
        sys.exit(1)

    signed_url = sophnet_tools.upload_oss(file_path, timeout=args.timeout)

    if signed_url:
        print(f"FILE_PATH={file_path}")
        print("UPLOAD_STATUS=uploaded")
        print(f"DOWNLOAD_URL={signed_url}")
    else:
        print(f"FILE_PATH={file_path}")
        print("UPLOAD_STATUS=failed")
        print("ERROR=upload_failed_no_url_returned")
        sys.exit(1)

if __name__ == "__main__":
    main()
