from typing import Optional, List # Added List
from datetime import datetime, timezone

from .backends.base import BaseBackend, AuditLogEntry # Added BaseBackend, AuditLogEntry


# initialize_audit_db_schema function removed


class AuditLogger:
    """
    A class for logging audit trail entries, delegating to a BaseBackend.
    """
    def __init__(self, backend: BaseBackend):
        """
        Initializes the AuditLogger with a backend.

        Args:
            backend: An instance of a class that implements BaseBackend.
        """
        self.backend = backend
        # self.db_path and self.conn removed
        # Parent directory creation logic removed

    # connect, close, __enter__, __exit__, and get_db_path methods are now removed.

    def log_event(self, app_name: str, user_name: str, model: str, log_type: str,
                  prompt_text: Optional[str] = None, response_text: Optional[str] = None,
                  remote_completion_id: Optional[str] = None, project: Optional[str] = None,
                  timestamp: Optional[datetime] = None):
        """
        Logs an event to the audit log using the backend.
        """
        ts = timestamp if timestamp is not None else datetime.now(timezone.utc)
        audit_entry = AuditLogEntry(
            timestamp=ts,
            app_name=app_name,
            user_name=user_name,
            model=model,
            prompt_text=prompt_text,
            response_text=response_text,
            remote_completion_id=remote_completion_id,
            project=project,
            log_type=log_type,
            id=None  # id is generated by the backend
        )
        self.backend.log_audit_event(audit_entry)

    def log_prompt(self, app_name: str, user_name: str, model: str, prompt_text: str,
                   project: Optional[str] = None, timestamp: Optional[datetime] = None):
        """
        Logs a prompt event to the audit log using the backend.
        """
        ts = timestamp if timestamp is not None else datetime.now(timezone.utc)
        audit_entry = AuditLogEntry(
            timestamp=ts,
            app_name=app_name,
            user_name=user_name,
            model=model,
            prompt_text=prompt_text,
            response_text=None,
            remote_completion_id=None,
            project=project,
            log_type='prompt',
            id=None  # id is generated by the backend
        )
        self.backend.log_audit_event(audit_entry)

    def log_response(self, app_name: str, user_name: str, model: str, response_text: str,
                     remote_completion_id: Optional[str] = None, project: Optional[str] = None,
                     timestamp: Optional[datetime] = None):
        """
        Logs a response event to the audit log using the backend.
        """
        ts = timestamp if timestamp is not None else datetime.now(timezone.utc)
        audit_entry = AuditLogEntry(
            timestamp=ts,
            app_name=app_name,
            user_name=user_name,
            model=model,
            prompt_text=None,
            response_text=response_text,
            remote_completion_id=remote_completion_id,
            project=project,
            log_type='response',
            id=None  # id is generated by the backend
        )
        self.backend.log_audit_event(audit_entry)

    def get_entries(
        self,
        start_date: Optional[datetime] = None,
        end_date: Optional[datetime] = None,
        app_name: Optional[str] = None,
        user_name: Optional[str] = None,
        project: Optional[str] = None,
        log_type: Optional[str] = None,
        limit: Optional[int] = None,
    ) -> List[AuditLogEntry]:
        """Retrieves audit log entries from the backend."""
        return self.backend.get_audit_log_entries(
            start_date=start_date,
            end_date=end_date,
            app_name=app_name,
            user_name=user_name,
            project=project,
            log_type=log_type,
            limit=limit
        )
