"""TEMP-001: Unsafe Temp Files detection rule."""

from __future__ import annotations

import ast

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


class UnsafeTempFilesRule(Rule):
    """Detects insecure temporary file creation."""

    @property
    def rule_id(self) -> str:
        return "TEMP-001"

    @property
    def name(self) -> str:
        return "Unsafe Temp Files"

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

    @property
    def description(self) -> str:
        return "Insecure temporary file creation may be exploitable."

    def check(self, tree: ast.Module, source: str) -> list[Finding]:
        """Check for insecure temp file creation."""
        findings: list[Finding] = []
        lines = self._get_lines(source)

        for i, line in enumerate(lines, 1):
            if "mktemp" in line and "tempfile.mkstemp" not in line:
                findings.append(Finding(
                    rule_id=self.rule_id,
                    severity=self.severity,
                    title="Insecure temporary file creation",
                    description="Use tempfile.mkstemp() or NamedTemporaryFile() instead.",
                    file="",
                    line=i,
                    cwe="CWE-377",
                    confidence="MEDIUM",
                ))
        return findings
