"""Create project directory structure for Cognitive Memory Layer."""

from pathlib import Path

# Reflects actual project layout. Run from repo root to create missing dirs/files (touch).
STRUCTURE = {
    ".github": {"workflows": ["ci.yml", "py-cml.yml"]},
    "docker": ["Dockerfile", "docker-compose.yml", "docker-compose.test-key.yml"],
    "examples": {
        "__init__.py": None,
        "README.md": None,
        "requirements.txt": None,
        "api_curl_examples.sh": None,
        "api_direct_minimal.py": None,
        "agent_integration.py": None,
        "anthropic_tool_calling.py": None,
        "async_example.py": None,
        "basic_usage.py": None,
        "chat_with_memory.py": None,
        "embedded_mode.py": None,
        "langchain_integration.py": None,
        "openai_tool_calling.py": None,
        "quickstart.py": None,
        "standalone_demo.py": None,
        "streamlit_app.py": None,
        "openclaw_skill": ["README.md", "SKILL.md"],
    },
    "migrations": {
        "versions": ["001_initial_schema.py"],
        "__init__.py": None,
        "env.py": None,
    },
    "src": {
        "__init__.py": None,
        "main.py": None,
        "celery_app.py": None,
        "api": {
            "__init__.py": None,
            "admin_routes.py": None,
            "app.py": None,
            "auth.py": None,
            "dependencies.py": None,
            "middleware.py": None,
            "routes.py": None,
            "schemas.py": None,
            "dashboard": [
                "__init__.py",
                "_shared.py",
                "overview_routes.py",
                "memory_routes.py",
                "events_routes.py",
                "graph_routes.py",
                "config_routes.py",
                "jobs_routes.py",
                "fact_routes.py",
            ],
        },
        "core": [
            "__init__.py",
            "config.py",
            "enums.py",
            "env_file.py",
            "exceptions.py",
            "schemas.py",
        ],
        "consolidation": [
            "__init__.py",
            "clusterer.py",
            "migrator.py",
            "sampler.py",
            "schema_aligner.py",
            "summarizer.py",
            "triggers.py",
            "worker.py",
        ],
        "extraction": [
            "__init__.py",
            "constraint_extractor.py",
            "entity_extractor.py",
            "fact_extractor.py",
            "relation_extractor.py",
            "unified_write_extractor.py",
            "write_time_facts.py",
        ],
        "forgetting": [
            "__init__.py",
            "actions.py",
            "compression.py",
            "executor.py",
            "interference.py",
            "scorer.py",
            "worker.py",
        ],
        "memory": {
            "__init__.py": None,
            "conversation.py": None,
            "knowledge_base.py": None,
            "orchestrator.py": None,
            "scratch_pad.py": None,
            "seamless_provider.py": None,
            "short_term.py": None,
            "tool_memory.py": None,
            "hippocampal": [
                "__init__.py",
                "redactor.py",
                "store.py",
                "write_gate.py",
            ],
            "neocortical": [
                "__init__.py",
                "fact_store.py",
                "schema_manager.py",
                "schemas.py",
                "store.py",
            ],
            "sensory": ["__init__.py", "buffer.py", "manager.py"],
            "working": ["__init__.py", "chunker.py", "manager.py", "models.py"],
        },
        "reconsolidation": [
            "__init__.py",
            "belief_revision.py",
            "conflict_detector.py",
            "labile_tracker.py",
            "service.py",
        ],
        "retrieval": [
            "__init__.py",
            "classifier.py",
            "memory_retriever.py",
            "packet_builder.py",
            "planner.py",
            "query_types.py",
            "reranker.py",
            "retriever.py",
        ],
        "storage": [
            "__init__.py",
            "async_pipeline.py",
            "base.py",
            "connection.py",
            "event_log.py",
            "models.py",
            "neo4j.py",
            "noop_stores.py",
            "postgres.py",
            "redis.py",
            "utils.py",
        ],
        "utils": [
            "__init__.py",
            "bounded_state.py",
            "embeddings.py",
            "llm.py",
            "logging_config.py",
            "metrics.py",
            "timing.py",
        ],
        "dashboard": [
            "package.json",
            "vite.config.js",
            "static/index.html",
            "static/css/styles.css",
            "static/js/app.js",
            "static/js/api.js",
        ],
    },
    "tests": {
        "unit": [
            "__init__.py",
            "test_api_auth_schemas.py",
            "test_api_ingestion.py",
            "test_api_read.py",
            "test_api_request_logging_rate_limit.py",
            "test_api_routes.py",
            "test_celery_forgetting_task.py",
            "test_chunker_semchunk.py",
            "test_constraint_layer.py",
            "test_consolidation_migrator_sampler.py",
            "test_consolidation_triggers_clusterer_sampler.py",
            "test_core_enums_schemas_config.py",
            "test_core_exceptions.py",
            "test_dashboard_routes.py",
            "test_deep_research_improvements.py",
            "test_embeddings_mock_client.py",
            "test_extraction_entity_relation_fact.py",
            "test_forgetting_executor.py",
            "test_forgetting_scorer_policy_interference.py",
            "test_hippocampal_write_gate_redactor.py",
            "test_llm_conflict_detection_only.py",
            "test_llm_constraint_extractor.py",
            "test_llm_memory_type.py",
            "test_llm_pii_redaction.py",
            "test_llm_query_classifier_only.py",
            "test_llm_salience_refinement.py",
            "test_llm_write_gate_importance.py",
            "test_llm_write_time_facts.py",
            "test_memory_modules_conversation_scratch_tool.py",
            "test_memory_retriever.py",
            "test_neocortical_schemas.py",
            "test_orchestrator_seamless_provider.py",
            "test_query_classifier_constraint_topk.py",
            "test_reconsolidation_labile_conflict_belief.py",
            "test_retrieval_classifier_planner_reranker.py",
            "test_retriever.py",
            "test_routes_helpers.py",
            "test_sensory_buffer_working_memory.py",
            "test_storage_noop_stores.py",
            "test_storage_utils.py",
            "test_timestamp_feature.py",
            "test_timestamp_ingestion.py",
            "test_unified_write_extractor.py",
            "test_utils_timing_metrics.py",
            "test_workflow.py",
            "test_chunk_type_from_memory_type.py",
        ],
        "integration": [
            "__init__.py",
            "conftest.py",
            "test_api_flow.py",
            "test_consolidation_constraint.py",
            "test_consolidation_flow.py",
            "test_constraint_semantic_disconnect.py",
            "test_dashboard_flow.py",
            "test_fact_store_integration.py",
            "test_forgetting_flow.py",
            "test_forgetting_llm_compression.py",
            "test_hippocampal_encode_flow.py",
            "test_historic_timestamps.py",
            "test_memory_type_storage_retrieval.py",
            "test_neocortical_store_flow.py",
            "test_reconsolidation_flow.py",
            "test_retrieval_flow.py",
            "test_short_term_memory_flow.py",
            "test_storage_event_log_repository.py",
            "test_unified_write_path.py",
        ],
        "e2e": ["__init__.py", "test_api_flows.py"],
        "conftest.py": None,
    },
}


def create_structure(base_path: Path, structure: dict) -> None:
    for name, content in structure.items():
        path = base_path / name
        if isinstance(content, dict):
            path.mkdir(parents=True, exist_ok=True)
            create_structure(path, content)
        elif isinstance(content, list):
            path.mkdir(parents=True, exist_ok=True)
            for file in content:
                (path / file).touch()
        elif content is None:
            path.parent.mkdir(parents=True, exist_ok=True)
            path.touch()


if __name__ == "__main__":
    create_structure(Path(__file__).resolve().parent.parent, STRUCTURE)
    print("Directory structure created.")
