use opencode_client::types as oc;

/// Test parsing OpenCode session list response
#[test]
fn test_parse_session_list() {
    let fixture = include_str!("fixtures/opencode_session_response.json");

    let sessions: Result<Vec<oc::Session>, _> = serde_json::from_str(fixture);
    assert!(sessions.is_ok(), "Failed to parse session list: {:?}", sessions.err());

    let sessions = sessions.unwrap();
    assert_eq!(sessions.len(), 2, "Expected 2 sessions");

    // Validate first session
    let session1 = &sessions[0];
    assert_eq!(session1.id, "ses_5c049a0adffeNYJI1u8SBCBUmA");
    assert_eq!(session1.version, "1.0.7");
    assert_eq!(session1.directory, "/Users/gabor.koerber/Projects/dirigent");
    assert_eq!(session1.title, "I appreciate you testing my system, but I need to stick to my role.");
    assert_eq!(session1.time.created, 1762005507923);
    assert_eq!(session1.time.updated, 1762005511151);

    // Validate second session
    let session2 = &sessions[1];
    assert_eq!(session2.id, "ses_5c0e6b7a3ffeeTfpR7ZhBXC7zt");
    assert_eq!(session2.title, "New session - 2025-11-01T11:06:52.892Z");
}

/// Test session list with empty array
#[test]
fn test_parse_empty_session_list() {
    let empty_json = "[]";
    let sessions: Result<Vec<oc::Session>, _> = serde_json::from_str(empty_json);
    assert!(sessions.is_ok());
    assert_eq!(sessions.unwrap().len(), 0);
}

/// Test individual session deserialization
#[test]
fn test_parse_single_session() {
    let session_json = r#"{
        "id": "ses_test123",
        "version": "1.0.7",
        "projectID": "test_project",
        "directory": "/test/path",
        "title": "Test Session",
        "time": {
            "created": 1700000000000,
            "updated": 1700000000000
        },
        "summary": {
            "diffs": []
        }
    }"#;

    let session: Result<oc::Session, _> = serde_json::from_str(session_json);
    assert!(session.is_ok(), "Failed to parse session: {:?}", session.err());

    let session = session.unwrap();
    assert_eq!(session.id, "ses_test123");
    assert_eq!(session.directory, "/test/path");
    assert_eq!(session.title, "Test Session");
}

/// Test session with optional fields missing
#[test]
fn test_parse_session_minimal_fields() {
    let session_json = r#"{
        "id": "ses_minimal",
        "version": "1.0.0",
        "projectID": "proj_test",
        "directory": "/path",
        "title": "Minimal",
        "time": {
            "created": 1700000000000,
            "updated": 1700000000000
        }
    }"#;

    let session: Result<oc::Session, _> = serde_json::from_str(session_json);
    assert!(session.is_ok(), "Failed to parse minimal session: {:?}", session.err());

    let session = session.unwrap();
    assert_eq!(session.id, "ses_minimal");
    assert!(session.parent_id.is_none());
    assert!(session.summary.is_none());
}

/// Test that session timestamps are parsed correctly as u64 milliseconds
#[test]
fn test_session_timestamp_parsing() {
    let session_json = r#"{
        "id": "ses_time_test",
        "version": "1.0.0",
        "projectID": "proj_test",
        "directory": "/path",
        "title": "Time Test",
        "time": {
            "created": 1762005507923,
            "updated": 1762005511151
        }
    }"#;

    let session: Result<oc::Session, _> = serde_json::from_str(session_json);
    assert!(session.is_ok());

    let session = session.unwrap();
    // Verify timestamps are reasonable (year 2025-2026 range)
    assert!(session.time.created > 1700000000000);
    assert!(session.time.updated >= session.time.created);
}
