use anyhow::Result;

use crate::config::{ProjectScope, load_plane_settings, resolve_plane_session};

/// Prints the current CLI session status: active workspace, default project,
/// and whether authentication is configured. No API calls — reads config only.
/// Respects `.plane.toml` overrides for workspace and project display.
/// Usage: `plane me`
#[derive(clap::Args)]
pub struct SessionStatusArgs;

/// Execution logic for the `plane me` subcommand
impl SessionStatusArgs {
    /// Reads the persisted settings and local overrides, then prints the active workspace,
    /// default project, and authentication status without making any network requests
    pub async fn execute(self) -> Result<()> {
        let plane_settings = load_plane_settings()?;
        let workspace_count = plane_settings.workspaces().workspace_count();

        match resolve_plane_session() {
            Ok(session) => {
                let (scope_label, project_label) = match session.project_scope() {
                    ProjectScope::Scoped(project_slug) => {
                        ("authenticated (project-scoped)", project_slug.to_string())
                    }
                    ProjectScope::ExplicitOnly => {
                        ("authenticated (workspace-scoped)", String::from("(not set)"))
                    }
                };

                println!("Workspace: {}", session.workspace_slug());
                println!("Project:   {}", project_label);
                println!("Workspaces registered: {}", workspace_count);
                println!("Auth:      {}", scope_label);
            }
            Err(resolution_error) => {
                println!("Workspace: (not configured)");
                println!("Project:   (not configured)");
                println!("Workspaces registered: {}", workspace_count);
                println!("Auth:      error: {}", resolution_error);
            }
        }

        Ok(())
    }
}
