use serde::{Deserialize, Serialize};
use uuid::Uuid;
use chrono::{DateTime, Utc};
#[cfg(feature = "openapi")]
#[cfg(feature = "openapi")]
use utoipa::ToSchema;
#[cfg(feature = "validation")]
use validator::Validate;
use crate::domain::entity::FileComment;
use crate::domain::entity::AuditMetadata;
use crate::domain::entity::CommentStatus;
#[derive(Debug, Clone, Deserialize)]
#[cfg_attr(feature = "openapi", derive(ToSchema))]
#[cfg_attr(feature = "validation", derive(Validate))]
#[serde(rename_all = "camelCase")]
pub struct CreateFileCommentDto {
#[cfg_attr(feature = "openapi", schema(example = "550e8400-e29b-41d4-a716-446655440000"))]
#[serde(alias = "file_id")]
pub file_id: Uuid,
#[cfg_attr(feature = "openapi", schema(example = "550e8400-e29b-41d4-a716-446655440000"))]
#[serde(alias = "user_id")]
pub user_id: Uuid,
#[serde(default, skip_serializing_if = "Option::is_none", alias = "parent_id")]
pub parent_id: Option<Uuid>,
#[cfg_attr(feature = "validation", validate(length(max = 10000)))]
#[cfg_attr(feature = "openapi", schema(example = "example"))]
pub content: String,
#[serde(default, skip_serializing_if = "Option::is_none", alias = "annotation_region")]
pub annotation_region: Option<serde_json::Value>,
pub mentions: Vec<Uuid>,
#[cfg_attr(feature = "openapi", schema(example = true))]
pub resolved: bool,
#[serde(default, skip_serializing_if = "Option::is_none", alias = "resolved_by")]
pub resolved_by: Option<Uuid>,
#[serde(default, skip_serializing_if = "Option::is_none", alias = "resolved_at")]
pub resolved_at: Option<DateTime<Utc>>,
pub status: CommentStatus,
}
#[derive(Debug, Clone, Deserialize)]
#[cfg_attr(feature = "openapi", derive(ToSchema))]
#[cfg_attr(feature = "validation", derive(Validate))]
#[serde(rename_all = "camelCase")]
pub struct UpdateFileCommentDto {
#[cfg_attr(feature = "openapi", schema(example = "550e8400-e29b-41d4-a716-446655440000"))]
#[serde(alias = "file_id")]
pub file_id: Uuid,
#[cfg_attr(feature = "openapi", schema(example = "550e8400-e29b-41d4-a716-446655440000"))]
#[serde(alias = "user_id")]
pub user_id: Uuid,
#[serde(default, skip_serializing_if = "Option::is_none", alias = "parent_id")]
pub parent_id: Option<Uuid>,
#[cfg_attr(feature = "validation", validate(length(max = 10000)))]
#[cfg_attr(feature = "openapi", schema(example = "example"))]
pub content: String,
#[serde(default, skip_serializing_if = "Option::is_none", alias = "annotation_region")]
pub annotation_region: Option<serde_json::Value>,
pub mentions: Vec<Uuid>,
#[cfg_attr(feature = "openapi", schema(example = true))]
pub resolved: bool,
#[serde(default, skip_serializing_if = "Option::is_none", alias = "resolved_by")]
pub resolved_by: Option<Uuid>,
#[serde(default, skip_serializing_if = "Option::is_none", alias = "resolved_at")]
pub resolved_at: Option<DateTime<Utc>>,
pub status: CommentStatus,
}
#[derive(Debug, Clone, Default, Deserialize)]
#[cfg_attr(feature = "openapi", derive(ToSchema))]
#[cfg_attr(feature = "validation", derive(Validate))]
#[serde(rename_all = "camelCase")]
pub struct PatchFileCommentDto {
#[cfg_attr(feature = "openapi", schema(example = "550e8400-e29b-41d4-a716-446655440000"))]
#[serde(skip_serializing_if = "Option::is_none", alias = "file_id")]
pub file_id: Option<Uuid>,
#[cfg_attr(feature = "openapi", schema(example = "550e8400-e29b-41d4-a716-446655440000"))]
#[serde(skip_serializing_if = "Option::is_none", alias = "user_id")]
pub user_id: Option<Uuid>,
#[serde(skip_serializing_if = "Option::is_none", alias = "parent_id")]
pub parent_id: Option<Uuid>,
#[cfg_attr(feature = "validation", validate(length(max = 10000)))]
#[cfg_attr(feature = "openapi", schema(example = "example"))]
#[serde(skip_serializing_if = "Option::is_none")]
pub content: Option<String>,
#[serde(skip_serializing_if = "Option::is_none", alias = "annotation_region")]
pub annotation_region: Option<serde_json::Value>,
#[serde(skip_serializing_if = "Option::is_none")]
pub mentions: Option<Vec<Uuid>>,
#[cfg_attr(feature = "openapi", schema(example = true))]
#[serde(skip_serializing_if = "Option::is_none")]
pub resolved: Option<bool>,
#[serde(skip_serializing_if = "Option::is_none", alias = "resolved_by")]
pub resolved_by: Option<Uuid>,
#[serde(skip_serializing_if = "Option::is_none", alias = "resolved_at")]
pub resolved_at: Option<DateTime<Utc>>,
#[serde(skip_serializing_if = "Option::is_none")]
pub status: Option<CommentStatus>,
}
impl PatchFileCommentDto {
pub fn has_changes(&self) -> bool {
self.file_id.is_some() || self.user_id.is_some() || self.parent_id.is_some() || self.content.is_some() || self.annotation_region.is_some() || self.mentions.is_some() || self.resolved.is_some() || self.resolved_by.is_some() || self.resolved_at.is_some() || self.status.is_some()
}
}
#[derive(Debug, Clone, Serialize)]
#[cfg_attr(feature = "openapi", derive(ToSchema))]
#[serde(rename_all = "camelCase")]
pub struct FileCommentResponseDto {
#[cfg_attr(feature = "openapi", schema(example = "550e8400-e29b-41d4-a716-446655440000"))]
pub id: Uuid,
#[cfg_attr(feature = "openapi", schema(example = "550e8400-e29b-41d4-a716-446655440000"))]
pub file_id: Uuid,
#[cfg_attr(feature = "openapi", schema(example = "550e8400-e29b-41d4-a716-446655440000"))]
pub user_id: Uuid,
pub parent_id: Option<Uuid>,
#[cfg_attr(feature = "openapi", schema(example = "example"))]
pub content: String,
pub annotation_region: Option<serde_json::Value>,
pub mentions: Vec<Uuid>,
#[cfg_attr(feature = "openapi", schema(example = true))]
pub resolved: bool,
pub resolved_by: Option<Uuid>,
pub resolved_at: Option<DateTime<Utc>>,
pub status: CommentStatus,
pub metadata: AuditMetadata,
}
#[derive(Debug, Clone, Serialize)]
#[cfg_attr(feature = "openapi", derive(ToSchema))]
#[serde(rename_all = "camelCase")]
pub struct FileCommentListResponseDto {
pub items: Vec<FileCommentResponseDto>,
pub total: u64,
pub page: u32,
pub per_page: u32,
pub total_pages: u32,
pub has_next: bool,
pub has_prev: bool,
}
impl FileCommentListResponseDto {
pub fn new(items: Vec<FileCommentResponseDto>, total: u64, page: u32, per_page: u32) -> Self {
let total_pages = if per_page > 0 {
((total as f64) / (per_page as f64)).ceil() as u32
} else {
0
};
Self {
items,
total,
page,
per_page,
total_pages,
has_next: page < total_pages,
has_prev: page > 1,
}
}
}
#[derive(Debug, Clone, Serialize)]
#[cfg_attr(feature = "openapi", derive(ToSchema))]
#[serde(rename_all = "camelCase")]
pub struct FileCommentSummaryDto {
pub id: Uuid,
pub file_id: Uuid,
pub user_id: Uuid,
pub parent_id: Option<Uuid>,
pub created_at: Option<DateTime<Utc>>,
}
impl From<FileComment> for FileCommentResponseDto {
fn from(entity: FileComment) -> Self {
Self {
id: entity.id,
file_id: entity.file_id,
user_id: entity.user_id,
parent_id: entity.parent_id,
content: entity.content,
annotation_region: entity.annotation_region,
mentions: entity.mentions,
resolved: entity.resolved,
resolved_by: entity.resolved_by,
resolved_at: entity.resolved_at,
status: entity.status,
metadata: entity.metadata,
}
}
}
impl From<FileComment> for FileCommentSummaryDto {
fn from(entity: FileComment) -> Self {
let created_at = backbone_core::PersistentEntity::created_at(&entity);
Self {
id: entity.id,
file_id: entity.file_id,
user_id: entity.user_id,
parent_id: entity.parent_id,
created_at,
}
}
}
impl From<CreateFileCommentDto> for FileComment {
fn from(dto: CreateFileCommentDto) -> Self {
Self {
id: Uuid::new_v4(),
file_id: dto.file_id,
user_id: dto.user_id,
parent_id: dto.parent_id,
content: dto.content,
annotation_region: dto.annotation_region,
mentions: dto.mentions,
resolved: dto.resolved,
resolved_by: dto.resolved_by,
resolved_at: dto.resolved_at,
status: dto.status,
metadata: AuditMetadata::default(),
}
}
}
impl From<&FileComment> for FileCommentResponseDto {
fn from(entity: &FileComment) -> Self {
Self {
id: entity.id.clone(),
file_id: entity.file_id.clone(),
user_id: entity.user_id.clone(),
parent_id: entity.parent_id.clone(),
content: entity.content.clone(),
annotation_region: entity.annotation_region.clone(),
mentions: entity.mentions.clone(),
resolved: entity.resolved.clone(),
resolved_by: entity.resolved_by.clone(),
resolved_at: entity.resolved_at.clone(),
status: entity.status.clone(),
metadata: entity.metadata.clone(),
}
}
}
impl backbone_core::FromCreateDto<CreateFileCommentDto> for FileComment {
fn from_create_dto(dto: CreateFileCommentDto) -> backbone_core::ServiceResult<Self> {
Ok(FileComment::from(dto))
}
}
impl backbone_core::ApplyUpdateDto<UpdateFileCommentDto> for FileComment {
fn apply_update(mut self, dto: UpdateFileCommentDto) -> backbone_core::ServiceResult<Self> {
self.file_id = dto.file_id;
self.user_id = dto.user_id;
self.parent_id = dto.parent_id;
self.content = dto.content;
self.annotation_region = dto.annotation_region;
self.mentions = dto.mentions;
self.resolved = dto.resolved;
self.resolved_by = dto.resolved_by;
self.resolved_at = dto.resolved_at;
self.status = dto.status;
Ok(self)
}
}