use anyhow::Result;
use async_trait::async_trait;
use clap::Subcommand;

// Platform-specific modules
#[cfg(target_os = "linux")]
mod dbus;

#[cfg(target_os = "macos")]
mod xpc;

#[cfg_attr(target_os = "macos", async_trait(?Send))]
#[cfg_attr(not(target_os = "macos"), async_trait)]
pub trait DaemonInterface {
    async fn print_version(&mut self) -> Result<()>;
    async fn print_conversations(&mut self) -> Result<()>;
    async fn sync_conversations(&mut self, conversation_id: Option<String>) -> Result<()>;
    async fn sync_conversations_list(&mut self) -> Result<()>;
    async fn print_messages(
        &mut self,
        conversation_id: String,
        last_message_id: Option<String>,
    ) -> Result<()>;
    async fn enqueue_outgoing_message(
        &mut self,
        conversation_id: String,
        text: String,
    ) -> Result<()>;
    async fn wait_for_signals(&mut self) -> Result<()>;
    async fn config(&mut self, cmd: ConfigCommands) -> Result<()>;
    async fn delete_all_conversations(&mut self) -> Result<()>;
    async fn download_attachment(&mut self, attachment_id: String) -> Result<()>;
    async fn upload_attachment(&mut self, path: String) -> Result<()>;
    async fn mark_conversation_as_read(&mut self, conversation_id: String) -> Result<()>;
}

struct StubDaemonInterface;
impl StubDaemonInterface {
    fn new() -> Result<Self> {
        Ok(Self)
    }
}

#[cfg_attr(target_os = "macos", async_trait(?Send))]
#[cfg_attr(not(target_os = "macos"), async_trait)]
impl DaemonInterface for StubDaemonInterface {
    async fn print_version(&mut self) -> Result<()> {
        Err(anyhow::anyhow!(
            "Daemon interface not implemented on this platform"
        ))
    }
    async fn print_conversations(&mut self) -> Result<()> {
        Err(anyhow::anyhow!(
            "Daemon interface not implemented on this platform"
        ))
    }
    async fn sync_conversations(&mut self, _conversation_id: Option<String>) -> Result<()> {
        Err(anyhow::anyhow!(
            "Daemon interface not implemented on this platform"
        ))
    }
    async fn sync_conversations_list(&mut self) -> Result<()> {
        Err(anyhow::anyhow!(
            "Daemon interface not implemented on this platform"
        ))
    }
    async fn print_messages(
        &mut self,
        _conversation_id: String,
        _last_message_id: Option<String>,
    ) -> Result<()> {
        Err(anyhow::anyhow!(
            "Daemon interface not implemented on this platform"
        ))
    }
    async fn enqueue_outgoing_message(
        &mut self,
        _conversation_id: String,
        _text: String,
    ) -> Result<()> {
        Err(anyhow::anyhow!(
            "Daemon interface not implemented on this platform"
        ))
    }
    async fn wait_for_signals(&mut self) -> Result<()> {
        Err(anyhow::anyhow!(
            "Daemon interface not implemented on this platform"
        ))
    }
    async fn config(&mut self, _cmd: ConfigCommands) -> Result<()> {
        Err(anyhow::anyhow!(
            "Daemon interface not implemented on this platform"
        ))
    }
    async fn delete_all_conversations(&mut self) -> Result<()> {
        Err(anyhow::anyhow!(
            "Daemon interface not implemented on this platform"
        ))
    }
    async fn download_attachment(&mut self, _attachment_id: String) -> Result<()> {
        Err(anyhow::anyhow!(
            "Daemon interface not implemented on this platform"
        ))
    }
    async fn upload_attachment(&mut self, _path: String) -> Result<()> {
        Err(anyhow::anyhow!(
            "Daemon interface not implemented on this platform"
        ))
    }
    async fn mark_conversation_as_read(&mut self, _conversation_id: String) -> Result<()> {
        Err(anyhow::anyhow!(
            "Daemon interface not implemented on this platform"
        ))
    }
}

pub fn new_daemon_interface() -> Result<Box<dyn DaemonInterface>> {
    #[cfg(target_os = "linux")]
    {
        Ok(Box::new(dbus::DBusDaemonInterface::new()?))
    }
    #[cfg(target_os = "macos")]
    {
        Ok(Box::new(xpc::XpcDaemonInterface::new()?))
    }
    #[cfg(not(any(target_os = "linux", target_os = "macos")))]
    {
        Ok(Box::new(StubDaemonInterface::new()?))
    }
}

#[derive(Subcommand)]
pub enum Commands {
    /// Gets all known conversations.
    Conversations,

    /// Runs a full sync operation for a conversation and its messages.
    Sync { conversation_id: Option<String> },

    /// Runs a sync operation for the conversation list.
    SyncList,

    /// Prints the server Kordophone version.
    Version,

    /// Configuration options
    Config {
        #[command(subcommand)]
        command: ConfigCommands,
    },

    /// Waits for signals from the daemon.
    Signals,

    /// Prints the messages for a conversation.
    Messages {
        conversation_id: String,
        last_message_id: Option<String>,
    },

    /// Deletes all conversations.
    DeleteAllConversations,

    /// Enqueues an outgoing message to be sent to a conversation.
    SendMessage {
        conversation_id: String,
        text: String,
    },

    /// Downloads an attachment from the server to the attachment store. Returns the path to the attachment.
    DownloadAttachment { attachment_id: String },

    /// Uploads an attachment to the server, returns upload guid.
    UploadAttachment { path: String },

    /// Marks a conversation as read.
    MarkConversationAsRead { conversation_id: String },
}

#[derive(Subcommand)]
pub enum ConfigCommands {
    /// Prints the current settings.
    Print,

    /// Sets the server URL.
    SetServerUrl { url: String },

    /// Sets the username.
    SetUsername { username: String },
}

impl Commands {
    pub async fn run(cmd: Commands) -> Result<()> {
        let mut client = new_daemon_interface()?;
        match cmd {
            Commands::Version => client.print_version().await,
            Commands::Conversations => client.print_conversations().await,
            Commands::Sync { conversation_id } => client.sync_conversations(conversation_id).await,
            Commands::SyncList => client.sync_conversations_list().await,
            Commands::Config { command } => client.config(command).await,
            Commands::Signals => client.wait_for_signals().await,
            Commands::Messages {
                conversation_id,
                last_message_id,
            } => {
                client
                    .print_messages(conversation_id, last_message_id)
                    .await
            }
            Commands::DeleteAllConversations => client.delete_all_conversations().await,
            Commands::SendMessage {
                conversation_id,
                text,
            } => client.enqueue_outgoing_message(conversation_id, text).await,
            Commands::UploadAttachment { path } => client.upload_attachment(path).await,
            Commands::DownloadAttachment { attachment_id } => {
                client.download_attachment(attachment_id).await
            }
            Commands::MarkConversationAsRead { conversation_id } => {
                client.mark_conversation_as_read(conversation_id).await
            }
        }
    }
}
