opencrates 3.0.1

Enterprise-grade AI-powered Rust development companion with comprehensive automation, monitoring, and deployment capabilities
//! Auth command implementation

use anyhow::Result;
use clap::{Args, Command, Subcommand};

#[derive(Subcommand, Debug)]
pub enum AuthCmd {
    /// Login to `OpenAI` services
    Login,
    /// Logout and clear credentials
    Logout,
    /// Show current auth status
    Status,
}

#[derive(Args, Debug)]
pub struct AuthArgs {
    #[command(subcommand)]
    pub cmd: AuthCmd,
}

impl AuthCmd {
    #[must_use]
    pub fn command() -> Command {
        Command::new("auth")
            .about("Authentication commands")
            .subcommand_required(true)
            .arg_required_else_help(true)
            .subcommand(Command::new("login").about("Log in to OpenCrates"))
            .subcommand(Command::new("logout").about("Log out from OpenCrates"))
            .subcommand(Command::new("status").about("Show current auth status"))
    }

    /// Runs the auth command
    ///
    /// # Errors
    /// Returns an error if the authentication operation fails
    pub async fn run(&self) -> Result<()> {
        match self {
            AuthCmd::Login => {
                println!("🔐 Authenticating with OpenAI...");
                
                // Check if API key exists in environment
                if let Ok(api_key) = std::env::var("OPENAI_API_KEY") {
                    if !api_key.is_empty() && api_key != "your_openai_api_key_here" {
                        println!("✅ OpenAI API key found and appears valid");
                        println!("🔑 Authentication successful!");
                    } else {
                        println!("❌ OpenAI API key is empty or using placeholder value");
                        println!("💡 Please set your OPENAI_API_KEY environment variable");
                    }
                } else {
                    println!("❌ OPENAI_API_KEY not found in environment");
                    println!("💡 Please set your OPENAI_API_KEY environment variable");
                    println!("💡 You can add it to your .env file in the project root");
                }
                Ok(())
            }
            AuthCmd::Logout => {
                println!("🔓 Logging out from OpenCrates...");
                println!("â„šī¸  Note: This removes cached credentials but doesn't modify environment variables");
                println!("✅ Logout completed");
                Ok(())
            }
            AuthCmd::Status => {
                println!("🔍 Checking authentication status...");
                
                if let Ok(api_key) = std::env::var("OPENAI_API_KEY") {
                    if !api_key.is_empty() && api_key != "your_openai_api_key_here" {
                        println!("✅ Authenticated with OpenAI");
                        println!("🔑 API Key: {}...{}", 
                                &api_key[..std::cmp::min(8, api_key.len())],
                                &api_key[std::cmp::max(0, api_key.len() - 4)..]);
                    } else {
                        println!("❌ Not authenticated - API key is empty or placeholder");
                    }
                } else {
                    println!("❌ Not authenticated - OPENAI_API_KEY not set");
                }
                
                // Check for other API keys
                if std::env::var("ANTHROPIC_API_KEY").is_ok() {
                    println!("✅ Anthropic API key also configured");
                }
                
                Ok(())
            }
        }
    }
}