package e2e_test

import (
	"testing"
)

func TestDueDate(t *testing.T) {
	env := newTestEnv(t, "testuser")

	t.Run("SetOnCreate", func(t *testing.T) {
		out := env.mustAx("add", "Issue with due date", "--due", "2099-12-31", "--json")
		n := env.parseNode(out)
		if n.DueDate != "2099-12-31" {
			t.Errorf("due_date: want 2099-12-31, got %q", n.DueDate)
		}
	})

	t.Run("PersistsOnShow", func(t *testing.T) {
		out := env.mustAx("add", "Issue for show test", "--due", "2050-06-15", "--json")
		id := env.parseNode(out).ID

		showOut := env.mustAx("show", id, "--json")
		n := env.parseNode(showOut)
		if n.DueDate != "2050-06-15" {
			t.Errorf("due_date after show: want 2050-06-15, got %q", n.DueDate)
		}
	})

	t.Run("Update", func(t *testing.T) {
		out := env.mustAx("add", "Issue to update due", "--due", "2099-01-01", "--json")
		id := env.parseNode(out).ID

		updOut := env.mustAx("update", id, "--due", "2099-06-30", "--json")
		n := env.parseNode(updOut)
		if n.DueDate != "2099-06-30" {
			t.Errorf("due_date after update: want 2099-06-30, got %q", n.DueDate)
		}
	})

	t.Run("ClearWithFlag", func(t *testing.T) {
		out := env.mustAx("add", "Issue to clear due", "--due", "2099-01-01", "--json")
		id := env.parseNode(out).ID

		updOut := env.mustAx("update", id, "--clear-due", "--json")
		n := env.parseNode(updOut)
		if n.DueDate != "" {
			t.Errorf("due_date after --clear-due: want empty, got %q", n.DueDate)
		}
	})

	t.Run("NotSetByDefault", func(t *testing.T) {
		out := env.mustAx("add", "Issue without due date", "--json")
		n := env.parseNode(out)
		if n.DueDate != "" {
			t.Errorf("due_date should be empty when not set, got %q", n.DueDate)
		}
	})

	t.Run("PersistsAcrossOtherUpdates", func(t *testing.T) {
		out := env.mustAx("add", "Issue with due and title", "--due", "2077-07-07", "--json")
		id := env.parseNode(out).ID

		// Updating title should not change due date.
		updOut := env.mustAx("update", id, "--title", "New title", "--json")
		n := env.parseNode(updOut)
		if n.DueDate != "2077-07-07" {
			t.Errorf("due_date should be preserved after title update, got %q", n.DueDate)
		}
	})

	t.Run("InListOutput", func(t *testing.T) {
		out := env.mustAx("add", "Listed issue with due", "--due", "2088-03-01", "--json")
		id := env.parseNode(out).ID

		listOut := env.mustAx("list", "--json")
		nodes := env.parseNodes(listOut)
		n, ok := env.findInList(nodes, id)
		if !ok {
			t.Fatalf("node %s not found in list output", id)
		}
		if n.DueDate != "2088-03-01" {
			t.Errorf("due_date in list: want 2088-03-01, got %q", n.DueDate)
		}
	})
}
