use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use uuid::Uuid;

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Event {
    pub id: Uuid,
    #[serde(rename = "type")]
    pub event_type: String,
    pub project_id: Uuid,
    pub source_id: Uuid,
    pub timestamp: DateTime<Utc>,
    pub body: Option<String>,
    pub created_at: DateTime<Utc>,
    // Log fields
    pub severity_number: Option<i16>,
    pub severity_text: Option<String>,
    pub observed_timestamp: Option<DateTime<Utc>>,
    pub attributes: Option<serde_json::Value>,
    pub resource_attributes: Option<serde_json::Value>,
    pub scope_name: Option<String>,
    pub scope_version: Option<String>,
    pub trace_id: Option<String>,
    pub span_id: Option<String>,
    pub flags: Option<i32>,
    // Message fields
    pub platform: Option<String>,
    pub external_id: Option<String>,
    pub from_id: Option<String>,
    pub from_name: Option<String>,
    pub to_id: Option<String>,
    pub to_name: Option<String>,
    pub channel_id: Option<String>,
    pub channel_name: Option<String>,
    pub subject: Option<String>,
    pub body_html: Option<String>,
    pub message_type: Option<String>,
    pub attachments: Option<serde_json::Value>,
    pub raw_payload: Option<serde_json::Value>,
    pub platform_timestamp: Option<DateTime<Utc>>,
    pub webhook_delivered: bool,
    pub webhook_delivered_at: Option<DateTime<Utc>>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct EventListResponse {
    pub events: Vec<Event>,
    pub next_cursor: Option<String>,
    pub has_more: bool,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct EventResponse {
    pub event: Event,
}

#[derive(Debug, Clone, Default)]
pub struct EventQuery {
    /// Filter by event type: "log" or "message"
    pub event_type: Option<String>,
    /// Filter by platform (for messages)
    pub platform: Option<String>,
    /// Filter by minimum severity number (for logs)
    pub severity: Option<i16>,
    /// Filter by service name from resource_attributes (for logs)
    pub service: Option<String>,
    /// Filter by sender ID (for messages)
    pub from: Option<String>,
    /// Full-text search across body, severity_text, from_name, subject
    pub search: Option<String>,
    /// Events after this time
    pub since: Option<DateTime<Utc>>,
    /// Events before this time
    pub until: Option<DateTime<Utc>>,
    /// Max results (1-100)
    pub limit: Option<i64>,
    /// Pagination cursor
    pub cursor: Option<String>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PlatformStat {
    pub platform: String,
    pub count: i64,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct StatsResponse {
    pub today: i64,
    pub total: i64,
    pub by_platform: Vec<PlatformStat>,
}

#[derive(Debug, Clone, Default, Serialize)]
pub struct LogEntry {
    pub severity_text: Option<String>,
    pub body: String,
    pub attributes: Option<serde_json::Value>,
    // Message fields — mapped into OTLP attributes for Pipestreamr
    pub platform: Option<String>,
    pub from_id: Option<String>,
    pub from_name: Option<String>,
    pub to_id: Option<String>,
    pub to_name: Option<String>,
    pub channel_id: Option<String>,
    pub channel_name: Option<String>,
    pub subject: Option<String>,
    pub body_html: Option<String>,
    pub message_type: Option<String>,
    pub external_id: Option<String>,
    pub attachments: Option<serde_json::Value>,
}
