#!/usr/bin/env python3
"""
Direct test of new shipping upload endpoint
"""
import os
import requests

# Disable proxy
os.environ['NO_PROXY'] = 'localhost,127.0.0.1'
os.environ['no_proxy'] = 'localhost,127.0.0.1'

BASE_URL = "http://localhost:8000"

def test_upload():
    """Test the upload endpoint directly"""

    # Test file path
    file_path = "/Users/jinjunqian/Downloads/工作簿11.16-1.xlsx"

    # Check if file exists
    if not os.path.exists(file_path):
        print(f"❌ File not found: {file_path}")
        return

    print(f"✅ File exists: {file_path}")

    # Prepare the multipart form data
    with open(file_path, 'rb') as f:
        files = {'file': ('工作簿11.16-1.xlsx', f, 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet')}
        data = {
            'shop_name': '晴七公主',
            'operator': 'admin'
        }

        # Send the request
        print("\n📤 Sending upload request...")
        print(f"URL: {BASE_URL}/api/v1/new-shipping/upload-shipping-list")
        print(f"Shop: {data['shop_name']}")
        print(f"Operator: {data['operator']}")

        try:
            response = requests.post(
                f"{BASE_URL}/api/v1/new-shipping/upload-shipping-list",
                files=files,
                data=data,
                proxies={'http': None, 'https': None}
            )

            print(f"\n📥 Response Status: {response.status_code}")

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

                # Try to parse JSON error
                try:
                    error_data = response.json()
                    print(f"Error detail: {error_data}")
                except:
                    pass

        except requests.exceptions.RequestException as e:
            print(f"❌ Request failed: {e}")

        except Exception as e:
            print(f"❌ Unexpected error: {e}")

if __name__ == "__main__":
    test_upload()