from fastapi.testclient import TestClient
from sqlalchemy.orm import Session
from tests.helpers import generators


def test_create_event(client: TestClient, db: Session) -> None:
    user, unhashed_password = generators.create_user(db)
    rsp = generators.login(db, user.username, unhashed_password)
    access_token = rsp["access_token"]
    refresh_token = rsp["refresh_token"]
    
    response = client.post("/api/calendar/events", 
        json={
            "title": "Test Event",
            "start_time": "2024-03-20T15:00:00Z"
        },
        headers={"Authorization": f"Bearer {access_token}"},
        cookies={"refresh_token": refresh_token},
    )
    assert response.status_code == 200
    assert response.json()["title"] == "Test Event"

def test_get_events(client: TestClient, db: Session) -> None:
    user, unhashed_password = generators.create_user(db)
    rsp = generators.login(db, user.username, unhashed_password)
    access_token = rsp["access_token"]
    refresh_token = rsp["refresh_token"]
    
    # Create an event to retrieve
    client.post("/api/calendar/events", 
        json={
            "title": "Test Event",
            "start_time": "2024-03-20T15:00:00Z"
        },
        headers={"Authorization": f"Bearer {access_token}"},
        cookies={"refresh_token": refresh_token},
    )
    
    response = client.get("/api/calendar/events", 
        headers={"Authorization": f"Bearer {access_token}"},
        cookies={"refresh_token": refresh_token},
    )
    assert response.status_code == 200
    assert len(response.json()) > 0