"""
test_setup_verification.py

Purpose:
  Simple tests to verify the testing infrastructure is working correctly.
  These tests validate that fixtures are properly configured and the test
  environment is isolated from production data.
"""

import os
import pytest
from flask import Flask


def test_app_fixture_creates_flask_instance(app):
    """Test that the app fixture creates a Flask instance."""
    assert isinstance(app, Flask)
    assert app.config['TESTING'] is True


def test_client_fixture_provides_test_client(client):
    """Test that the client fixture provides a test client."""
    response = client.get('/')
    # Should get a response (even if it's a redirect or error)
    assert response is not None


def test_database_fixture_has_required_tables(app, db):
    """Test that the database fixture provides a working database with all required tables."""
    # Test that we can query the database
    cursor = db.execute("SELECT name FROM sqlite_master WHERE type='table'")
    tables = [row[0] for row in cursor.fetchall()]
    
    # Verify all required tables exist
    required_tables = ['users', 'cameras', 'markers', 'login_attempts', 
                      'login_ip_attempts', 'auth_log', 'admin_audit']
    for table in required_tables:
        assert table in tables, f"Required table '{table}' not found in database"


def test_temp_static_fixture_provides_directory(temp_static):
    """Test that the temp_static fixture provides a temporary directory."""
    assert temp_static is not None
    assert os.path.exists(temp_static)
    assert os.path.isdir(temp_static)


def test_test_users_fixture_creates_users(app, test_users):
    """Test that the test_users fixture creates users in the database."""
    assert 'regular' in test_users
    assert 'admin' in test_users
    assert 'limited' in test_users
    
    # Verify users have expected properties
    assert test_users['regular']['username'] == 'testuser'
    assert test_users['admin']['username'] == 'testadmin'
    assert test_users['limited']['username'] == 'limiteduser'
    
    assert test_users['regular']['is_admin'] is False
    assert test_users['admin']['is_admin'] is True
    assert test_users['limited']['is_admin'] is False


def test_sample_data_fixtures_provide_test_data(sample_camera_data, sample_image_data, sample_marker_data):
    """Test that sample data fixtures provide expected data structures."""
    # Camera data
    assert 'valid' in sample_camera_data
    assert 'camera_id' in sample_camera_data['valid']
    assert 'camera_name' in sample_camera_data['valid']
    assert len(sample_camera_data['valid']['camera_id']) == 12
    
    # Image data
    assert 'jpg' in sample_image_data
    assert 'filename' in sample_image_data['jpg']
    assert 'content_type' in sample_image_data['jpg']
    assert sample_image_data['jpg']['content_type'] == 'image/jpeg'
    
    # Marker data
    assert 'camera_marker' in sample_marker_data
    assert 'latitude' in sample_marker_data['camera_marker']
    assert 'longitude' in sample_marker_data['camera_marker']
    assert isinstance(sample_marker_data['camera_marker']['latitude'], float)


def test_database_isolation_between_tests(app):
    """Test that each test gets a fresh database."""
    with app.app_context():
        # Use direct connection to avoid caching issues
        import sqlite3
        db_path = os.environ.get('DATABASE_PATH')
        db = sqlite3.connect(db_path)
        db.row_factory = sqlite3.Row
        
        # Insert test data
        db.execute("INSERT INTO users (username, password_hash, is_admin) VALUES (?, ?, ?)",
                   ('isolation_test', 'hash', False))
        db.commit()
        
        # Verify data exists
        cursor = db.execute("SELECT username FROM users WHERE username = ?", ('isolation_test',))
        result = cursor.fetchone()
        assert result is not None
        assert result[0] == 'isolation_test'
        
        db.close()


def test_authenticated_clients_work(authenticated_client_regular, authenticated_client_admin):
    """Test that authenticated client fixtures work without errors."""
    # These should not raise exceptions
    assert authenticated_client_regular is not None
    assert authenticated_client_admin is not None