"""
Module: proposal_gen.py
Description: Auto-generates client proposals based on predefined templates
and input data.
"""

import os
import logging
from datetime import datetime
from typing import Dict, Any, List, Optional

# Configure logging
logging.basicConfig(level=logging.INFO,
                    format='%(asctime)s - %(levelname)s - %(message)s')

# Define constants
TEMPLATE_DIR = "/mnt/e/genesis-system/core/revenue/templates"  # Assuming templates are stored here
DEFAULT_PROPOSAL_FILENAME = "proposal_{}.txt"
GENESIS_LEGAL_DISCLAIMER = "This proposal is subject to Genesis Corp's standard terms and conditions." #Replace with real legal disclaimers


class ProposalGenerator:
    """
    Generates client proposals based on provided data and templates.
    """

    def __init__(self, template_dir: str = TEMPLATE_DIR, legal_disclaimer: str = GENESIS_LEGAL_DISCLAIMER) -> None:
        """
        Initializes the ProposalGenerator.

        Args:
            template_dir: The directory containing proposal templates.
            legal_disclaimer: Standard legal disclaimers for the proposal.
        """
        self.template_dir = template_dir
        self.legal_disclaimer = legal_disclaimer
        self.templates = self._load_templates()

    def _load_templates(self) -> Dict[str, str]:
        """
        Loads proposal templates from the template directory.

        Returns:
            A dictionary mapping template names to template content.
        """
        templates: Dict[str, str] = {}
        try:
            for filename in os.listdir(self.template_dir):
                if filename.endswith(".txt"):  # Assuming templates are text files
                    filepath = os.path.join(self.template_dir, filename)
                    with open(filepath, "r") as f:
                        templates[filename[:-4]] = f.read()  # Remove .txt extension
            logging.info(f"Loaded templates from {self.template_dir}")
        except FileNotFoundError:
            logging.error(f"Template directory not found: {self.template_dir}")
            raise
        except Exception as e:
            logging.error(f"Error loading templates: {e}")
            raise
        return templates

    def generate_proposal(self, template_name: str, data: Dict[str, Any], output_filename: Optional[str] = None) -> str:
        """
        Generates a proposal based on a template and provided data.

        Args:
            template_name: The name of the template to use.
            data: A dictionary containing the data to populate the template.
            output_filename: Optional filename for saving the proposal. If None, the proposal will not be saved.

        Returns:
            The generated proposal content as a string.

        Raises:
            ValueError: If the specified template does not exist.
        """

        if template_name not in self.templates:
            logging.error(f"Template not found: {template_name}")
            raise ValueError(f"Template '{template_name}' not found.")

        try:
            template = self.templates[template_name]
            proposal_content = template.format(**data) # format the template with provided data.
            proposal_content += "\n\n" + self.legal_disclaimer #append legal disclaimer

            if output_filename:
                self.save_proposal(proposal_content, output_filename)

            logging.info(f"Generated proposal using template: {template_name}")
            return proposal_content
        except KeyError as e:
            logging.error(f"Missing data in proposal template: {e}")
            raise ValueError(f"Missing data in proposal template. Key: {e}")
        except Exception as e:
            logging.error(f"Error generating proposal: {e}")
            raise

    def save_proposal(self, proposal_content: str, filename: str) -> None:
        """
        Saves the generated proposal to a file.

        Args:
            proposal_content: The content of the proposal.
            filename: The name of the file to save the proposal to.
        """
        try:
            with open(filename, "w") as f:
                f.write(proposal_content)
            logging.info(f"Proposal saved to: {filename}")
        except Exception as e:
            logging.error(f"Error saving proposal to file: {e}")
            raise

def generate_default_filename() -> str:
    """
    Generates a default proposal filename with a timestamp.

    Returns:
        A string representing the default filename.
    """
    timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
    return DEFAULT_PROPOSAL_FILENAME.format(timestamp)

# Example Usage (can be moved to a separate test script)
if __name__ == "__main__":
    try:
        proposal_gen = ProposalGenerator()

        # Example Data
        client_data = {
            "client_name": "Acme Corp",
            "project_name": "Project Phoenix",
            "description": "Develop a new AI-powered system.",
            "price": "$100,000",
            "start_date": "2024-01-15",
            "end_date": "2024-06-30"
        }

        # Generate proposal
        proposal = proposal_gen.generate_proposal("standard_proposal", client_data, "acme_corp_proposal.txt")

        print("Proposal Generated:\n", proposal)

        # Generate proposal with default filename
        proposal_gen.generate_proposal("standard_proposal", client_data, generate_default_filename())


    except Exception as e:
        logging.error(f"An error occurred during example usage: {e}")