#!/usr/bin/env python3
"""
Test script for store management features.

Tests:
- Create store with both emails
- Create store with only owner_email
- Update store (contact_email)
- Transfer ownership
"""

import requests

BASE_URL = "http://localhost:8000"
ADMIN_TOKEN = None  # Will be set after login


def login_admin():
    """Login as admin and get token."""
    global ADMIN_TOKEN

    response = requests.post(
        f"{BASE_URL}/api/v1/admin/auth/login",  # ✅ Changed: added /admin/
        json={
            "username": "admin",  # Replace with your admin username
            "password": "admin123",  # Replace with your admin password
        },
    )

    if response.status_code == 200:
        data = response.json()
        ADMIN_TOKEN = data["access_token"]
        print("✅ Admin login successful")
        return True
    print(f"❌ Admin login failed: {response.status_code}")
    print(response.text)
    return False


def get_headers():
    """Get authorization headers."""
    return {
        "Authorization": f"Bearer {ADMIN_TOKEN}",
        "Content-Type": "application/json",
    }


def test_create_store_with_both_emails():
    """Test creating store with both owner_email and contact_email."""
    print("\n" + "=" * 60)
    print("TEST 1: Create store with both emails")
    print("=" * 60)

    store_data = {
        "store_code": "TESTDUAL",
        "name": "Test Dual Email Store",
        "subdomain": "testdual",
        "owner_email": "owner@testdual.com",
        "contact_email": "contact@testdual.com",
        "description": "Test store with separate emails",
    }

    response = requests.post(
        f"{BASE_URL}/api/v1/admin/stores", headers=get_headers(), json=store_data
    )

    if response.status_code == 200:
        data = response.json()
        print("✅ Store created successfully")
        print("\n📧 Emails:")
        print(f"  Owner Email:   {data['owner_email']}")
        print(f"  Contact Email: {data['contact_email']}")
        print("\n🔑 Credentials:")
        print(f"  Username: {data['owner_username']}")
        print(f"  Password: {data['temporary_password']}")  # noqa: SEC021
        print(f"\n🔗 Login URL: {data['login_url']}")
        return data["id"]
    print(f"❌ Failed: {response.status_code}")
    print(response.text)
    return None


def test_create_store_single_email():
    """Test creating store with only owner_email (contact should default)."""
    print("\n" + "=" * 60)
    print("TEST 2: Create store with only owner_email")
    print("=" * 60)

    store_data = {
        "store_code": "TESTSINGLE",
        "name": "Test Single Email Store",
        "subdomain": "testsingle",
        "owner_email": "owner@testsingle.com",
        "description": "Test store with single email",
    }

    response = requests.post(
        f"{BASE_URL}/api/v1/admin/stores", headers=get_headers(), json=store_data
    )

    if response.status_code == 200:
        data = response.json()
        print("✅ Store created successfully")
        print("\n📧 Emails:")
        print(f"  Owner Email:   {data['owner_email']}")
        print(f"  Contact Email: {data['contact_email']}")

        if data["owner_email"] == data["contact_email"]:
            print("✅ Contact email correctly defaulted to owner email")
        else:
            print("❌ Contact email should have defaulted to owner email")

        return data["id"]
    print(f"❌ Failed: {response.status_code}")
    print(response.text)
    return None


def test_update_store_contact_email(store_id):
    """Test updating store's contact email."""
    print("\n" + "=" * 60)
    print(f"TEST 3: Update store {store_id} contact_email")
    print("=" * 60)

    update_data = {
        "contact_email": "newcontact@business.com",
        "name": "Updated Store Name",
    }

    response = requests.put(
        f"{BASE_URL}/api/v1/admin/stores/{store_id}",
        headers=get_headers(),
        json=update_data,
    )

    if response.status_code == 200:
        data = response.json()
        print("✅ Store updated successfully")
        print("\n📧 Emails after update:")
        print(f"  Owner Email:   {data['owner_email']} (unchanged)")
        print(f"  Contact Email: {data['contact_email']} (updated)")
        print(f"\n📝 Name: {data['name']} (updated)")
        return True
    print(f"❌ Failed: {response.status_code}")
    print(response.text)
    return False


def test_get_store_details(store_id):
    """Test getting store details with owner info."""
    print("\n" + "=" * 60)
    print(f"TEST 4: Get store {store_id} details")
    print("=" * 60)

    response = requests.get(
        f"{BASE_URL}/api/v1/admin/stores/{store_id}", headers=get_headers()
    )

    if response.status_code == 200:
        data = response.json()
        print("✅ Store details retrieved")
        print("\n📋 Store Info:")
        print(f"  ID: {data['id']}")
        print(f"  Code: {data['store_code']}")
        print(f"  Name: {data['name']}")
        print(f"  Subdomain: {data['subdomain']}")
        print("\n👤 Owner Info:")
        print(f"  Username: {data['owner_username']}")
        print(f"  Email: {data['owner_email']}")
        print("\n📧 Contact Info:")
        print(f"  Email: {data['contact_email']}")
        print(f"  Phone: {data.get('contact_phone', 'N/A')}")
        return True
    print(f"❌ Failed: {response.status_code}")
    print(response.text)
    return False


def test_transfer_ownership(store_id, new_owner_user_id):
    """Test transferring store ownership."""
    print("\n" + "=" * 60)
    print(f"TEST 5: Transfer ownership of store {store_id}")
    print("=" * 60)

    # First, get current owner info
    response = requests.get(
        f"{BASE_URL}/api/v1/admin/stores/{store_id}", headers=get_headers()
    )

    if response.status_code == 200:
        current_data = response.json()
        print("\n📋 Current Owner:")
        print(f"  User ID: {current_data['owner_user_id']}")
        print(f"  Username: {current_data['owner_username']}")
        print(f"  Email: {current_data['owner_email']}")

    # Transfer ownership
    transfer_data = {
        "new_owner_user_id": new_owner_user_id,
        "confirm_transfer": True,
        "transfer_reason": "Testing ownership transfer feature",
    }

    response = requests.post(
        f"{BASE_URL}/api/v1/admin/stores/{store_id}/transfer-ownership",
        headers=get_headers(),
        json=transfer_data,
    )

    if response.status_code == 200:
        data = response.json()
        print(f"\n✅ {data['message']}")
        print("\n👤 Old Owner:")
        print(f"  ID: {data['old_owner']['id']}")
        print(f"  Username: {data['old_owner']['username']}")
        print(f"  Email: {data['old_owner']['email']}")
        print("\n👤 New Owner:")
        print(f"  ID: {data['new_owner']['id']}")
        print(f"  Username: {data['new_owner']['username']}")
        print(f"  Email: {data['new_owner']['email']}")
        print(f"\n📝 Reason: {data['transfer_reason']}")
        print(f"⏰ Transferred at: {data['transferred_at']}")
        return True
    print(f"❌ Failed: {response.status_code}")
    print(response.text)
    return False


def main():
    """Run all tests."""
    print("\n🧪 Starting Store Management Tests")
    print("=" * 60)

    # Login first
    if not login_admin():
        print("\n❌ Cannot proceed without admin login")
        return

    # Test 1: Create with both emails
    store_id_1 = test_create_store_with_both_emails()

    # Test 2: Create with single email
    test_create_store_single_email()

    if store_id_1:
        # Test 3: Update contact email
        test_update_store_contact_email(store_id_1)

        # Test 4: Get store details
        test_get_store_details(store_id_1)

        # Test 5: Transfer ownership (you need to provide a valid user ID)
        # Uncomment and replace with actual user ID to test
        # test_transfer_ownership(store_id_1, new_owner_user_id=2)

    print("\n" + "=" * 60)
    print("✅ All tests completed!")
    print("=" * 60)


if __name__ == "__main__":
    main()
