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

use super::CollabRole;

#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Serialize, Deserialize)]
#[sea_orm(table_name = "repo_collaborator")]
pub struct Model {
    #[sea_orm(primary_key)]
    pub repo: RepoId,
    #[sea_orm(primary_key)]
    pub user: UserId,
    pub scope: String,
    pub created_at: DateTimeUtc,
}

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

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

impl ActiveModelBehavior for ActiveModel {}
