//! 控制面 store trait。

use crate::error::ControlPlaneError;
use crate::model::{
    AiPolicyRecord, AiRuntimeEventRecord, AiRuntimeRunRecord, AppendAiRuntimeEventInput,
    AppendAuditInput, AuditLogRecord, AuthSessionRecord, AuthenticatePasswordInput,
    CreatePasswordIdentityInput, CreateSessionInput, CreateShareLinkInput, CreatedShareLink,
    DirectoryGrantInput, DirectoryGrantLookup, DirectoryGrantRecord, OutboxEventInput,
    OutboxEventRecord, ResolvedAccess, ResolvedAuthSession, ShareLinkRecord, SidebarShortcutRecord,
    SyncStateRecord, UpsertAiPolicyInput, UpsertAiRuntimeRunInput, UpsertSidebarShortcutInput,
    UpsertSyncStateInput, UpsertUserInput, UpsertUserUiPreferenceInput, UserRecord,
    UserUiPreferenceRecord, WorkspaceRecord,
};

pub trait ControlPlaneStore: Send + Sync {
    fn upsert_user(&self, input: UpsertUserInput) -> Result<UserRecord, ControlPlaneError>;

    fn create_password_identity(
        &self,
        input: CreatePasswordIdentityInput,
    ) -> Result<(), ControlPlaneError>;

    fn authenticate_password(
        &self,
        input: AuthenticatePasswordInput,
    ) -> Result<ResolvedAuthSession, ControlPlaneError>;

    fn create_session(
        &self,
        input: CreateSessionInput,
    ) -> Result<AuthSessionRecord, ControlPlaneError>;

    fn get_session_by_token_hash(
        &self,
        token_hash: &str,
    ) -> Result<Option<ResolvedAuthSession>, ControlPlaneError>;

    fn revoke_session(&self, session_id: &str) -> Result<(), ControlPlaneError>;

    fn ensure_default_workspace(
        &self,
        actor_id: &str,
    ) -> Result<WorkspaceRecord, ControlPlaneError>;

    fn grant_directory_access(
        &self,
        input: DirectoryGrantInput,
    ) -> Result<DirectoryGrantRecord, ControlPlaneError>;

    fn list_directory_grants(&self) -> Result<Vec<DirectoryGrantRecord>, ControlPlaneError>;

    fn list_directory_grants_for_actor(
        &self,
        actor_id: &str,
    ) -> Result<Vec<DirectoryGrantRecord>, ControlPlaneError>;

    fn find_directory_grants(
        &self,
        lookup: DirectoryGrantLookup,
    ) -> Result<Vec<DirectoryGrantRecord>, ControlPlaneError>;

    fn revoke_directory_grant(
        &self,
        grant_id: &str,
        expected_revision: Option<i64>,
    ) -> Result<(), ControlPlaneError>;

    fn resolve_access(
        &self,
        actor_id: &str,
        root_uri: &str,
    ) -> Result<ResolvedAccess, ControlPlaneError>;

    fn create_share_link(
        &self,
        input: CreateShareLinkInput,
    ) -> Result<CreatedShareLink, ControlPlaneError>;

    fn list_share_links(
        &self,
        workspace_id: &str,
    ) -> Result<Vec<ShareLinkRecord>, ControlPlaneError>;

    fn resolve_share_link(
        &self,
        token_hash: &str,
    ) -> Result<Option<ShareLinkRecord>, ControlPlaneError>;

    fn revoke_share_link(&self, link_id: &str) -> Result<(), ControlPlaneError>;

    fn append_audit(&self, input: AppendAuditInput) -> Result<(), ControlPlaneError>;

    fn list_audit_log(&self, limit: usize) -> Result<Vec<AuditLogRecord>, ControlPlaneError>;

    fn append_outbox(
        &self,
        input: OutboxEventInput,
    ) -> Result<OutboxEventRecord, ControlPlaneError>;

    fn drain_outbox(&self, limit: usize) -> Result<Vec<OutboxEventRecord>, ControlPlaneError>;

    fn mark_outbox_delivered(&self, event_id: &str) -> Result<(), ControlPlaneError>;

    fn upsert_sidebar_shortcut(
        &self,
        input: UpsertSidebarShortcutInput,
    ) -> Result<SidebarShortcutRecord, ControlPlaneError>;

    fn list_sidebar_shortcuts(
        &self,
        user_id: &str,
        workspace_id: &str,
    ) -> Result<Vec<SidebarShortcutRecord>, ControlPlaneError>;

    fn list_sidebar_shortcuts_with_global_local(
        &self,
        user_id: &str,
        workspace_id: &str,
    ) -> Result<Vec<SidebarShortcutRecord>, ControlPlaneError>;

    fn delete_sidebar_shortcut(
        &self,
        user_id: &str,
        shortcut_id: &str,
    ) -> Result<(), ControlPlaneError>;

    fn upsert_user_ui_preference(
        &self,
        input: UpsertUserUiPreferenceInput,
    ) -> Result<UserUiPreferenceRecord, ControlPlaneError>;

    fn list_user_ui_preferences(
        &self,
        user_id: &str,
        workspace_id: Option<&str>,
        source_kind: Option<&str>,
    ) -> Result<Vec<UserUiPreferenceRecord>, ControlPlaneError>;

    fn upsert_ai_policy(
        &self,
        input: UpsertAiPolicyInput,
    ) -> Result<AiPolicyRecord, ControlPlaneError>;

    fn get_ai_policy(
        &self,
        actor_id: &str,
        workspace_id: Option<&str>,
    ) -> Result<Option<AiPolicyRecord>, ControlPlaneError>;

    fn upsert_sync_state(
        &self,
        input: UpsertSyncStateInput,
    ) -> Result<SyncStateRecord, ControlPlaneError>;

    fn get_sync_state(
        &self,
        workspace_id: &str,
        remote_kind: &str,
    ) -> Result<Option<SyncStateRecord>, ControlPlaneError>;

    fn upsert_ai_runtime_run(
        &self,
        input: UpsertAiRuntimeRunInput,
    ) -> Result<AiRuntimeRunRecord, ControlPlaneError>;

    fn list_ai_runtime_runs(
        &self,
        user_id: &str,
        workspace_id: Option<&str>,
        document_id: Option<&str>,
        session_id: Option<&str>,
        limit: usize,
    ) -> Result<Vec<AiRuntimeRunRecord>, ControlPlaneError>;

    fn append_ai_runtime_event(
        &self,
        input: AppendAiRuntimeEventInput,
    ) -> Result<AiRuntimeEventRecord, ControlPlaneError>;

    fn list_ai_runtime_events(
        &self,
        user_id: &str,
        run_id: &str,
        limit: usize,
    ) -> Result<Vec<AiRuntimeEventRecord>, ControlPlaneError>;

    fn rename_ai_runtime_session(
        &self,
        user_id: &str,
        session_id: &str,
        workspace_id: Option<&str>,
        title: &str,
    ) -> Result<Vec<AiRuntimeRunRecord>, ControlPlaneError>;

    fn auto_title_ai_runtime_session(
        &self,
        user_id: &str,
        session_id: &str,
        workspace_id: Option<&str>,
    ) -> Result<Option<AiRuntimeRunRecord>, ControlPlaneError>;

    fn delete_ai_runtime_session(
        &self,
        user_id: &str,
        session_id: &str,
        workspace_id: Option<&str>,
    ) -> Result<usize, ControlPlaneError>;
}
