#![allow(dead_code)]
#![allow(unused_imports)]
use mongodb::{
    bson::{doc, oid::ObjectId, DateTime},
    Collection,
};
use serde::{Deserialize, Serialize};

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Family {
    #[serde(rename = "_id", skip_serializing_if = "Option::is_none")]
    pub id: Option<ObjectId>,
    #[serde(rename = "familyId")]
    pub family_id: String,
    #[serde(rename = "name")]
    pub name: String,
    #[serde(rename = "nameIv")]
    pub name_iv: String,
    #[serde(rename = "nameAuthTag")]
    pub name_auth_tag: String,
    #[serde(rename = "memberIds")]
    pub member_ids: Vec<String>,
    #[serde(rename = "createdAt")]
    pub created_at: DateTime,
    #[serde(rename = "updatedAt")]
    pub updated_at: DateTime,
}

pub struct FamilyRepository {
    collection: Collection<Family>,
}

impl FamilyRepository {
    pub fn new(collection: Collection<Family>) -> Self {
        Self { collection }
    }

    pub async fn create(&self, family: &Family) -> mongodb::error::Result<()> {
        self.collection.insert_one(family, None).await?;
        Ok(())
    }

    pub async fn find_by_family_id(
        &self,
        family_id: &str,
    ) -> mongodb::error::Result<Option<Family>> {
        self.collection
            .find_one(doc! { "familyId": family_id }, None)
            .await
    }
}
