"""Tests for CLI."""
import pytest
import tempfile
import subprocess
import sys
from pathlib import Path


# Create a minimal test deck file for CLI tests
TEST_DECK_CONTENT = '''
from openslides import Brand, base_html

brand = Brand(primary="#054dfe", name="Test")

def slide_hero():
    """Hero slide."""
    return f"""{base_html(brand)}
<body style="background:{brand.background};">
    <h1 style="color:{brand.primary};">Test</h1>
</body></html>"""

def slide_problem():
    """Problem slide."""
    return f"""{base_html(brand)}
<body style="background:{brand.background};">
    <h1>Problem</h1>
</body></html>"""
'''


class TestCLI:
    """Test CLI commands."""

    def test_cli_help(self):
        """CLI --help works."""
        result = subprocess.run(
            [sys.executable, "-m", "openslides.cli", "--help"],
            capture_output=True,
            text=True,
            cwd=str(Path(__file__).parent.parent),
        )
        assert result.returncode == 0
        assert "openslides" in result.stdout
        assert "build" in result.stdout

    def test_cli_version(self):
        """CLI --version works."""
        result = subprocess.run(
            [sys.executable, "-m", "openslides.cli", "--version"],
            capture_output=True,
            text=True,
            cwd=str(Path(__file__).parent.parent),
        )
        assert result.returncode == 0
        assert "1.0.0" in result.stdout

    def test_cli_list(self):
        """CLI list command shows slides."""
        with tempfile.NamedTemporaryFile(mode="w", suffix=".py", delete=False) as f:
            f.write(TEST_DECK_CONTENT)
            f.flush()

            result = subprocess.run(
                [sys.executable, "-m", "openslides.cli", "list", f.name],
                capture_output=True,
                text=True,
                cwd=str(Path(__file__).parent.parent),
            )

            Path(f.name).unlink()

        assert result.returncode == 0
        assert "slide_hero" in result.stdout
        assert "slide_problem" in result.stdout

    def test_cli_build(self):
        """CLI build command creates slides."""
        with tempfile.NamedTemporaryFile(mode="w", suffix=".py", delete=False) as f:
            f.write(TEST_DECK_CONTENT)
            f.flush()

            with tempfile.TemporaryDirectory() as tmpdir:
                result = subprocess.run(
                    [sys.executable, "-m", "openslides.cli", "build", f.name, "-o", tmpdir],
                    capture_output=True,
                    text=True,
                    cwd=str(Path(__file__).parent.parent),
                )

                Path(f.name).unlink()

                assert result.returncode == 0
                assert "Exported 2 slides" in result.stdout
                assert (Path(tmpdir) / "slide-01.png").exists()
                assert (Path(tmpdir) / "slide-02.png").exists()

    def test_cli_build_pdf(self):
        """CLI build --format pdf creates PDF files."""
        with tempfile.NamedTemporaryFile(mode="w", suffix=".py", delete=False) as f:
            f.write(TEST_DECK_CONTENT)
            f.flush()

            with tempfile.TemporaryDirectory() as tmpdir:
                result = subprocess.run(
                    [sys.executable, "-m", "openslides.cli", "build", f.name, "-o", tmpdir, "-f", "pdf"],
                    capture_output=True,
                    text=True,
                    cwd=str(Path(__file__).parent.parent),
                )

                Path(f.name).unlink()

                assert result.returncode == 0
                assert (Path(tmpdir) / "slide-01.pdf").exists()

    def test_cli_build_file_not_found(self):
        """CLI build with nonexistent file fails."""
        result = subprocess.run(
            [sys.executable, "-m", "openslides.cli", "build", "nonexistent.py"],
            capture_output=True,
            text=True,
            cwd=str(Path(__file__).parent.parent),
        )
        assert result.returncode != 0
        assert "not found" in result.stderr.lower()
