# tests/conftest.py
import pytest
import os
import sys
from unittest.mock import MagicMock, patch
from datetime import datetime, timedelta
import tempfile
import json


@pytest.fixture
def mock_env_vars(monkeypatch):
    """Set up standard environment variables for testing."""
    env_vars = {
        'DB_HOST': 'test-host',
        'DB_PORT': '5432',
        'DB_USER': 'test-user',
        'DB_PASS': 'test-pass',
        'DB_NAME': 'test-db',
        'POLL_INTERVAL': '30',
        'LOG_FILE': '/tmp/test_bridge.log'
    }
    for key, value in env_vars.items():
        monkeypatch.setenv(key, value)
    return env_vars


@pytest.fixture
def mock_db_connection():
    """Create a mock PostgreSQL connection and cursor."""
    mock_conn = MagicMock()
    mock_cursor = MagicMock()
    
    # Configure cursor context manager
    mock_cursor.__enter__ = MagicMock(return_value=mock_cursor)
    mock_cursor.__exit__ = MagicMock(return_value=False)
    
    mock_conn.cursor.return_value = mock_cursor
    
    return mock_conn, mock_cursor


@pytest.fixture
def sample_directive():
    """Create a sample directive for testing."""
    return {
        'id': 123,
        'direction': 'inbound',
        'status': 'pending',
        'target': 'claude',
        'command_type': 'code_generation',
        'payload': json.dumps({'task': 'create function'}),
        'priority': 5,
        'created_at': datetime.now() - timedelta(minutes=5),
        'updated_at': datetime.now() - timedelta(minutes=5)
    }


@pytest.fixture
def urgent_directive():
    """Create an urgent priority directive."""
    return {
        'id': 456,
        'direction': 'inbound',
        'status': 'pending',
        'target': 'claude',
        'command_type': 'urgent_fix',
        'payload': json.dumps({'task': 'fix production bug'}),
        'priority': 9,
        'created_at': datetime.now() - timedelta(minutes=1),
        'updated_at': datetime.now() - timedelta(minutes=1)
    }


@pytest.fixture
def high_priority_directive():
    """Create a high priority directive."""
    return {
        'id': 789,
        'direction': 'inbound',
        'status': 'pending',
        'target': 'claude',
        'command_type': 'review',
        'payload': json.dumps({'task': 'review PR'}),
        'priority': 7,
        'created_at': datetime.now() - timedelta(minutes=2),
        'updated_at': datetime.now() - timedelta(minutes=2)
    }


@pytest.fixture
def temp_log_file(tmp_path):
    """Create a temporary log file."""
    log_file = tmp_path / "test_bridge.log"
    return str(log_file)


@pytest.fixture
def mock_signal_handlers():
    """Mock signal handlers for graceful shutdown testing."""
    with patch('signal.signal') as mock_signal:
        yield mock_signal


@pytest.fixture
def mock_time():
    """Mock time functions for processing time tracking."""
    with patch('time.time') as mock_time_func, \
         patch('time.sleep') as mock_sleep:
        
        # Start at time 1000, increment by 1 second per call
        mock_time_func.side_effect = [1000.0 + i for i in range(1000)]
        yield mock_time_func, mock_sleep