from bukka.logistics.files.file_manager import FileManager
from bukka.coding.utils.template_handler import TemplateBaseClass
from typing import Any

# Minimal pyproject.toml template for pip-installable ML projects
PYPROJECT_TOML_TEMPLATE = '''
# pyproject.toml - Generated by Bukka
# This file makes your project pip-installable with: pip install -e .

[project]
name = "{project_name}"
version = "0.1.0"
requires-python = ">=3.10"
dynamic = ["dependencies"]

[tool.setuptools.dynamic]
dependencies = {{file = ["{requirements_file}"]}}

[build-system]
requires = ["setuptools>=77.0.0"]
build-backend = "setuptools.build_meta"

[tool.setuptools.packages.find]
where = ["."]
include = ["pipelines*", "utils*"]
'''


class PyprojectTomlWriter(TemplateBaseClass):
    """
    Writes a pyproject.toml file to make Bukka-generated projects pip-installable.

    This class extends TemplateBaseClass to generate a pyproject.toml file with
    package metadata, dependencies, and build configuration. The generated file
    enables users to run 'pip install -e .' in the project directory.

    Parameters
    ----------
    file_manager : FileManager
        The FileManager instance containing all project paths.
    project_name : str, optional
        The name of the project. Default is "bukka_project".
    target_column : str, optional
        The name of the target column for ML tasks. Default is "target".
    
    Examples
    --------
    >>> from bukka.logistics.files.file_manager import FileManager
    >>> from pathlib import Path
    >>> file_manager = FileManager(
    ...     project_path=Path("my_project"),
    ...     orig_dataset=Path("data.csv")
    ... )
    >>> writer = PyprojectTomlWriter(
    ...     file_manager=file_manager,
    ...     project_name="my_ml_project",
    ...     target_column="label"
    ... )
    >>> writer.write_class()  # Writes pyproject.toml to project root
    
    >>> # Custom configuration
    >>> writer = PyprojectTomlWriter(
    ...     file_manager=file_manager,
    ...     project_name="classification_project",
    ...     target_column="churn"
    ... )
    >>> writer.write_class()
    """
    
    def __init__(
        self,
        file_manager: FileManager,
        project_name: str = "bukka_project"
    ) -> None:
        """
        Initialize the PyprojectTomlWriter with file manager and configuration.

        Parameters
        ----------
        file_manager : FileManager
            The FileManager instance containing all project paths.
        project_name : str, optional
            The name of the project. Default is "bukka_project".
        
        Examples
        --------
        >>> file_manager = FileManager(Path("project"), Path("data.csv"))
        >>> writer = PyprojectTomlWriter(file_manager, project_name="demo")
        """
        self.file_manager = file_manager
        
        # Build kwargs dictionary with all template values
        kwargs: dict[str, Any] = {
            "project_name": project_name,
            "requirements_file": file_manager.requirements_path.name
        }
        
        expected_args = ["project_name", "requirements_file"]
        
        # Initialize parent class with template and paths
        super().__init__(
            template=PYPROJECT_TOML_TEMPLATE,
            output_path=file_manager.pyproject_toml_path,
            kwargs=kwargs,
            expected_args=expected_args
        )
