using CommunityToolkit.Mvvm.ComponentModel;
using MarcusLaw.OutlookAddin.Core.Models;

namespace MarcusLaw.OutlookAddin.UI.ViewModels
{
    public sealed partial class AttachmentRowViewModel : ObservableObject
    {
        public EspoAttachment Data { get; }

        [ObservableProperty]
        private bool isSelected = true;

        public string FileName
        {
            get => Data.Name;
            set
            {
                var newName = value ?? string.Empty;
                if (Data.Name == newName) return;
                Data.Name = newName;
                OnPropertyChanged();
            }
        }

        public string SizeDisplay { get; }

        public AttachmentRowViewModel(EspoAttachment data)
        {
            Data = data;
            SizeDisplay = ComputeSize(data.ContentBase64);
        }

        private static string ComputeSize(string base64)
        {
            if (string.IsNullOrEmpty(base64)) return string.Empty;
            // Base64 expands binary 4-for-3, ignoring padding chars.
            var padding = 0;
            if (base64.Length > 0 && base64[base64.Length - 1] == '=') padding++;
            if (base64.Length > 1 && base64[base64.Length - 2] == '=') padding++;
            var bytes = (long)((base64.Length * 3) / 4) - padding;
            return FormatBytes(bytes);
        }

        private static string FormatBytes(long bytes)
        {
            if (bytes < 1024) return bytes + " B";
            if (bytes < 1024 * 1024) return (bytes / 1024.0).ToString("F1") + " KB";
            return (bytes / (1024.0 * 1024.0)).ToString("F1") + " MB";
        }
    }
}
