"""Integration tests for fixture combinations."""

from pathlib import Path
from ghostty_tui.models import OptionType


def test_all_data_fixtures_together(
    sample_schema_path,
    sample_schema,
    parser,
    parsed_options,
    options_dict
):
    """Test that all data fixtures work together."""
    # Sample schema path exists
    assert sample_schema_path.exists()

    # Sample schema has content
    assert len(sample_schema) > 0

    # Parser can parse the schema
    manual_parse = parser.parse(sample_schema)
    assert len(manual_parse) > 0

    # Parsed options match manual parse
    assert len(parsed_options) == len(manual_parse)

    # Options dict may have fewer items than parsed_options due to duplicates
    # but should contain all unique names
    assert len(options_dict) <= len(parsed_options)
    option_names_in_list = {opt.name for opt in parsed_options}
    assert len(options_dict) == len(option_names_in_list)
    for opt in parsed_options:
        assert opt.name in options_dict


def test_factories_create_compatible_objects(
    option_factory,
    config_value_factory,
    option_model_factory
):
    """Test that factory-created objects work together."""
    # Create components separately
    opt_def = option_factory(
        name="test-opt",
        option_type=OptionType.INTEGER,
        default_value="10"
    )

    config_val = config_value_factory(
        name="test-opt",
        value="20",
        is_modified=True
    )

    # Create using option_model_factory
    opt_model = option_model_factory(
        name="test-opt",
        option_type=OptionType.INTEGER,
        default_value="10",
        current_value="20",
        is_modified=True
    )

    # Verify compatibility
    assert opt_model.definition.name == opt_def.name
    assert opt_model.definition.option_type == opt_def.option_type
    assert opt_model.current.name == config_val.name
    assert opt_model.current.value == config_val.value


def test_tmp_fixtures_with_parser(tmp_config_file, tmp_config_dir, parser):
    """Test temporary file fixtures with parser."""
    # Parse from temporary file
    content = tmp_config_file.read_text()
    assert "font-size = 14" in content

    # Create a file in temp directory and parse it
    test_config = tmp_config_dir / "test.config"
    test_config.write_text("""# Test config
font-size = 16
cursor-style = block
""")

    # Parser can read from temp files
    options = parser.parse(test_config.read_text())
    assert len(options) == 2


def test_sample_options_with_factories(
    sample_options_dict,
    option_factory,
    config_value_factory
):
    """Test sample options with factory fixtures."""
    # Sample options should contain expected items
    assert "font-size" in sample_options_dict
    assert "cursor-blink" in sample_options_dict

    # Create similar options using factories
    custom_font = option_factory(
        name="font-size",
        option_type=OptionType.INTEGER,
        default_value="12"
    )

    # Compare types
    assert sample_options_dict["font-size"].definition.option_type == custom_font.option_type


def test_mock_output_with_parser(parser, mock_ghostty_output):
    """Test mock output with parser."""
    options = parser.parse(mock_ghostty_output)

    # Should parse all options from mock
    option_names = {opt.name for opt in options}
    assert "font-size" in option_names
    assert "cursor-style" in option_names
    assert "cursor-blink" in option_names
    assert "window-title" in option_names

    # Verify types are correctly inferred
    options_by_name = {opt.name: opt for opt in options}
    assert options_by_name["font-size"].option_type == OptionType.INTEGER
    assert options_by_name["cursor-blink"].option_type == OptionType.BOOLEAN
    assert options_by_name["cursor-style"].option_type == OptionType.ENUM


def test_complete_workflow(
    parser,
    tmp_config_file,
    option_factory,
    option_model_factory
):
    """Test a complete workflow using multiple fixtures."""
    # 1. Read config file
    config_content = tmp_config_file.read_text()

    # 2. Parse it
    options = parser.parse(config_content)

    # 3. Create option models for each
    models = []
    for opt in options:
        model = option_model_factory(
            name=opt.name,
            option_type=opt.option_type,
            default_value=opt.default_value,
            current_value=opt.default_value
        )
        models.append(model)

    # 4. Verify we have models
    assert len(models) > 0

    # 5. All models should have valid data
    for model in models:
        assert model.definition.name is not None
        assert model.definition.option_type is not None


def test_fixture_isolation(tmp_config_file):
    """Test that fixtures are properly isolated between tests."""
    # Modify the file
    original_content = tmp_config_file.read_text()
    tmp_config_file.write_text("# Modified content")

    # Verify modification
    assert tmp_config_file.read_text() == "# Modified content"

    # This should not affect other tests
    assert original_content != "# Modified content"


def test_fixture_reusability(option_factory):
    """Test that factory fixtures can be used multiple times."""
    opt1 = option_factory(name="opt1", default_value="val1")
    opt2 = option_factory(name="opt2", default_value="val2")
    opt3 = option_factory(name="opt3", default_value="val3")

    assert opt1.name != opt2.name
    assert opt2.name != opt3.name
    assert opt1.default_value != opt2.default_value
