use anyhow::{anyhow, Result};
use std::path::Path;

use crate::db;
use crate::editor;
use crate::ops;

pub struct Opts<'a> {
    pub title: Option<&'a str>,
    pub priority: db::Priority,
    pub effort: db::Effort,
    pub task_type: &'a str,
    pub desc: Option<&'a str>,
    pub parent: Option<&'a str>,
    pub labels: Option<&'a str>,
    pub json: bool,
}

/// Template shown in the editor when the user runs `td create` without a title.
const TEMPLATE: &str = "
TD: Please provide the task title on the first line, and an optional
TD: description below. Lines starting with 'TD: ' will be ignored.
TD: An empty message aborts.";

pub fn run(root: &Path, opts: Opts) -> Result<()> {
    // If neither title nor description were supplied, try to open an editor.
    // We treat the presence of TD_FORCE_EDITOR as an explicit interactive
    // signal (used by tests); otherwise we check whether stdin is a tty.
    let (title_owned, desc_owned);
    let (title, desc) = if opts.title.is_none() && opts.desc.is_none() {
        let interactive = std::env::var("TD_FORCE_EDITOR").is_ok()
            || std::io::IsTerminal::is_terminal(&std::io::stdin());
        if interactive {
            let (t, d) = editor::open(TEMPLATE)?;
            title_owned = t;
            desc_owned = d;
            (title_owned.as_str(), desc_owned.as_str())
        } else {
            return Err(anyhow!(
                "title required; provide it as a positional argument or run interactively to open an editor"
            ));
        }
    } else {
        (
            opts.title.ok_or_else(|| anyhow!("title required"))?,
            opts.desc.unwrap_or(""),
        )
    };

    let store = db::open(root)?;

    let parent = if let Some(raw) = opts.parent {
        Some(db::resolve_task_id(&store, raw, false)?)
    } else {
        None
    };

    let labels = opts
        .labels
        .map(|s| {
            s.split(',')
                .map(str::trim)
                .filter(|l| !l.is_empty())
                .map(String::from)
                .collect()
        })
        .unwrap_or_default();

    let task = ops::create_task(
        &store,
        ops::CreateOpts {
            title: title.to_owned(),
            description: desc.to_owned(),
            task_type: opts.task_type.to_owned(),
            priority: opts.priority,
            effort: opts.effort,
            parent,
            labels,
        },
    )?;

    if opts.json {
        println!("{}", serde_json::to_string(&task)?);
    } else {
        let c = crate::color::stdout_theme();
        println!("{}created{} {}: {}", c.green, c.reset, task.id, task.title);
    }

    Ok(())
}
