use crate::editor::model::{EditorBlockDocument, ReferenceTokenStrategy};
use serde::{Deserialize, Serialize};

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
#[serde(rename_all = "snake_case")]
pub enum MarkdownFlavor {
    Commonmark,
    Gfm,
    Mnote,
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
#[serde(rename_all = "snake_case")]
pub enum MarkdownImportMode {
    ReplaceDocument,
    ReplaceSelection,
    MergeIntoDocument,
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
#[serde(rename_all = "snake_case")]
pub enum MarkdownExportMode {
    FullDocument,
    Selection,
    SingleBlock,
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, Default)]
#[serde(rename_all = "camelCase")]
pub struct MarkdownImportBoundary {
    pub allow_front_matter: bool,
    pub allow_html_blocks: bool,
    pub allow_reference_tokens: bool,
    pub unsupported_blocks_as_paragraph: bool,
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, Default)]
#[serde(rename_all = "camelCase")]
pub struct MarkdownExportBoundary {
    pub preserve_heading_collapse: bool,
    pub preserve_reference_tokens: bool,
    pub allow_lossy_block_fallback: bool,
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
#[serde(rename_all = "camelCase")]
pub struct MarkdownImportOptions {
    pub mode: MarkdownImportMode,
    pub flavor: MarkdownFlavor,
    pub reference_token_strategy: ReferenceTokenStrategy,
    pub boundary: MarkdownImportBoundary,
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
#[serde(rename_all = "camelCase")]
pub struct MarkdownExportOptions {
    pub mode: MarkdownExportMode,
    pub flavor: MarkdownFlavor,
    pub reference_token_strategy: ReferenceTokenStrategy,
    pub boundary: MarkdownExportBoundary,
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
#[serde(rename_all = "camelCase")]
pub struct MarkdownImportRequest {
    pub markdown: String,
    pub options: MarkdownImportOptions,
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
#[serde(rename_all = "camelCase")]
pub struct MarkdownImportResult {
    pub document: EditorBlockDocument,
    #[serde(default)]
    pub warnings: Vec<String>,
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
#[serde(rename_all = "camelCase")]
pub struct MarkdownExportRequest {
    pub document: EditorBlockDocument,
    pub options: MarkdownExportOptions,
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
#[serde(rename_all = "camelCase")]
pub struct MarkdownExportResult {
    pub markdown: String,
    #[serde(default)]
    pub warnings: Vec<String>,
}
