"""Pytest configuration and fixtures.

This module contains shared fixtures for all tests.
"""

import pytest
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker, Session
from typing import Generator

from src.config.database import Base
from src.infrastructure.cache.redis_client import get_redis


# Test Database
TEST_DATABASE_URL = "sqlite:///./test.db"

engine = create_engine(
    TEST_DATABASE_URL,
    connect_args={"check_same_thread": False}
)
TestingSessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)


@pytest.fixture(scope="function")
def db_session() -> Generator[Session, None, None]:
    """Create a fresh database session for each test.
    
    Yields:
        Session: Database session
    """
    Base.metadata.create_all(bind=engine)
    session = TestingSessionLocal()
    try:
        yield session
    finally:
        session.close()
        Base.metadata.drop_all(bind=engine)


@pytest.fixture(scope="function")
def redis_client():
    """Get Redis client for testing.
    
    Returns:
        Redis client instance
    """
    client = get_redis()
    yield client
    # Cleanup
    client.flushdb()


@pytest.fixture
def sample_user_data() -> dict:
    """Sample user data for testing.
    
    Returns:
        dict: User data
    """
    return {
        "email": "test@example.com",
        "password": "SecurePassword123!",
        "full_name": "Test User",
        "phone": "+989123456789",
    }


@pytest.fixture
def sample_service_data() -> dict:
    """Sample service data for testing.
    
    Returns:
        dict: Service data
    """
    return {
        "name": "Test VPS",
        "type": "vps",
        "plan_id": 1,
        "status": "active",
    }

