use serde::{Deserialize, Serialize};
use sqlx::FromRow;
use uuid::Uuid;

use super::BaseFields;

// ==================== CHAT (AI ASSISTANT) ====================

/// Chat message role
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum ChatRole {
    User,
    Assistant,
    Tool,
}

impl ChatRole {
    pub fn as_str(&self) -> &'static str {
        match self {
            Self::User => "user",
            Self::Assistant => "assistant",
            Self::Tool => "tool",
        }
    }
}

/// Chat conversation with AI assistant
#[derive(Debug, Clone, Serialize, Deserialize, FromRow)]
pub struct ChatConversation {
    #[sqlx(flatten)]
    #[serde(flatten)]
    pub base: BaseFields,
    pub team_profile_id: Uuid,
    pub title: String,
    pub is_active: bool,
}

/// Chat message within a conversation
#[derive(Debug, Clone, Serialize, Deserialize, FromRow)]
pub struct ChatMessage {
    #[sqlx(flatten)]
    #[serde(flatten)]
    pub base: BaseFields,
    pub conversation_id: Uuid,
    /// Role: 'user', 'assistant', 'tool'
    pub role: String,
    pub content: Option<String>,
    /// Tool calls made by assistant (JSON array)
    pub tool_calls: serde_json::Value,
    /// Tool results returned (JSON array)
    pub tool_results: serde_json::Value,
}
