using CommunityToolkit.Mvvm.ComponentModel;
using Sonex.DBWrapper;
using System;
using System.Threading.Tasks;

namespace Sonex.Client.Models;

public partial class PermissionsModel : ObservableObject
{
    [ObservableProperty]
    private int id;

    [ObservableProperty]
    private string name = string.Empty;

    [ObservableProperty]
    private string description = string.Empty;
    public string DisplayName
    {
        get
        {
            var text = name.Replace("permission_", "").Replace('_', ' ').Trim();
            return string.IsNullOrEmpty(text)
                ? string.Empty
                : char.ToUpper(text[0]) + text.Substring(1);
        }
    }

    partial void OnNameChanged(string value) => OnPropertyChanged(nameof(DisplayName));

    public static Task<DB.SingleResult<PermissionsModel>> Get(string name)
    {
        return DB.QuerySingleAsync<PermissionsModel>("SELECT * FROM sonex.v_permissions WHERE name = @permission_name LIMIT 1;",
            new { permission_name = name });
    }

    public static Task<DB.Result<PermissionsModel>> GetAll()
    {
        return DB.QueryListAsync<PermissionsModel>("SELECT * FROM sonex.v_permissions;");
    }
}
