using ClaudeDo.Ui.Services;
using CommunityToolkit.Mvvm.ComponentModel;

namespace ClaudeDo.Ui.ViewModels.Modals.Settings;

public sealed partial class PrimeScheduleRowViewModel : ViewModelBase
{
    public Guid Id { get; }
    public bool IsExisting { get; }

    [ObservableProperty] private bool _enabled;
    [ObservableProperty] private DateOnly _startDate;
    [ObservableProperty] private DateOnly _endDate;
    [ObservableProperty] private TimeSpan _timeOfDay;
    [ObservableProperty] private bool _workdaysOnly;
    [ObservableProperty] private DateTimeOffset? _lastRunAt;

    public string LastRunLabel => LastRunAt is { } v ? v.LocalDateTime.ToString("g") : "—";

    partial void OnLastRunAtChanged(DateTimeOffset? value) => OnPropertyChanged(nameof(LastRunLabel));

    public PrimeScheduleRowViewModel(PrimeScheduleDto dto, bool isExisting)
    {
        Id = dto.Id;
        IsExisting = isExisting;
        Enabled = dto.Enabled;
        StartDate = dto.StartDate;
        EndDate = dto.EndDate;
        TimeOfDay = dto.TimeOfDay;
        WorkdaysOnly = dto.WorkdaysOnly;
        LastRunAt = dto.LastRunAt;
    }

    public PrimeScheduleDto ToDto() =>
        new(Id, StartDate, EndDate, TimeOfDay, WorkdaysOnly, Enabled, LastRunAt, null);
}
