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

use super::IssueState;

#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Serialize, Deserialize)]
#[sea_orm(table_name = "issue")]
pub struct Model {
    /// UUID for global uniqueness / API exposure.
    #[sea_orm(primary_key)]
    pub id: IssueId,
    /// Project this issue belongs to.
    pub project: ProjectId,
    /// Sequential issue number within the project.  Composite with `project` for uniqueness.
    #[sea_orm(primary_key)]
    pub number: i64,
    pub title: String,
    pub body: Option<String>,
    /// `"open"` or `"closed"`.
    pub state: String,
    pub author: UserId,
    pub milestone: Option<String>,
    pub created_at: DateTimeUtc,
    pub updated_at: DateTimeUtc,
    pub closed_at: Option<DateTimeUtc>,
    pub created_by_ai: bool,
}

impl Model {
    pub fn state_enum(&self) -> Result<IssueState, &'static str> {
        self.state.parse()
    }
}

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

impl ActiveModelBehavior for ActiveModel {}
