import os
import toml
import logging
from pathlib import Path

# Configure logging
logging.basicConfig(level=logging.INFO, format="%(levelname)s: %(message)s")

class WorkspaceManager:
    """
    Auto-generated by mulch based on scaffold.json.
    """

    WORKSPACES_DIR_NAME = "workspaces"

    ROOT_CONTENTS = ["config", "data", "imports", "exports", "scripts", "secrets", "queries"]

    EXPORTS_DIR_NAME = ["aggregate"]

    CONFIG_DIR_NAME = ["default-workspace.toml", "logging.json"]

    SECRETS_DIR_NAME = ["secrets.yaml", "secrets-example.yaml"]

    QUERIES_DIR_NAME = ["default-queries.toml"]


    DEFAULT_WORKSPACE_TOML = "default-workspace.toml"
    SECRETS_YAML = "secrets.yaml"
    SECRETS_EXAMPLE_YAML = "secrets-example.yaml"
    DEFAULT_QUERIES_TOML = "default-queries.toml"

    ROOT_DIR = Path(__file__).resolve().parents[2]

    def __init__(self, workspace_name):
        self.workspace_name = workspace_name
        self.workspace_dir = self.ROOT_DIR / self.WORKSPACES_DIR_NAME / workspace_name
        self.ensure_structure()

    def ensure_structure(self):
        """
        Ensures all directories and files from scaffold exist in the workspace.
        """

        base = self.workspace_dir / ""

        path = base / "config"
        if "." in "config":
            if not path.exists():
                path.parent.mkdir(parents=True, exist_ok=True)
                path.touch()
                logging.DEBUG(f"Created file: config")
        else:
            if not path.exists():
                path.mkdir(parents=True, exist_ok=True)
                logging.DEBUG(f"Created folder: config")

        path = base / "data"
        if "." in "data":
            if not path.exists():
                path.parent.mkdir(parents=True, exist_ok=True)
                path.touch()
                logging.DEBUG(f"Created file: data")
        else:
            if not path.exists():
                path.mkdir(parents=True, exist_ok=True)
                logging.DEBUG(f"Created folder: data")

        path = base / "imports"
        if "." in "imports":
            if not path.exists():
                path.parent.mkdir(parents=True, exist_ok=True)
                path.touch()
                logging.DEBUG(f"Created file: imports")
        else:
            if not path.exists():
                path.mkdir(parents=True, exist_ok=True)
                logging.DEBUG(f"Created folder: imports")

        path = base / "exports"
        if "." in "exports":
            if not path.exists():
                path.parent.mkdir(parents=True, exist_ok=True)
                path.touch()
                logging.DEBUG(f"Created file: exports")
        else:
            if not path.exists():
                path.mkdir(parents=True, exist_ok=True)
                logging.DEBUG(f"Created folder: exports")

        path = base / "scripts"
        if "." in "scripts":
            if not path.exists():
                path.parent.mkdir(parents=True, exist_ok=True)
                path.touch()
                logging.DEBUG(f"Created file: scripts")
        else:
            if not path.exists():
                path.mkdir(parents=True, exist_ok=True)
                logging.DEBUG(f"Created folder: scripts")

        path = base / "secrets"
        if "." in "secrets":
            if not path.exists():
                path.parent.mkdir(parents=True, exist_ok=True)
                path.touch()
                logging.DEBUG(f"Created file: secrets")
        else:
            if not path.exists():
                path.mkdir(parents=True, exist_ok=True)
                logging.DEBUG(f"Created folder: secrets")

        path = base / "queries"
        if "." in "queries":
            if not path.exists():
                path.parent.mkdir(parents=True, exist_ok=True)
                path.touch()
                logging.DEBUG(f"Created file: queries")
        else:
            if not path.exists():
                path.mkdir(parents=True, exist_ok=True)
                logging.DEBUG(f"Created folder: queries")


        base = self.workspace_dir / "exports"

        path = base / "aggregate"
        if "." in "aggregate":
            if not path.exists():
                path.parent.mkdir(parents=True, exist_ok=True)
                path.touch()
                logging.DEBUG(f"Created file: aggregate")
        else:
            if not path.exists():
                path.mkdir(parents=True, exist_ok=True)
                logging.DEBUG(f"Created folder: aggregate")


        base = self.workspace_dir / "config"

        path = base / "default-workspace.toml"
        if "." in "default-workspace.toml":
            if not path.exists():
                path.parent.mkdir(parents=True, exist_ok=True)
                path.touch()
                logging.DEBUG(f"Created file: default-workspace.toml")
        else:
            if not path.exists():
                path.mkdir(parents=True, exist_ok=True)
                logging.DEBUG(f"Created folder: default-workspace.toml")

        path = base / "logging.json"
        if "." in "logging.json":
            if not path.exists():
                path.parent.mkdir(parents=True, exist_ok=True)
                path.touch()
                logging.DEBUG(f"Created file: logging.json")
        else:
            if not path.exists():
                path.mkdir(parents=True, exist_ok=True)
                logging.DEBUG(f"Created folder: logging.json")


        base = self.workspace_dir / "secrets"

        path = base / "secrets.yaml"
        if "." in "secrets.yaml":
            if not path.exists():
                path.parent.mkdir(parents=True, exist_ok=True)
                path.touch()
                logging.DEBUG(f"Created file: secrets.yaml")
        else:
            if not path.exists():
                path.mkdir(parents=True, exist_ok=True)
                logging.DEBUG(f"Created folder: secrets.yaml")

        path = base / "secrets-example.yaml"
        if "." in "secrets-example.yaml":
            if not path.exists():
                path.parent.mkdir(parents=True, exist_ok=True)
                path.touch()
                logging.DEBUG(f"Created file: secrets-example.yaml")
        else:
            if not path.exists():
                path.mkdir(parents=True, exist_ok=True)
                logging.DEBUG(f"Created folder: secrets-example.yaml")


        base = self.workspace_dir / "queries"

        path = base / "default-queries.toml"
        if "." in "default-queries.toml":
            if not path.exists():
                path.parent.mkdir(parents=True, exist_ok=True)
                path.touch()
                logging.DEBUG(f"Created file: default-queries.toml")
        else:
            if not path.exists():
                path.mkdir(parents=True, exist_ok=True)
                logging.DEBUG(f"Created folder: default-queries.toml")



    def get_workspace_path(self):
        return self.workspace_dir

    def get_config_path(self, filename=DEFAULT_WORKSPACE_TOML):
        return self.workspace_dir / "config" / filename

    def get_secrets_path(self):
        return self.workspace_dir / "secrets" / self.SECRETS_YAML

    def get_queries_path(self):
        return self.workspace_dir / "queries" / self.DEFAULT_QUERIES_TOML


if __name__ == "__main__":
    wm = WorkspaceManager("")
    print("Workspace initialized:", wm.get_workspace_path())