"""Auto-learned skill generated by GPIA."""

from typing import Any, Dict

from skills.base import Skill, SkillCategory, SkillContext, SkillLevel, SkillMetadata, SkillResult

def solve(task: str, context: dict) -> object:
    import re
    numbers = re.findall(r"\d+", task)
    n = int(numbers[0]) if numbers else 0
    if n <= 0:
        return {"sequence": []}
    seq = [0, 1]
    while len(seq) < n:
        seq.append(seq[-1] + seq[-2])
    return {"sequence": seq[:n]}

class AutoLearnedSkill(Skill):
    _cached_metadata: SkillMetadata = None

    def metadata(self) -> SkillMetadata:
        if AutoLearnedSkill._cached_metadata is None:
            AutoLearnedSkill._cached_metadata = SkillMetadata(
                id="auto_learned/write_a_python_factorial_n_with_docstrin_da8f5da1",
                name="Auto Learned write_a_python_factorial_n_with_docstrin",
                description="Auto-learned skill for: write a python factorial(n) with docstring and input validation",
                category=SkillCategory.AUTOMATION,
                level=SkillLevel.INTERMEDIATE,
                tags=["auto-learned", "drafted"],
            )
        return AutoLearnedSkill._cached_metadata

    def execute(self, input_data: Dict[str, Any], context: SkillContext) -> SkillResult:
        task = input_data.get("task", "")
        payload = input_data.get("context", input_data)
        try:
            result = solve(task, payload)
        except Exception as exc:
            return SkillResult(
                success=False,
                output=None,
                error=str(exc),
                error_code="AUTO_LEARNED_ERROR",
                skill_id=self.metadata().id,
            )
        output = result if isinstance(result, dict) else {"result": result}
        return SkillResult(
            success=True,
            output=output,
            skill_id=self.metadata().id,
        )
