"""Tests pour la commande CLI fetch."""

import pytest
from unittest.mock import patch, MagicMock
from typer.testing import CliRunner

from pricewatch.app.cli.main import app

runner = CliRunner()


class TestFetchCommand:
    """Tests pour la commande fetch."""

    def test_fetch_conflicting_options(self):
        """Fetch doit echouer si --http et --playwright sont specifies."""
        result = runner.invoke(
            app, ["fetch", "https://example.com", "--http", "--playwright"]
        )
        assert result.exit_code == 1
        assert "impossible" in result.stdout.lower()

    @patch("pricewatch.app.cli.main.fetch_http")
    def test_fetch_http_success(self, mock_fetch: MagicMock):
        """Fetch HTTP doit afficher le resultat."""
        mock_result = MagicMock()
        mock_result.success = True
        mock_result.html = "<html>test</html>"
        mock_result.status_code = 200
        mock_result.duration_ms = 150
        mock_fetch.return_value = mock_result

        result = runner.invoke(app, ["fetch", "https://example.com", "--http"])
        assert result.exit_code == 0
        assert "Succes" in result.stdout or "✓" in result.stdout
        assert "150" in result.stdout

    @patch("pricewatch.app.cli.main.fetch_http")
    def test_fetch_http_failure(self, mock_fetch: MagicMock):
        """Fetch HTTP doit signaler l'echec."""
        mock_result = MagicMock()
        mock_result.success = False
        mock_result.error = "Connection refused"
        mock_fetch.return_value = mock_result

        result = runner.invoke(app, ["fetch", "https://example.com", "--http"])
        assert result.exit_code == 1
        assert "Connection refused" in result.stdout

    @patch("pricewatch.app.cli.main.fetch_playwright")
    def test_fetch_playwright_success(self, mock_fetch: MagicMock):
        """Fetch Playwright doit afficher le resultat."""
        mock_result = MagicMock()
        mock_result.success = True
        mock_result.html = "<html>test playwright</html>"
        mock_result.duration_ms = 2500
        mock_fetch.return_value = mock_result

        result = runner.invoke(app, ["fetch", "https://example.com", "--playwright"])
        assert result.exit_code == 0
        assert "Succes" in result.stdout or "✓" in result.stdout
        assert "2500" in result.stdout

    @patch("pricewatch.app.cli.main.fetch_playwright")
    def test_fetch_playwright_failure(self, mock_fetch: MagicMock):
        """Fetch Playwright doit signaler l'echec."""
        mock_result = MagicMock()
        mock_result.success = False
        mock_result.error = "Timeout waiting for page"
        mock_fetch.return_value = mock_result

        result = runner.invoke(app, ["fetch", "https://example.com", "--playwright"])
        assert result.exit_code == 1
        assert "Timeout" in result.stdout

    @patch("pricewatch.app.cli.main.fetch_playwright")
    def test_fetch_default_is_playwright(self, mock_fetch: MagicMock):
        """Fetch sans option utilise Playwright par defaut."""
        mock_result = MagicMock()
        mock_result.success = True
        mock_result.html = "<html>test</html>"
        mock_result.duration_ms = 1000
        mock_fetch.return_value = mock_result

        result = runner.invoke(app, ["fetch", "https://example.com"])
        assert result.exit_code == 0
        mock_fetch.assert_called_once()

    @patch("pricewatch.app.cli.main.fetch_playwright")
    def test_fetch_with_debug(self, mock_fetch: MagicMock):
        """Fetch doit fonctionner avec --debug."""
        mock_result = MagicMock()
        mock_result.success = True
        mock_result.html = "<html>test</html>"
        mock_result.duration_ms = 1000
        mock_fetch.return_value = mock_result

        result = runner.invoke(app, ["fetch", "https://example.com", "--debug"])
        assert result.exit_code == 0
