//! GraphQL-style API types and documentation
//!
//! Defines types and documentation for GraphQL-equivalent REST endpoints.
//! Implemented as REST endpoints rather than full GraphQL to avoid dependency conflicts.

use serde::{Deserialize, Serialize};

/// GraphQL ID type (alias for String)
#[allow(dead_code)]
pub type ID = String;

/// Project type for GraphQL
#[allow(dead_code)]
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Project {
    /// Project ID
    pub id: ID,
    /// Project name
    pub name: String,
    /// Project version
    pub version: String,
    /// Project description
    pub description: String,
    /// Project type
    pub project_type: String,
    /// Current phase
    pub current_phase: String,
    /// Creation timestamp
    pub created_at: String,
    /// Last update timestamp
    pub updated_at: String,
}

/// Phase type for GraphQL
#[allow(dead_code)]
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Phase {
    /// Phase ID
    pub id: ID,
    /// Project ID
    pub project_id: ID,
    /// Phase name
    pub phase: String,
    /// Duration in days
    pub duration_days: i32,
    /// Start timestamp
    pub started_at: String,
    /// Completion timestamp (optional)
    pub completed_at: Option<String>,
    /// Phase notes
    pub notes: Option<String>,
}

/// Tool type for GraphQL
#[allow(dead_code)]
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Tool {
    /// Tool ID
    pub id: ID,
    /// Tool name
    pub name: String,
    /// Is tool enabled
    pub enabled: bool,
    /// Tool description
    pub description: String,
    /// Applicable phases (comma-separated)
    pub phases: String,
}

/// Checklist item type for GraphQL
#[allow(dead_code)]
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ChecklistItem {
    /// Item ID
    pub id: ID,
    /// Task ID
    pub task_id: String,
    /// Task description
    pub description: String,
    /// Is task completed
    pub completed: bool,
    /// Completion timestamp
    pub completed_at: Option<String>,
    /// Creation timestamp
    pub created_at: String,
}

/// GraphQL Query documentation
///
/// In a full GraphQL implementation, these would be resolver functions.
/// This module documents the equivalent GraphQL query types.
#[allow(dead_code)]
pub mod query_docs {
    /// Get a project by ID
    pub const PROJECT: &str = "query { project(id: \"...\") { id name version ... } }";

    /// List all projects
    pub const PROJECTS: &str = "query { projects { id name version ... } }";

    /// Get phase history for a project
    pub const PHASES: &str = "query { phases(projectId: \"...\") { id phase duration_days ... } }";

    /// Get tools for a project
    pub const TOOLS: &str = "query { tools(projectId: \"...\") { id name enabled ... } }";

    /// Get checklist items for a project
    pub const CHECKLIST: &str =
        "query { checklist(projectId: \"...\") { id description completed ... } }";
}

/// GraphQL Mutation documentation
///
/// In a full GraphQL implementation, these would be resolver functions.
/// This module documents the equivalent GraphQL mutation types.
#[allow(dead_code)]
pub mod mutation_docs {
    /// Create a new project
    pub const CREATE_PROJECT: &str =
        "mutation { createProject(name: \"...\", ...) { id name ... } }";

    /// Transition project to new phase
    pub const TRANSITION_PHASE: &str =
        "mutation { transitionPhase(projectId: \"...\", toPhase: \"...\") { id phase ... } }";

    /// Enable a tool for a project
    pub const ENABLE_TOOL: &str =
        "mutation { enableTool(projectId: \"...\", toolName: \"...\") { id name enabled } }";

    /// Disable a tool for a project
    pub const DISABLE_TOOL: &str =
        "mutation { disableTool(projectId: \"...\", toolName: \"...\") { id name enabled } }";

    /// Add checklist item
    pub const ADD_CHECKLIST_ITEM: &str =
        "mutation { addChecklistItem(projectId: \"...\", taskId: \"...\", description: \"...\") { id description ... } }";

    /// Update checklist item
    pub const UPDATE_CHECKLIST_ITEM: &str =
        "mutation { updateChecklistItem(itemId: \"...\", completed: true) { id completed ... } }";
}

/// REST API equivalent endpoints mapping
/// This serves as documentation for GraphQL-equivalent REST endpoints:
///
/// Queries:
/// - GET /api/projects/:id -> query { project(id: "...") }
/// - GET /api/projects -> query { projects }
/// - GET /api/projects/:id/phases/history -> query { phases(projectId: "...") }
/// - GET /api/projects/:id/tools -> query { tools(projectId: "...") }
/// - GET /api/projects/:id/checklists -> query { checklist(projectId: "...") }
///
/// Mutations:
/// - POST /api/projects -> mutation { createProject(...) }
/// - POST /api/projects/:id/phases/transition -> mutation { transitionPhase(...) }
/// - POST /api/projects/:id/tools/:name/enable -> mutation { enableTool(...) }
/// - POST /api/projects/:id/tools/:name/disable -> mutation { disableTool(...) }
/// - POST /api/projects/:id/checklists -> mutation { addChecklistItem(...) }
/// - PUT /api/projects/:id/checklists/items/:item_id -> mutation { updateChecklistItem(...) }
///
/// Subscriptions (via WebSocket /ws):
/// - project_events: PhaseTransition, ToolEnabled, ToolDisabled, ProjectCreated, ProjectDeleted
#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_project_type() {
        let project = Project {
            id: ID::from("proj-1"),
            name: "Test Project".to_string(),
            version: "1.0.0".to_string(),
            description: "Test project description".to_string(),
            project_type: "MultiLang".to_string(),
            current_phase: "Creation".to_string(),
            created_at: "2024-01-01T00:00:00Z".to_string(),
            updated_at: "2024-01-01T00:00:00Z".to_string(),
        };

        assert_eq!(project.name, "Test Project");
        assert_eq!(project.current_phase, "Creation");
        assert_eq!(project.project_type, "MultiLang");
    }

    #[test]
    fn test_phase_type() {
        let phase = Phase {
            id: ID::from("phase-1"),
            project_id: ID::from("proj-1"),
            phase: "Development".to_string(),
            duration_days: 30,
            started_at: "2024-01-01T00:00:00Z".to_string(),
            completed_at: None,
            notes: Some("In progress".to_string()),
        };

        assert_eq!(phase.phase, "Development");
        assert_eq!(phase.duration_days, 30);
        assert!(phase.completed_at.is_none());
    }

    #[test]
    fn test_tool_type() {
        let tool = Tool {
            id: ID::from("tool-1"),
            name: "tracking".to_string(),
            enabled: true,
            description: "Tracking tool".to_string(),
            phases: "Creation,Development".to_string(),
        };

        assert_eq!(tool.name, "tracking");
        assert!(tool.enabled);
    }

    #[test]
    fn test_checklist_item_type() {
        let item = ChecklistItem {
            id: ID::from("item-1"),
            task_id: "task-1".to_string(),
            description: "Setup project".to_string(),
            completed: false,
            completed_at: None,
            created_at: "2024-01-01T00:00:00Z".to_string(),
        };

        assert_eq!(item.description, "Setup project");
        assert!(!item.completed);
    }

    #[test]
    fn test_query_docs_exist() {
        // Query documentation constants are compiled into the module
        // and are accessible for API documentation purposes
        let _ = query_docs::PROJECT;
        let _ = query_docs::PROJECTS;
        let _ = query_docs::PHASES;
        let _ = query_docs::TOOLS;
        let _ = query_docs::CHECKLIST;
    }

    #[test]
    fn test_mutation_docs_exist() {
        // Mutation documentation constants are compiled into the module
        // and are accessible for API documentation purposes
        let _ = mutation_docs::CREATE_PROJECT;
        let _ = mutation_docs::TRANSITION_PHASE;
        let _ = mutation_docs::ENABLE_TOOL;
        let _ = mutation_docs::DISABLE_TOOL;
        let _ = mutation_docs::ADD_CHECKLIST_ITEM;
        let _ = mutation_docs::UPDATE_CHECKLIST_ITEM;
    }
}
