import pytest
from fastapi.testclient import TestClient
from app.main import app

client = TestClient(app)

def test_chat_endpoint():
    response = client.post(
        "/login",
        data={"username": "professor1@uni.edu", "password": "1234"}
    )
    
    token_data = response.json()
    
    headers = {"Authorization": f"Bearer {token_data['access_token']}"}
    response = client.post("/chat", json={"chat_id": None, "message": "Digue's un numero del 1 al 3"}, headers=headers)

    assert response.status_code == 200
    assert "chat_id" in response.json()
    assert "response" in response.json()


def test_get_chats():
    response = client.post(
        "/login",
        data={"username": "professor1@uni.edu", "password": "1234"}
    )
    
    token_data = response.json()  
    
    headers = {"Authorization": f"Bearer {token_data['access_token']}"}
    
    response = client.get("/chats/me", headers=headers)
    
    assert response.status_code == 200
    
    chats = response.json()

    assert len(chats) == 1
    
    assert "_id" in chats[0]
    assert "messages" in chats[0]

def test_chat_endpoint_unauthorized():
    response = client.post("/chat", json={"chat_id": None, "message": "Hola, bot!"})
    assert response.status_code == 401
    assert response.json() == {"detail": "Not authenticated"}

def test_get_chats_unauthorized():
    response = client.get("/chats/me")
    assert response.status_code == 401
    assert response.json() == {"detail": "Not authenticated"}