import uuid
from datetime import datetime

class PRDGenerator:
    """
    A simple PRD (Product Requirements Document) Auto-Generator inspired by the
    Ralph Wiggum Loop: direct, straightforward, and focused on basic output
    for identified capability gaps.
    """

    def generate_prd(self, gap_description: str) -> dict:
        """
        Generates a basic Product Requirements Document (PRD) based on a
        given capability gap description.

        Args:
            gap_description (str): A description of the capability gap detected.
            This should describe the *missing* capability, e.g., "real-time data streaming"
            or "user authentication."

        Returns:
            dict: A dictionary representing the generated PRD.

        Raises:
            ValueError: If gap_description is empty or not a string.
        """
        if not isinstance(gap_description, str) or not gap_description.strip():
            raise ValueError("Gap description must be a non-empty string.")

        # Sanitize input for use in titles/descriptions
        sanitized_gap = gap_description.strip().capitalize()
        prd_id = str(uuid.uuid4())
        creation_date = datetime.now().isoformat()

        # --- Ralph Wiggum Loop: Direct translation of gap to PRD elements ---

        # 1. Title: Directly uses the sanitized gap description.
        title = f"PRD: Addressing the Capability Gap: {sanitized_gap}"

        # 2. Description: General statement about the PRD's purpose.
        description = (
            f"This Product Requirements Document outlines the necessary features "
            f"and functionalities to address the identified capability gap: '{sanitized_gap}'. "
            f"The goal is to provide a clear path for development and verification."
        )

        # 3. Problem Statement: Directly states the gap as a problem.
        problem_statement = (
            f"The current system has a significant capability gap related to '{sanitized_gap}'. "
            f"This gap is preventing users from achieving [specific goal related to {sanitized_gap}] "
            f"and impacts [area of impact]."
        )

        # 4. Solution Overview: A simple, direct approach to resolving the gap.
        solution_overview = (
            f"To resolve the '{sanitized_gap}' gap, we propose implementing a new feature "
            f"or enhancing existing functionality to directly address the missing capability. "
            f"This will involve [e.g., building a new module, integrating a service, improving UI/UX]."
        )

        # 5. User Stories and their Acceptance Criteria:
        # Generating simple, atomic, and testable stories directly from the gap.
        # Each story implies a direct action or need related to the gap.
        user_stories = [
            {
                "story": f"As a user, I want to [perform action related to {sanitized_gap}] "
                         f"so that I can [achieve a benefit related to {sanitized_gap}].",
                "acceptance_criteria": [
                    f"The system allows users to [perform action related to {sanitized_gap}] successfully.",
                    f"The outcome of [performing action related to {sanitized_gap}] is verifiable and correct."
                ]
            },
            {
                "story": f"As an administrator, I want to [manage aspects related to {sanitized_gap}] "
                         f"so that I can [ensure the system functions correctly].",
                "acceptance_criteria": [
                    f"Administrators can configure settings for [aspects related to {sanitized_gap}].",
                    f"Changes made by administrators to [aspects related to {sanitized_gap}] are applied correctly."
                ]
            }
        ]

        # 6. Overall PRD Acceptance Criteria: General, verifiable criteria for the PRD itself.
        prd_acceptance_criteria = [
            "All generated user stories are implemented and tested.",
            f"The solution effectively closes the '{sanitized_gap}' capability gap.",
            "The implemented features meet performance and security standards.",
            "Documentation for the new functionality is complete and accurate."
        ]

        prd = {
            "prd_id": prd_id,
            "creation_date": creation_date,
            "title": title,
            "description": description,
            "problem_statement": problem_statement,
            "solution_overview": solution_overview,
            "user_stories": user_stories,
            "overall_acceptance_criteria": prd_acceptance_criteria,
            "status": "Draft" # Indicates this is an initial auto-generated draft
        }

        return prd
