using ClaudeDo.Data.Models;
using ClaudeDo.Ui.ViewModels.Islands;
using Xunit;
using TaskStatus = ClaudeDo.Data.Models.TaskStatus;

namespace ClaudeDo.Worker.Tests.UiVm;

public class TaskRowViewModelPlanningTests
{
    private static TaskRowViewModel MakeRow(
        TaskStatus status,
        string? parentTaskId = null,
        PlanningPhase phase = PlanningPhase.None)
        => new TaskRowViewModel { Id = "t", Status = status, ParentTaskId = parentTaskId, PlanningPhase = phase };

    [Fact]
    public void IdleChild_IsDraft_WhenParentIdIsNotNull()
    {
        var vm = MakeRow(TaskStatus.Idle, parentTaskId: "parent-id");
        Assert.True(vm.IsChild);
        Assert.True(vm.IsDraft);
        Assert.False(vm.IsPlanningParent);
    }

    [Fact]
    public void ActivePlanning_SetsIsPlanningParent()
    {
        var vm = MakeRow(TaskStatus.Idle, phase: PlanningPhase.Active);
        Assert.True(vm.IsPlanningParent);
        Assert.False(vm.IsChild);
        Assert.Equal("PLANNING", vm.PlanningBadge);
    }

    [Fact]
    public void FinalizedPlanning_ShowsPlannedBadge()
    {
        var vm = MakeRow(TaskStatus.Idle, phase: PlanningPhase.Finalized);
        Assert.True(vm.IsPlanningParent);
        Assert.Equal("PLANNED", vm.PlanningBadge);
    }

    [Fact]
    public void PlainIdle_NoBadge()
    {
        var vm = MakeRow(TaskStatus.Idle);
        Assert.False(vm.IsPlanningParent);
        Assert.Null(vm.PlanningBadge);
    }

    [Fact]
    public void DraftChild_CannotSendToQueue()
    {
        var vm = MakeRow(TaskStatus.Idle, parentTaskId: "parent-id");
        vm.ParentFinalized = false;
        Assert.True(vm.IsDraft);
        Assert.False(vm.IsPlanned);
        Assert.False(vm.CanSendToQueue);
    }

    [Fact]
    public void PlannedChild_CanSendToQueue()
    {
        var vm = MakeRow(TaskStatus.Idle, parentTaskId: "parent-id");
        vm.ParentFinalized = true;
        Assert.False(vm.IsDraft);
        Assert.True(vm.IsPlanned);
        Assert.True(vm.CanSendToQueue);
    }

    [Fact]
    public void StandaloneIdle_CanSendToQueue()
    {
        var vm = MakeRow(TaskStatus.Idle);
        Assert.False(vm.IsChild);
        Assert.True(vm.CanSendToQueue);
    }

    [Fact]
    public void FinalizedParentWithChildren_CanQueuePlan()
    {
        var vm = MakeRow(TaskStatus.Idle, phase: PlanningPhase.Finalized);
        vm.HasPlanningChildren = true;
        Assert.True(vm.CanQueuePlan);
    }

    [Fact]
    public void ActiveParentWithChildren_CannotQueuePlan()
    {
        var vm = MakeRow(TaskStatus.Idle, phase: PlanningPhase.Active);
        vm.HasPlanningChildren = true;
        Assert.False(vm.CanQueuePlan);
    }

    [Fact]
    public void FinalizedParentWithoutChildren_CannotQueuePlan()
    {
        var vm = MakeRow(TaskStatus.Idle, phase: PlanningPhase.Finalized);
        Assert.False(vm.CanQueuePlan);
    }
}
