use crate::common::{ActorPayload, AffectedObject, SourcePayload, TargetRef};

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct CommandEnvelope<T> {
    pub name: String,
    pub command_id: String,
    pub idempotency_key: Option<String>,
    pub actor: ActorPayload,
    pub source: SourcePayload,
    pub target: Option<TargetRef>,
    pub payload: T,
    pub reason: Option<String>,
    pub refs: Vec<String>,
    pub dry_run: bool,
    pub validate_only: bool,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct CommandResult {
    pub ok: bool,
    pub command_id: String,
    pub event_ids: Vec<String>,
    pub affected_objects: Vec<AffectedObject>,
    pub revision: Option<u64>,
    pub warnings: Vec<String>,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct CreateWorkspace {
    pub name: String,
    pub slug: Option<String>,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct CreatePage {
    pub title: String,
    pub parent_page_id: Option<String>,
    pub position: Option<String>,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct UpdatePageTitle {
    pub page_id: String,
    pub title: String,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct CreateDocumentPage {
    pub page_id: String,
    pub parent_page_id: Option<String>,
    pub title: String,
    pub workspace_seed_id: String,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct MoveDocumentPage {
    pub page_id: String,
    pub parent_page_id: Option<String>,
    pub sort_order: i64,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct DeleteDocumentPage {
    pub page_id: String,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct RestoreDocumentPage {
    pub page_id: String,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct DuplicateDocumentPage {
    pub source_page_id: String,
    pub new_page_id: String,
    pub title: Option<String>,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct CopyTreeDocumentPages {
    pub items_json: String,
    pub target_parent_page_id: Option<String>,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct UpdatePageStats {
    pub page_id: String,
    pub word_count: i64,
    pub character_count: i64,
    pub block_count: i64,
    pub todo_total: i64,
    pub todo_done: i64,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct UpdatePageOptions {
    pub page_id: String,
    pub wide_layout: Option<bool>,
    pub small_text: Option<bool>,
    pub show_heading_numbers: Option<bool>,
    pub show_toc: Option<bool>,
    pub show_structure: Option<bool>,
    pub protect_editing: Option<bool>,
    pub show_word_count: Option<bool>,
    pub collapse_backlinks: Option<bool>,
    pub page_font: Option<String>,
    pub layout_density: Option<String>,
    pub hide_child_pages: Option<bool>,
    pub show_block_ref_count: Option<bool>,
    pub embed_default_block_id: Option<String>,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ReplaceMediaAssetStorage {
    pub asset_id: String,
    pub storage_id: String,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct SavePageContent {
    pub page_id: String,
    pub workspace_id: Option<String>,
    pub revision: Option<u64>,
    pub content_json: String,
    pub conflict_detection_key: Option<String>,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct PatchBlock {
    pub page_id: String,
    pub block_id: String,
    pub workspace_id: Option<String>,
    pub revision: Option<u64>,
    pub block_snapshot_json: String,
    pub conflict_detection_key: Option<String>,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct PatchPageBlock {
    pub page_id: String,
    pub block_id: String,
    pub workspace_id: Option<String>,
    pub revision: Option<u64>,
    pub block_snapshot_json: String,
    pub conflict_detection_key: Option<String>,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct PutMindmap {
    pub document_id: String,
    pub mindmap_id: String,
    pub data_json: String,
    pub create_only: bool,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct InsertBlock {
    pub page_id: String,
    pub block_type: String,
    pub content: String,
    pub parent_block_id: Option<String>,
    pub prev_block_id: Option<String>,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct UpdateBlock {
    pub block_id: String,
    pub patch_content: Option<String>,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct MoveBlock {
    pub block_id: String,
    pub new_parent_block_id: Option<String>,
    pub new_page_id: Option<String>,
    pub prev_block_id: Option<String>,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct EmbedBlock {
    pub source_document_id: String,
    pub source_block_id: String,
    pub target_document_id: String,
    pub target_block_id: Option<String>,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct DeleteBlock {
    pub block_id: String,
}
