"""
Tests for deck management endpoints.
"""
import pytest
from httpx import AsyncClient
from sqlalchemy.ext.asyncio import AsyncSession

from app.models.models import DecklistFile, DecklistFolder
from app.core.security import hash_password


class TestDeckManagement:
    """Test deck management endpoints."""
    
    @pytest.mark.asyncio
    async def test_create_deck(self, client: AsyncClient, auth_headers: dict):
        """Test creating a new deck."""
        response = await client.post(
            "/api/v1/decks/",
            json={
                "name": "Test Deck",
                "content": "4 Lightning Bolt\n2 Searing Blaze",
                "format": "plain",
            },
            headers=auth_headers,
        )
        assert response.status_code == 200
        data = response.json()
        assert data["name"] == "Test Deck"
        assert data["format"] == "plain"
    
    @pytest.mark.asyncio
    async def test_list_decks(self, client: AsyncClient, auth_headers: dict):
        """Test listing decks."""
        # Create a deck first
        await client.post(
            "/api/v1/decks/",
            json={
                "name": "List Test Deck",
                "content": "4 Lightning Bolt",
                "format": "plain",
            },
            headers=auth_headers,
        )
        
        # List decks
        response = await client.get("/api/v1/decks/", headers=auth_headers)
        assert response.status_code == 200
        data = response.json()
        assert isinstance(data, list)
        assert len(data) > 0
    
    @pytest.mark.asyncio
    async def test_get_deck(self, client: AsyncClient, auth_headers: dict):
        """Test getting a specific deck."""
        # Create a deck first
        create_response = await client.post(
            "/api/v1/decks/",
            json={
                "name": "Get Test Deck",
                "content": "4 Lightning Bolt",
                "format": "plain",
            },
            headers=auth_headers,
        )
        deck_id = create_response.json()["id"]
        
        # Get deck
        response = await client.get(f"/api/v1/decks/{deck_id}", headers=auth_headers)
        assert response.status_code == 200
        data = response.json()
        assert data["name"] == "Get Test Deck"
    
    @pytest.mark.asyncio
    async def test_update_deck(self, client: AsyncClient, auth_headers: dict):
        """Test updating a deck."""
        # Create a deck first
        create_response = await client.post(
            "/api/v1/decks/",
            json={
                "name": "Update Test Deck",
                "content": "4 Lightning Bolt",
                "format": "plain",
            },
            headers=auth_headers,
        )
        deck_id = create_response.json()["id"]
        
        # Update deck
        response = await client.patch(
            f"/api/v1/decks/{deck_id}",
            json={"name": "Updated Deck Name"},
            headers=auth_headers,
        )
        assert response.status_code == 200
        data = response.json()
        assert data["name"] == "Updated Deck Name"
    
    @pytest.mark.asyncio
    async def test_delete_deck(self, client: AsyncClient, auth_headers: dict):
        """Test deleting a deck."""
        # Create a deck first
        create_response = await client.post(
            "/api/v1/decks/",
            json={
                "name": "Delete Test Deck",
                "content": "4 Lightning Bolt",
                "format": "plain",
            },
            headers=auth_headers,
        )
        deck_id = create_response.json()["id"]
        
        # Delete deck
        response = await client.delete(f"/api/v1/decks/{deck_id}", headers=auth_headers)
        assert response.status_code == 200
    
    @pytest.mark.asyncio
    async def test_create_folder(self, client: AsyncClient, auth_headers: dict):
        """Test creating a folder."""
        response = await client.post(
            "/api/v1/decks/folders",
            json={
                "name": "Test Folder",
            },
            headers=auth_headers,
        )
        assert response.status_code == 200
        data = response.json()
        assert data["name"] == "Test Folder"
    
    @pytest.mark.asyncio
    async def test_list_folders(self, client: AsyncClient, auth_headers: dict):
        """Test listing folders."""
        # Create a folder first
        await client.post(
            "/api/v1/decks/folders",
            json={"name": "List Test Folder"},
            headers=auth_headers,
        )
        
        # List folders
        response = await client.get("/api/v1/decks/folders", headers=auth_headers)
        print(f"\nDEBUG list_folders: status={response.status_code}, body={response.text}")
        assert response.status_code == 200
        data = response.json()
        assert isinstance(data, list)
        assert len(data) > 0
    
    @pytest.mark.asyncio
    async def test_delete_folder(self, client: AsyncClient, auth_headers: dict):
        """Test deleting a folder."""
        # Create a folder first
        create_response = await client.post(
            "/api/v1/decks/folders",
            json={"name": "Delete Test Folder"},
            headers=auth_headers,
        )
        folder_id = create_response.json()["id"]
        
        # Delete folder
        response = await client.delete(f"/api/v1/decks/folders/{folder_id}", headers=auth_headers)
        assert response.status_code == 200
