using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;

namespace ClawdDotNet.Desktop.ViewModels;

public sealed record AddServiceResultData(
    string ServiceName,
    string ServiceType,
    int ServicePort,
    string ServiceDescription
);

public sealed partial class AddServiceViewModel : ViewModelBase
{
    [ObservableProperty]
    private string _serviceName = "";

    [ObservableProperty]
    private string _serviceType = "HTTP";

    [ObservableProperty]
    private int _servicePort = 8080;

    [ObservableProperty]
    private string _serviceDescription = "";

    [ObservableProperty]
    private string _statusMessage = "";

    public List<string> AvailableServiceTypes { get; } = ["HTTP", "TCP", "gRPC", "WebSocket", "Custom"];

    public AddServiceResultData? Result { get; private set; }

    public event Action? CloseRequested;

    [RelayCommand]
    private void Confirm()
    {
        if (string.IsNullOrWhiteSpace(ServiceName))
        {
            StatusMessage = "Bitte einen Dienstnamen eingeben.";
            return;
        }

        if (ServicePort <= 0 || ServicePort > 65535)
        {
            StatusMessage = "Gültigen Port zwischen 1 und 65535 eingeben.";
            return;
        }

        Result = new AddServiceResultData(
            ServiceName.Trim(),
            ServiceType,
            ServicePort,
            ServiceDescription.Trim()
        );

        CloseRequested?.Invoke();
    }

    [RelayCommand]
    private void Cancel()
    {
        Result = null;
        CloseRequested?.Invoke();
    }
}
