use colored::Colorize;

// use comfy_table::{presets::UTF8_FULL, Cell, Color, ContentArrangement, Table};
use crate::client::WorkflowInstanceResponse;

pub fn print_success(message: &str) {
    println!("{} {}", "✓".green().bold(), message.green());
}

#[allow(dead_code)]
pub fn print_error(message: &str) {
    eprintln!("{} {}", "✗".red().bold(), message.red());
}

#[allow(dead_code)]
pub fn print_workflows_table(workflows: &[WorkflowInstanceResponse]) {
    if workflows.is_empty() {
        println!("{}", "No active workflows".yellow());
        return;
    }

    // Print header
    println!(
        "{}",
        format!(
            "{:<10} {:<20} {:<15} {:<10} {:<20}",
            "ID", "Template", "Status", "Progress", "Created"
        )
        .cyan()
        .bold()
    );
    println!("{}", "─".repeat(75).cyan());

    // Print rows
    for workflow in workflows {
        let status_colored = match workflow.status.as_str() {
            s if s.starts_with("running") => workflow.status.green(),
            s if s.starts_with("waiting") => workflow.status.yellow(),
            s if s.starts_with("completed") => workflow.status.blue(),
            s if s.starts_with("failed") => workflow.status.red(),
            _ => workflow.status.normal(),
        };

        let progress = format!("{}/{}", workflow.current_stage + 1, workflow.total_stages);

        println!(
            "{:<10} {:<20} {:<15} {:<10} {:<20}",
            &workflow.id[..8.min(workflow.id.len())],
            &workflow.template_name,
            status_colored,
            progress,
            &workflow.created_at[..19.min(workflow.created_at.len())],
        );
    }

    println!("{}", "─".repeat(75).cyan());
}

pub fn print_workflow_details(workflow: &WorkflowInstanceResponse) {
    println!("\n{}", "Workflow Details".cyan().bold());
    println!("{}", "─".repeat(60).cyan());
    println!("{:<15} {}", "ID:".bold(), workflow.id);
    println!("{:<15} {}", "Template:".bold(), workflow.template_name);

    let status_colored = match workflow.status.as_str() {
        s if s.starts_with("running") => workflow.status.green(),
        s if s.starts_with("waiting") => workflow.status.yellow(),
        s if s.starts_with("completed") => workflow.status.blue(),
        s if s.starts_with("failed") => workflow.status.red(),
        _ => workflow.status.normal(),
    };
    println!("{:<15} {}", "Status:".bold(), status_colored);

    println!(
        "{:<15} {}/{}",
        "Progress:".bold(),
        workflow.current_stage + 1,
        workflow.total_stages
    );
    println!("{:<15} {}", "Created:".bold(), workflow.created_at);
    println!("{:<15} {}", "Updated:".bold(), workflow.updated_at);
    println!("{}", "─".repeat(60).cyan());
}

pub fn print_templates(templates: &[String]) {
    if templates.is_empty() {
        println!("{}", "No workflow templates available".yellow());
        return;
    }

    println!("\n{}", "Available Workflow Templates".cyan().bold());
    println!("{}", "─".repeat(60).cyan());

    for (idx, template) in templates.iter().enumerate() {
        println!("{:2}. {}", idx + 1, template.green());
    }

    println!("{}", "─".repeat(60).cyan());
    println!(
        "\nUse {} to start a workflow",
        "vapora workflow start --template <name>".yellow()
    );
}
