"""Tests for update startup check functionality."""

from pathlib import Path
from unittest.mock import AsyncMock, MagicMock, patch

import pytest

from webdrop_bridge.config import Config
from webdrop_bridge.ui.main_window import UpdateCheckWorker


@pytest.fixture
def sample_config(tmp_path):
    """Create a sample config for testing."""
    return Config(
        app_name="Test WebDrop",
        app_version="1.0.0",
        log_level="INFO",
        log_file=None,
        allowed_roots=[tmp_path],
        allowed_urls=[],
        webapp_url="file:///./webapp/index.html",
        window_width=800,
        window_height=600,
        window_title="Test WebDrop v1.0.0",
        enable_logging=False,
    )


class TestUpdateCheckWorker:
    """Tests for UpdateCheckWorker."""

    def test_worker_initialization(self):
        """Test worker can be initialized."""
        manager = MagicMock()
        worker = UpdateCheckWorker(manager, "0.0.1")
        
        assert worker.manager is manager
        assert worker.current_version == "0.0.1"

    def test_worker_has_signals(self):
        """Test worker has required signals."""
        manager = MagicMock()
        worker = UpdateCheckWorker(manager, "0.0.1")
        
        assert hasattr(worker, "update_available")
        assert hasattr(worker, "update_status")
        assert hasattr(worker, "finished")

    def test_worker_run_method_exists(self):
        """Test worker has run method."""
        manager = MagicMock()
        worker = UpdateCheckWorker(manager, "0.0.1")
        
        assert hasattr(worker, "run")
        assert callable(worker.run)


class TestMainWindowStartupCheck:
    """Test startup check integration in MainWindow."""

    def test_window_has_startup_check_method(self, qtbot, sample_config):
        """Test MainWindow has check_for_updates_startup method."""
        from webdrop_bridge.ui.main_window import MainWindow
        
        window = MainWindow(sample_config)
        qtbot.addWidget(window)
        
        assert hasattr(window, "check_for_updates_startup")
        assert callable(window.check_for_updates_startup)

    def test_window_has_update_available_signal(self, qtbot, sample_config):
        """Test MainWindow has update_available signal."""
        from webdrop_bridge.ui.main_window import MainWindow
        
        window = MainWindow(sample_config)
        qtbot.addWidget(window)
        
        assert hasattr(window, "update_available")

    def test_startup_check_initializes_without_error(self, qtbot, sample_config):
        """Test startup check can be called without raising."""
        from webdrop_bridge.ui.main_window import MainWindow
        
        window = MainWindow(sample_config)
        qtbot.addWidget(window)
        
        # Should not raise
        window.check_for_updates_startup()

    def test_on_update_status_updates_status_bar(self, qtbot, sample_config):
        """Test _on_update_status updates the status bar."""
        from webdrop_bridge.ui.main_window import MainWindow
        
        window = MainWindow(sample_config)
        qtbot.addWidget(window)
        
        window._on_update_status("Testing", "✓")
        assert "Testing" in window.update_status_label.text()
        assert "✓" in window.update_status_label.text()

    def test_on_update_available_emits_signal(self, qtbot, sample_config):
        """Test _on_update_available shows dialog and updates status."""
        from unittest.mock import patch

        from webdrop_bridge.ui.main_window import MainWindow
        
        window = MainWindow(sample_config)
        qtbot.addWidget(window)
        
        # Create mock release
        mock_release = MagicMock()
        mock_release.version = "0.0.2"
        mock_release.body = "Bug fixes"
        
        # Mock the dialog creation to avoid showing it
        with patch('webdrop_bridge.ui.update_manager_ui.UpdateAvailableDialog'):
            window._on_update_available(mock_release)
            assert "0.0.2" in window.update_status_label.text()

    def test_on_update_available_updates_status(self, qtbot, sample_config):
        """Test _on_update_available updates status bar."""
        from unittest.mock import patch

        from webdrop_bridge.ui.main_window import MainWindow
        
        window = MainWindow(sample_config)
        qtbot.addWidget(window)
        
        # Create mock release
        mock_release = MagicMock()
        mock_release.version = "0.0.2"
        mock_release.body = "Bug fixes"
        
        # Mock the dialog creation to avoid showing it
        with patch('webdrop_bridge.ui.update_manager_ui.UpdateAvailableDialog'):
            window._on_update_available(mock_release)
            assert "0.0.2" in window.update_status_label.text()
            assert "✅" in window.update_status_label.text()
