from datetime import datetime
from fastapi.testclient import TestClient
from sqlmodel import Session, select
from models import Vertical, Musician, BandMembership, UserVerticalPreference, PreferenceTier, Notification, NotificationType, Artist

def test_create_vertical(client: TestClient, session: Session):
    vertical = Vertical(name="Test Band", slug="test-band")
    session.add(vertical)
    session.commit()
    
    response = client.get("/bands/test-band")
    assert response.status_code == 200
    data = response.json()
    # The API returns structured data: { "band": {...}, "stats": {...} }
    assert data["band"]["name"] == "Test Band"
    assert data["band"]["slug"] == "test-band"

def test_create_musician_and_membership(client: TestClient, session: Session):
    # 1. Setup Artist & Vertical
    artist = Artist(name="The Testers", slug="the-testers-artist")
    session.add(artist)
    session.commit()
    
    band = Vertical(name="The Testers", slug="the-testers", primary_artist_id=artist.id)
    session.add(band)
    session.commit()
    
    # 2. Setup Musician
    musician = Musician(name="John Doe", slug="john-doe")
    session.add(musician)
    session.commit()
    
    # 3. Link them via Artist
    membership = BandMembership(
        musician_id=musician.id,
        artist_id=artist.id,
        role="Guitar",
        start_date=datetime(2020, 1, 1)
    )
    session.add(membership)
    session.commit()
    
    # 4. Test API
    response = client.get("/musicians/john-doe")
    assert response.status_code == 200
    data = response.json()
    assert data["musician"]["name"] == "John Doe"
    assert len(data["bands"]) == 1
    # The 'bands' list in response contains artist_name/slug
    assert data["bands"][0]["artist_name"] == "The Testers"

def test_notification_integration(client: TestClient, session: Session, test_user_token: str):
    # 1. Setup Band
    band = Vertical(name="Notify Band", slug="notify-band")
    session.add(band)
    session.commit()
    
    # 2. Setup User & Preference
    # We need the user ID. The 'test_user_token' fixture creates a user with email "test@example.com".
    from models import User
    user = session.exec(select(User).where(User.email == "test@example.com")).first()
    assert user is not None
    
    pref = UserVerticalPreference(
        user_id=user.id,
        vertical_id=band.id,
        tier=PreferenceTier.HEADLINER,
        notify_on_show=True
    )
    session.add(pref)
    session.commit()
    
    # 3. Create Show via API (triggering notification)
    # Ensure venue exists for potential creation
    from models import Venue
    venue = Venue(name="Notify Venue", city="City", country="Country", slug="notify-venue")
    session.add(venue)
    session.commit()

    response = client.post(
        "/shows/",
        json={
            "date": "2025-01-01T00:00:00",
            "vertical_id": band.id,
            "venue_id": venue.id,
            "slug": "notify-show-1"
        },
        headers={"Authorization": f"Bearer {test_user_token}"}
    )
    
    assert response.status_code == 200, f"Response: {response.text}"
    
    # 4. Verify Notification
    notes = session.exec(select(Notification).where(Notification.user_id == user.id)).all()
    assert len(notes) > 0, "No notifications found for user"
    assert notes[0].type == NotificationType.SHOW_ALERT
    assert "Notify Band" in notes[0].title
