"""
End-to-end test for Vehicle/Asset creation flow.
Uses the authenticated_client fixture to test adding a new vehicle to the user's garage.
"""
import pytest
import httpx
import uuid


@pytest.mark.asyncio
async def test_vehicle_creation(authenticated_client: httpx.AsyncClient, setup_organization):
    """
    Test that a user can add a new vehicle (asset) to their garage.
    Uses the new POST /api/v1/assets/vehicles endpoint.
    """
    # Generate unique VIN and license plate
    unique_suffix = uuid.uuid4().hex[:8]
    # VIN must be exactly 17 characters
    vin = f"VIN{unique_suffix}123456"  # 3 + 8 + 6 = 17
    payload = {
        "vin": vin,
        "license_plate": f"TEST-{unique_suffix[:6]}",
        # catalog_id omitted (optional)
        "organization_id": setup_organization,
    }
    # The backend will uppercase the VIN, so we compare case-insensitively
    expected_vin = vin.upper()
    
    # POST to the new endpoint
    response = await authenticated_client.post(
        "/api/v1/assets/vehicles",
        json=payload
    )
    
    # Assert success (201 Created)
    assert response.status_code == 201, f"Unexpected status: {response.status_code}, response: {response.text}"
    
    # Parse response
    data = response.json()
    # Expect AssetResponse schema
    assert "id" in data
    assert data["vin"] == expected_vin
    assert data["license_plate"] == payload["license_plate"].upper()
    
    asset_id = data["id"]
    print(f"✅ Vehicle/Asset created with ID: {asset_id}")
    
    # Return the asset_id for potential use in expense test
    return asset_id