using System;
using ClaudeDo.Data.Models;
using ClaudeDo.Ui.Services;
using ClaudeDo.Ui.ViewModels;
using Xunit;

namespace ClaudeDo.Ui.Tests;

public class IslandsShellViewModelWorkerLogTests
{
    [Fact]
    public void Receiving_event_sets_text_level_and_visible()
    {
        var vm = new IslandsShellViewModel();
        var at = new DateTime(2026, 4, 23, 14, 32, 0, DateTimeKind.Utc);

        vm.OnWorkerLogReceived(new WorkerLogEntry("Created worktree for \"X\"", WorkerLogLevel.Info, at));

        Assert.True(vm.IsWorkerLogVisible);
        Assert.Equal(WorkerLogLevel.Info, vm.WorkerLogLevel);
        Assert.Contains("Created worktree for \"X\"", vm.WorkerLogText);
    }

    [Fact]
    public void Second_event_replaces_first()
    {
        var vm = new IslandsShellViewModel();
        vm.OnWorkerLogReceived(new WorkerLogEntry("first", WorkerLogLevel.Info,    DateTime.UtcNow));
        vm.OnWorkerLogReceived(new WorkerLogEntry("second", WorkerLogLevel.Success, DateTime.UtcNow));

        Assert.Contains("second", vm.WorkerLogText);
        Assert.Equal(WorkerLogLevel.Success, vm.WorkerLogLevel);
    }

    [Fact]
    public void ClearWorkerLog_hides_line()
    {
        var vm = new IslandsShellViewModel();
        vm.OnWorkerLogReceived(new WorkerLogEntry("msg", WorkerLogLevel.Info, DateTime.UtcNow));

        vm.ClearWorkerLog();

        Assert.False(vm.IsWorkerLogVisible);
        Assert.Null(vm.WorkerLogText);
    }

    [Fact]
    public void Text_is_formatted_as_HHmm_dot_message_local_time()
    {
        var vm = new IslandsShellViewModel();
        var utc = new DateTime(2026, 4, 23, 12, 0, 0, DateTimeKind.Utc);
        var expectedLocalHhmm = utc.ToLocalTime().ToString("HH:mm");

        vm.OnWorkerLogReceived(new WorkerLogEntry("hello", WorkerLogLevel.Info, utc));

        Assert.StartsWith(expectedLocalHhmm + " · ", vm.WorkerLogText);
    }
}
