//! Types related to share links.

use std::sync::Arc;

use chrono::{DateTime, Utc};
use derive_more::Display;
use paperless_api_macros::{Item, UpdateDto};
use serde::{Deserialize, Serialize};

use crate::{id::DocumentId, share_link::ShareLinkFileVersion};

/// A share link bundle
#[derive(Debug, Clone, Deserialize, Serialize, UpdateDto, Item)]
#[api_info(endpoint = "share_link_bundles")]
pub struct ShareLinkBundle {
    /// Unique identifier of the share link bundle.
    #[dto(skip)]
    pub id: crate::id::ShareLinkBundleId,

    pub documents: Vec<DocumentId>,

    /// File version of the share link.
    pub file_version: ShareLinkFileVersion,

    #[dto(skip)]
    pub slug: String,

    #[dto(skip)]
    pub created: DateTime<Utc>,

    #[dto(skip)]
    pub built_at: DateTime<Utc>,

    #[dto(skip)]
    pub expiration: DateTime<Utc>,

    #[dto(skip)]
    pub size_bytes: u32,

    #[dto(skip)]
    pub document_count: u32,

    #[serde(skip)]
    pub(crate) base_url: Arc<str>,
}

/// The status of a share link bundle.
#[derive(Debug, Display, Clone, Copy, PartialEq, Eq, Hash, Deserialize, Serialize)]
#[serde(rename_all = "lowercase")]
pub enum ShareLinkBundleStatus {
    Pending,
    Processing,
    Ready,
    Failed,
}

impl ShareLinkBundle {
    #[must_use]
    pub fn url(&self) -> String {
        format!("{}/share/{}", self.base_url, self.slug)
    }
}
