use crate::domain::{ConfidenceTier, TargetTool};
use serde::{Deserialize, Serialize};
use ts_rs::TS;

#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq, TS)]
#[serde(rename_all = "snake_case")]
#[ts(export, export_to = "../frontend/src/lib/types/generated/")]
pub enum WakeupProfile {
    Developer,
    Project,
}

#[derive(Debug, Clone, Serialize, TS)]
#[ts(export, export_to = "../frontend/src/lib/types/generated/")]
pub struct WakeupQuery {
    pub task: String,
    pub cwd: String,
    pub files: Vec<String>,
}

#[derive(Debug, Clone, Serialize, Default, TS)]
#[ts(export, export_to = "../frontend/src/lib/types/generated/")]
pub struct WakeupIdentity {
    pub project_id: Option<String>,
    pub project_name: Option<String>,
    pub repo_paths: Vec<String>,
    pub modules: Vec<String>,
    pub scenes: Vec<String>,
    pub active_profile: String,
    pub developer_roots: Vec<String>,
}

#[derive(Debug, Clone, Serialize, Default, TS)]
#[ts(export, export_to = "../frontend/src/lib/types/generated/")]
pub struct WakeupSection {
    pub items: Vec<WakeupMemoryItem>,
}

#[derive(Debug, Clone, Serialize, TS)]
#[ts(export, export_to = "../frontend/src/lib/types/generated/")]
pub struct WakeupMemoryItem {
    pub title: String,
    pub summary: String,
    pub memory_type: Option<String>,
    pub source: String,
    pub sensitivity: Option<String>,
    pub source_of_truth: bool,
    pub confidence: ConfidenceTier,
}

#[derive(Debug, Clone, Serialize, TS)]
#[ts(export, export_to = "../frontend/src/lib/types/generated/")]
pub struct WakeupRecommendedNote {
    pub path: String,
    pub title: String,
    pub memory_type: Option<String>,
    pub why_relevant: String,
    pub score: i32,
    pub confidence: ConfidenceTier,
}

#[derive(Debug, Clone, Serialize, TS)]
#[ts(export, export_to = "../frontend/src/lib/types/generated/")]
pub struct WakeupProvenanceSource {
    pub path: String,
    pub source_of_truth: bool,
    pub memory_type: Option<String>,
}

#[derive(Debug, Clone, Serialize, Default, TS)]
#[ts(export, export_to = "../frontend/src/lib/types/generated/")]
pub struct WakeupProvenance {
    pub derived_from: Vec<WakeupProvenanceSource>,
    pub selection_basis: Vec<String>,
}

#[derive(Debug, Clone, Serialize, TS)]
#[ts(export, export_to = "../frontend/src/lib/types/generated/")]
pub struct WakeupPolicy {
    pub max_sensitivity_included: Option<String>,
    pub redactions_applied: bool,
    pub suppressed_note_count: usize,
    pub policy_mode: String,
}

#[derive(Debug, Clone, Serialize, TS)]
#[ts(export, export_to = "../frontend/src/lib/types/generated/")]
pub struct WakeupPacket {
    pub version: String,
    pub generated_at: String,
    pub target: TargetTool,
    pub profile: WakeupProfile,
    pub query: WakeupQuery,
    pub identity: WakeupIdentity,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub knowledge_index: Option<String>,
    pub working_style: WakeupSection,
    pub active_context: WakeupSection,
    pub priorities: Vec<String>,
    pub constraints: Vec<WakeupMemoryItem>,
    pub decisions: Vec<WakeupMemoryItem>,
    pub incidents: Vec<WakeupMemoryItem>,
    pub recommended_notes: Vec<WakeupRecommendedNote>,
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub maintenance_hints: Vec<String>,
    pub provenance: WakeupProvenance,
    pub policy: WakeupPolicy,
}
