//! `ph login`: Authenticate with Product Hunt.
//!
//! Paste your Developer Token from the PH dashboard for read access.

use ph_cli_core::{Client, Result};
use tracing::info;

/// Execute the login command.
pub async fn execute(client: &mut Client) -> Result<()> {
    println!("=== Product Hunt Authentication ===\n");
    println!("Get your Developer Token from:");
    println!("  https://www.producthunt.com/v2/oauth/applications\n");
    println!("Steps:");
    println!("  1. Create a new app (or use existing one)");
    println!("  2. Copy the Developer Token");
    println!("  3. Paste it below\n");

    println!("Enter your Developer Token:");

    let mut token = String::new();
    std::io::stdin().read_line(&mut token)?;
    let token = token.trim();

    if token.is_empty() {
        return Err(ph_cli_core::Error::GraphQLError(
            "No token provided. Login cancelled.".to_string(),
        ));
    }

    client.set_developer_token(token)?;

    println!("\nToken saved successfully!");
    println!("You can now use ph-cli to browse Product Hunt.");

    info!("Developer token configured");
    Ok(())
}

/// Execute logout.
pub async fn logout(client: &mut Client) -> Result<()> {
    client.logout()?;
    println!("Logged out successfully. Token cleared.");
    Ok(())
}
