use crate::{DateTimeUtc, ProjectId, UserId};
use sea_orm::entity::prelude::*;
use serde::{Deserialize, Serialize};

use super::MemberRole;

#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Serialize, Deserialize)]
#[sea_orm(table_name = "project_members")]
pub struct Model {
    #[sea_orm(primary_key)]
    pub id: i64,
    #[sea_orm(column_name = "project_uuid")]
    pub project: ProjectId,
    #[sea_orm(column_name = "user_uuid")]
    pub user: UserId,
    pub scope: String,
    pub joined_at: DateTimeUtc,
}

impl Model {
    pub fn scope_role(&self) -> Result<MemberRole, &'static str> {
        self.scope.parse()
    }
}

#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
pub enum Relation {}

impl ActiveModelBehavior for ActiveModel {}
