//! `SeaORM` Entity, @generated by sea-orm-codegen 2.0

use sea_orm::entity::prelude::*;
use serde::Serialize;

/// Who may read a repository. Pushing is always the owner's alone, so this
/// only decides who may fetch it, over SSH or over the web.
#[derive(Clone, Copy, Debug, PartialEq, Eq, EnumIter, DeriveActiveEnum, Serialize)]
#[sea_orm(rs_type = "String", db_type = "String(StringLen::N(16))")]
pub enum Visibility {
    /// Anyone with a key, and anyone at all once the web serves repositories.
    #[sea_orm(string_value = "public")]
    Public,
    /// The owner, and nobody else.
    #[sea_orm(string_value = "private")]
    Private,
}

#[sea_orm::model]
#[derive(Clone, Debug, PartialEq, Eq, DeriveEntityModel, Serialize)]
#[sea_orm(table_name = "repositories")]
pub struct Model {
    #[sea_orm(primary_key)]
    pub id: i64,
    #[sea_orm(unique)]
    pub uuid: Uuid,
    pub owner_id: i64,
    pub slug: String,
    pub description: Option<String>,
    // Hand-tuned after codegen: the column is a string in SQLite, but the
    // `Visibility` enum is what the rest of the server reasons about.
    pub visibility: Visibility,
    pub default_branch: String,
    /// Where the number of this repository's next ticket comes from: one
    /// more than the last ticket that was opened, whether or not that ticket
    /// still exists. Counted here so a number is never handed out twice.
    pub next_ticket_number: i64,
    /// Where the number of this repository's next patch proposal comes from.
    /// One more than the last PP that was opened, whether or not that PP
    /// still exists.
    pub next_patch_number: i64,
    /// The default merge policy for patch proposals in this repository.
    /// Each PP snapshots this at creation time.
    pub default_merge_policy: MergePolicy,
    pub created_at: DateTimeWithTimeZone,
    pub deleted_at: Option<DateTimeWithTimeZone>,
    #[sea_orm(
        belongs_to,
        from = "owner_id",
        to = "id",
        on_update = "NoAction",
        on_delete = "Cascade"
    )]
    pub users: BelongsTo<super::users::Entity>,
}

/// Merge policy for patch proposals
#[derive(Clone, Copy, Debug, PartialEq, Eq, EnumIter, DeriveActiveEnum, Serialize)]
#[sea_orm(rs_type = "String", db_type = "String(StringLen::N(32))")]
pub enum MergePolicy {
    /// Prefer fast-forward, fall back to merge commit
    #[sea_orm(string_value = "prefer-fast-forward")]
    PreferFastForward,
    /// Only allow fast-forward merges
    #[sea_orm(string_value = "fast-forward-only")]
    FastForwardOnly,
    /// Always create a merge commit
    #[sea_orm(string_value = "merge-commit-only")]
    MergeCommitOnly,
    /// Rebase then fast-forward
    #[sea_orm(string_value = "rebase-then-fast-forward")]
    RebaseThenFastForward,
}

impl ActiveModelBehavior for ActiveModel {}
