"""
Achievable coverage improvements - simple, working tests
"""

from click.testing import CliRunner

from catalogmx.cli import (
    curp,
    curp_generate,
    curp_validate,
    main,
    rfc,
    rfc_generate_fisica,
    rfc_generate_moral,
    rfc_validate,
)


class TestCLICommands:
    """Test CLI commands to improve cli.py coverage"""

    def setup_method(self):
        self.runner = CliRunner()

    def test_main_without_args(self):
        """Test main without args"""
        result = self.runner.invoke(main, [])
        # Exit code 2 is expected for Click groups without subcommand
        assert result.exit_code == 2

    def test_rfc_group_without_args(self):
        """Test RFC group without args"""
        result = self.runner.invoke(rfc, [])
        assert result.exit_code == 2

    def test_curp_group_without_args(self):
        """Test CURP group without args"""
        result = self.runner.invoke(curp, [])
        assert result.exit_code == 2

    def test_rfc_validate_valid_shows_details(self):
        """Test RFC validate shows validation details"""
        result = self.runner.invoke(rfc_validate, ["GODE561231GR8"])
        assert result.exit_code == 0
        assert "valid" in result.output
        assert "Type:" in result.output
        assert "Validation details:" in result.output

    def test_rfc_validate_invalid_shows_error(self):
        """Test RFC validate shows error for invalid"""
        result = self.runner.invoke(rfc_validate, ["INVALID"])
        assert result.exit_code == 0
        assert "invalid" in result.output

    def test_rfc_generate_fisica_shows_all_info(self):
        """Test RFC generate fisica shows all information"""
        result = self.runner.invoke(
            rfc_generate_fisica,
            [
                "--nombre",
                "Juan",
                "--paterno",
                "Garcia",
                "--materno",
                "Lopez",
                "--fecha",
                "1990-05-15",
            ],
        )
        assert result.exit_code == 0
        assert "Generated RFC:" in result.output
        assert "Name:" in result.output
        assert "Birth date:" in result.output

    def test_rfc_generate_fisica_value_error(self):
        """Test RFC generate fisica with ValueError"""
        result = self.runner.invoke(
            rfc_generate_fisica,
            ["--nombre", "Juan", "--paterno", "Garcia", "--fecha", "invalid-date"],
        )
        assert result.exit_code == 0
        assert "Error:" in result.output

    def test_rfc_generate_moral_shows_all_info(self):
        """Test RFC generate moral shows all information"""
        result = self.runner.invoke(
            rfc_generate_moral,
            ["--razon-social", "Tecnologia Sistemas Integrales", "--fecha", "2009-09-09"],
        )
        assert result.exit_code == 0
        assert "Generated RFC:" in result.output
        assert "Company:" in result.output
        assert "Incorporation date:" in result.output

    def test_rfc_generate_moral_value_error(self):
        """Test RFC generate moral with ValueError"""
        result = self.runner.invoke(
            rfc_generate_moral, ["--razon-social", "Test", "--fecha", "invalid"]
        )
        assert result.exit_code == 0
        assert "Error:" in result.output

    def test_curp_validate_valid_shows_message(self):
        """Test CURP validate shows valid message"""
        result = self.runner.invoke(curp_validate, ["GORS561231HVZNNL00"])
        assert result.exit_code == 0
        assert "valid" in result.output

    def test_curp_validate_invalid_shows_message(self):
        """Test CURP validate shows invalid message"""
        result = self.runner.invoke(curp_validate, ["INVALID"])
        assert result.exit_code == 0
        assert "invalid" in result.output

    def test_curp_generate_shows_all_info(self):
        """Test CURP generate shows all information"""
        result = self.runner.invoke(
            curp_generate,
            [
                "--nombre",
                "Juan",
                "--paterno",
                "Garcia",
                "--materno",
                "Lopez",
                "--fecha",
                "1990-05-15",
                "--sexo",
                "H",
                "--estado",
                "Jalisco",
            ],
        )
        assert result.exit_code == 0
        assert "Generated CURP:" in result.output
        assert "Name:" in result.output
        assert "Birth date:" in result.output
        assert "Gender:" in result.output
        assert "Birth state:" in result.output
        assert "homoclave" in result.output
        assert "RENAPO" in result.output

    def test_curp_generate_value_error(self):
        """Test CURP generate with ValueError"""
        result = self.runner.invoke(
            curp_generate,
            [
                "--nombre",
                "Juan",
                "--paterno",
                "Garcia",
                "--fecha",
                "invalid",
                "--sexo",
                "H",
                "--estado",
                "Jalisco",
            ],
        )
        assert result.exit_code == 0
        assert "Error:" in result.output

    def test_curp_generate_exception_handling(self):
        """Test CURP generate generic exception handling"""
        # This will trigger the except Exception block by using invalid state
        result = self.runner.invoke(
            curp_generate,
            [
                "--nombre",
                "Juan",
                "--paterno",
                "Garcia",
                "--materno",
                "Lopez",
                "--fecha",
                "1990-05-15",
                "--sexo",
                "H",
                "--estado",
                "InvalidState",
            ],
        )
        assert result.exit_code == 0
        # CURP generator handles invalid states gracefully and assigns default code
        # So this test should just verify it doesn't crash
        assert "Generated CURP:" in result.output or "Error:" in result.output

    def test_rfc_generate_fisica_exception_handling(self):
        """Test RFC fisica exception handling"""
        # Try to trigger exception with edge case
        result = self.runner.invoke(
            rfc_generate_fisica, ["--nombre", "", "--paterno", "X", "--fecha", "1900-01-01"]
        )
        # Should handle gracefully
        assert result.exit_code == 0 or "error" in result.output.lower()

    def test_rfc_generate_moral_exception_handling(self):
        """Test RFC moral exception handling"""
        # Try to trigger exception
        result = self.runner.invoke(
            rfc_generate_moral, ["--razon-social", "", "--fecha", "2009-09-09"]
        )
        # Should handle gracefully
        assert result.exit_code == 0 or "error" in result.output.lower()
