// SPDX-License-Identifier: GPL-3.0-or-later

//! CLI integration tests for schedule subcommands.
//!
//! Note: These tests use the real config dir (~/.config/temperlux/).
//! Tests that depend on empty state are skipped if profiles exist.

use assert_cmd::Command;
use predicates::prelude::PredicateBooleanExt;

fn temperlux() -> Command {
    Command::cargo_bin("temperlux").unwrap()
}

#[test]
fn schedule_add_requires_name() {
    temperlux()
        .args(["schedule", "add"])
        .assert()
        .failure()
        .stderr(predicates::str::contains("required").and(predicates::str::contains("name")));
}

#[test]
fn schedule_add_requires_trigger() {
    temperlux()
        .args(["schedule", "add", "--name", "Test", "--monitors", "ddc:1=50"])
        .assert()
        .failure()
        .stderr(predicates::str::contains("trigger is required"));
}

#[test]
fn schedule_add_invalid_time() {
    temperlux()
        .args(["schedule", "add", "--name", "Test", "--time", "25:00"])
        .assert()
        .failure()
        .stderr(predicates::str::contains("Invalid time"));
}

#[test]
fn schedule_delete_nonexistent() {
    temperlux()
        .args(["schedule", "delete", "nonexistent-id-12345"])
        .assert()
        .failure()
        .stderr(predicates::str::contains("not found"));
}

#[test]
fn schedule_help() {
    temperlux()
        .args(["schedule", "--help"])
        .assert()
        .success()
        .stdout(predicates::str::contains("Manage schedule profiles"));
}

#[test]
fn schedule_add_help() {
    temperlux()
        .args(["schedule", "add", "--help"])
        .assert()
        .success()
        .stdout(predicates::str::contains("--name"))
        .stdout(predicates::str::contains("--time"))
        .stdout(predicates::str::contains("--after-sunrise"))
        .stdout(predicates::str::contains("--after-sunset"));
}

#[test]
fn schedule_list_runs() {
    // Just verify it doesn't crash — output depends on real config
    temperlux()
        .args(["schedule", "list"])
        .assert()
        .success();
}

#[test]
fn schedule_list_json_valid() {
    // JSON output should parse as valid JSON array
    temperlux()
        .args(["schedule", "list", "--json"])
        .assert()
        .success()
        .stdout(predicates::str::starts_with("["));
}

#[test]
fn schedule_show_times_runs() {
    // Just verify it doesn't crash — output depends on whether profiles exist
    temperlux()
        .args(["schedule", "show-times"])
        .assert()
        .success()
        .stdout(predicates::str::contains("Current time")
            .or(predicates::str::contains("No schedule profiles")));
}

#[test]
fn schedule_apply_runs() {
    // Just verify it doesn't crash
    temperlux()
        .args(["schedule", "apply"])
        .assert()
        .success();
}
