// Demonstration of archivist types serialization
// Run with: cargo run --package dirigent_archivist --example demo_types

use chrono::Utc;
use dirigent_archivist::*;
use uuid::Uuid;

fn main() {
    println!("=== ARCHIVIST TYPES DEMONSTRATION ===\n");

    // Demo 1: SessionMetadata (matches session.json format)
    println!("1. SessionMetadata (session.json):");
    let session_metadata = SessionMetadata {
        version: 1,
        scroll_id: Uuid::now_v7(),
        created_at: Utc::now(),
        updated_at: Utc::now(),
        title: Some("Example Session".to_string()),
        connector_uid: Uuid::now_v7(),
        native_session_id: Some("abc123".to_string()),
        agent_id: Some("claude-3-5".to_string()),
        parent_scroll_id: None,
        continuation: Some(Continuation::Split),
        tags: vec!["example".to_string(), "test".to_string()],
        metadata: serde_json::json!({
            "source": "OpenCode",
            "project": "dirigent"
        }),
        no_update: false,
        kind: SessionKind::Chat,
        acp_client_id: None,
        is_connected: None,
        current_session_id: None,
        models: None,
        modes: None,
        config_options: None,
        completeness: SessionCompleteness::default(),
        matrix_room_id: None,
        matrix_sharing_active: false,
        matrix_shared_at: None,
        is_subagent: false,
        subagent_type: None,
        spawning_tool_use_id: None,
    };
    println!(
        "{}\n",
        serde_json::to_string_pretty(&session_metadata).unwrap()
    );

    // Demo 2: MessageRecord (matches messages.ndjson format)
    println!("2. MessageRecord (messages.ndjson line):");
    let message = MessageRecord {
        version: 1,
        message_id: Uuid::now_v7(),
        session: session_metadata.scroll_id,
        parent_id: None,
        ts: Utc::now(),
        role: "user".to_string(),
        author: Some("alice".to_string()),
        content_md: "How do I implement archivist types?".to_string(),
        content_parts: None,
        attachments: vec![AttachmentRef {
            file_id: "sha256:abc123".to_string(),
            name: "spec.pdf".to_string(),
            mime_type: Some("application/pdf".to_string()),
        }],
        metadata: serde_json::json!({
            "connector_msg_id": "msg-456"
        }),
    };
    // NDJSON format (one line)
    println!("{}\n", serde_json::to_string(&message).unwrap());

    // Demo 3: ConnectorRecord (matches connector.json format)
    println!("3. ConnectorRecord (connector.json):");
    let connector = ConnectorRecord {
        version: 1,
        connector_uid: session_metadata.connector_uid,
        r#type: "OpenCode".to_string(),
        title: "OpenCode Local".to_string(),
        client_native_id: "opencode@http://localhost:12225".to_string(),
        alias_of: None,
        created_at: Utc::now(),
        metadata: serde_json::json!({}),
        fingerprint: None,
    };
    println!("{}\n", serde_json::to_string_pretty(&connector).unwrap());

    // Demo 4: SessionMapping (matches sessions.ndjson format)
    println!("4. SessionMapping (sessions.ndjson line):");
    let mapping = SessionMapping {
        version: 1,
        connector_uid: connector.connector_uid,
        native_session_id: "abc123".to_string(),
        scroll_id: session_metadata.scroll_id,
        created_at: Utc::now(),
        alias_of: None,
    };
    println!("{}\n", serde_json::to_string(&mapping).unwrap());

    // Demo 5: FileRecord (matches file_index.jsonl format)
    println!("5. FileRecord (file_index.jsonl line):");
    let file_record = FileRecord {
        version: 1,
        file_id: "sha256:abc123def456".to_string(),
        path: ".files/ab/cd/abc123def456".to_string(),
        size: 123456,
        mime: Some("application/pdf".to_string()),
        original_name: "spec.pdf".to_string(),
        sessions: vec![session_metadata.scroll_id],
        metadata: serde_json::json!({
            "source": "upload"
        }),
    };
    println!("{}\n", serde_json::to_string(&file_record).unwrap());

    // Demo 6: Enum serialization
    println!("6. Enum Serialization:");
    println!(
        "  Continuation::Split: {}",
        serde_json::to_string(&Continuation::Split).unwrap()
    );
    println!(
        "  Continuation::Compact: {}",
        serde_json::to_string(&Continuation::Compact).unwrap()
    );
    println!(
        "  RegisterStatus::Accepted: {}",
        serde_json::to_string(&RegisterStatus::Accepted).unwrap()
    );
    println!(
        "  RegisterStatus::Aliased: {}",
        serde_json::to_string(&RegisterStatus::Aliased).unwrap()
    );
    println!();

    // Demo 7: API types
    println!("7. RegisterConnectorResponse:");
    let response = RegisterConnectorResponse {
        status: RegisterStatus::Accepted,
        connector_uid: Uuid::now_v7(),
        alias_of: None,
        note: Some("Successfully registered".to_string()),
    };
    println!("{}\n", serde_json::to_string_pretty(&response).unwrap());

    println!("8. RegisterSessionResponse:");
    let response = RegisterSessionResponse {
        status: RegisterStatus::Aliased,
        scroll_id: Uuid::now_v7(),
        alias_of: Some(Uuid::now_v7()),
    };
    println!("{}\n", serde_json::to_string_pretty(&response).unwrap());

    println!("=== ALL TYPES MATCH VISION.MD SPECIFICATION ===");
}
