# tests/conftest.py
# By: Md. Fahim Bin Amin
# Global pytest configuration and fixtures for the test suite.

import pytest
from unittest.mock import patch, MagicMock
import json

# Import routine-related fixtures for global use in the test suite
from tests.fixtures.routine_fixtures import (
    mock_var_names,
    sample_routine_data,
    sample_routine_data_missing_optional,
    sample_routine_data_for_display,
    sample_routine_data_missing_required,
    sample_routine_data_malformed_layer,
    gap_adjustment_scenarios,
    boundary_gap_cases,
    empty_layer_routine,
    routine_layer_get_on_call_cases,
    # Add any additional shared fixtures here as needed
)

@pytest.fixture(autouse=True)
def mock_s3_resource():
    """
    Globally mocks `boto3.resource('s3')` to prevent real AWS S3 access during all test executions.

    Simulates reading a JSON object from S3, particularly for code paths such as `utils.s3.read_json()`.
    The mock ensures any code interacting with `boto3.resource('s3')` will get controlled, dummy data.

    Yields:
        MagicMock: The patched `boto3.resource` mock, for further assertion if needed.
    """
    # Create a fake S3 object with .get()['Body'].read() returning dummy credentials as JSON
    fake_body = MagicMock()
    fake_body.read.return_value = json.dumps({
        'access_key': 'dummy_access_key',
        'refresh_key': 'dummy_refresh_key',
        'secret_keys': 'dummy_secret_keys',
        'fernet_key': 'dummy_fernet_key'
    }).encode('utf-8')

    fake_object = MagicMock()
    fake_object.get.return_value = {'Body': fake_body}

    fake_resource = MagicMock()
    fake_resource.Object.return_value = fake_object

    # Patch only within the context of each test
    with patch('utils.s3.boto3.resource', return_value=fake_resource) as mock_boto_resource:
        yield mock_boto_resource
    # Patch is automatically undone at the end of each test

# Example fixture pattern to re-alias or provide legacy access.
# Delete or extend as needed based on actual usage across the test suite.

@pytest.fixture
def sample_routine_data_fixture(sample_routine_data):
    """
    Provides sample routine data for tests. (Legacy/alias usage.)

    Args:
        sample_routine_data (dict): Injected by import above.

    Returns:
        dict: Sample routine data for use in tests.
    """
    return sample_routine_data

@pytest.fixture(autouse=True)
def _patch_utils_var_names(monkeypatch, mock_var_names):
    """
    Make every attribute from MockVarNames available on utils.var_names
    so production code that imports utils.var_names.* never breaks.
    """
    import utils.var_names as real
    for attr in dir(mock_var_names):
        if not attr.startswith("_"):
            monkeypatch.setattr(real, attr, getattr(mock_var_names, attr), raising=False)

# No need to import or redefine routine fixtures in individual test files.
# All fixtures imported above are available automatically in the test suite.

