"""PATH-002: Absolute Paths detection rule."""

from __future__ import annotations

import ast

from . import Rule
from ..models import Finding, Severity


class AbsolutePathsRule(Rule):
    """Detects hardcoded absolute filesystem paths."""

    @property
    def rule_id(self) -> str:
        return "PATH-002"

    @property
    def name(self) -> str:
        return "Absolute Paths"

    @property
    def severity(self) -> Severity:
        return Severity.MEDIUM

    @property
    def description(self) -> str:
        return "Hardcoded absolute paths reduce portability and may leak info."

    def check(self, tree: ast.Module, source: str) -> list[Finding]:
        """Check for hardcoded absolute paths in string constants."""
        findings: list[Finding] = []
        prefixes = ("/Users/", "/home/", "C:\\Users\\")

        for node in ast.walk(tree):
            if not isinstance(node, ast.Constant):
                continue
            if not isinstance(node.value, str):
                continue
            if any(node.value.startswith(p) for p in prefixes):
                findings.append(Finding(
                    rule_id=self.rule_id,
                    severity=self.severity,
                    title="Path absoluto hardcodeado",
                    description=f"Absolute path: {node.value[:60]}...",
                    file="",
                    line=node.lineno,
                    code_snippet=self._get_snippet(source, node.lineno),
                    recommendation="Use Path.home() or environment variables instead.",
                    cwe="CWE-22",
                    confidence="MEDIUM",
                ))
        return findings
