"""
Storage backend factory.

Usage:
    from autopil.db import create_stores

    audit_log, key_store, tenant_store, policy_store = create_stores(database_url="postgresql://...")
    audit_log, key_store, tenant_store, policy_store = create_stores(db_path="/data/autopil.db")
"""

from .base import AuditLogBase, KeyStoreBase, TenantStoreBase, PolicyStoreBase


def create_stores(
    database_url: str | None = None,
    db_path: str = "autopil_audit.db",
) -> tuple[AuditLogBase, KeyStoreBase, TenantStoreBase, PolicyStoreBase]:
    """
    Return (audit_log, key_store, tenant_store, policy_store) using the appropriate backend.

    - If database_url is set (postgresql://...) → Postgres backend
    - Otherwise → SQLite backend using db_path
    """
    if database_url:
        from .postgres import PostgresAuditLog, PostgresKeyStore, PostgresTenantStore, PostgresPolicyStore
        return (
            PostgresAuditLog(database_url),
            PostgresKeyStore(database_url),
            PostgresTenantStore(database_url),
            PostgresPolicyStore(database_url),
        )

    from .sqlite import SQLiteAuditLog, SQLiteKeyStore, SQLiteTenantStore, SQLitePolicyStore
    return (
        SQLiteAuditLog(db_path),
        SQLiteKeyStore(db_path),
        SQLiteTenantStore(db_path),
        SQLitePolicyStore(db_path),
    )


__all__ = ["create_stores", "AuditLogBase", "KeyStoreBase", "TenantStoreBase", "PolicyStoreBase"]
