using TakeoutSaaS.Application.Storage.Enums;

namespace TakeoutSaaS.Application.Storage.Extensions;

/// <summary>
/// 上传类型解析与辅助方法。
/// </summary>
public static class UploadFileTypeParser
{
    /// <summary>
    /// 将字符串解析为上传类型。
    /// </summary>
    public static bool TryParse(string? value, out UploadFileType type)
    {
        type = UploadFileType.Other;
        if (string.IsNullOrWhiteSpace(value))
        {
            return false;
        }

        var normalized = value.Trim().ToLowerInvariant();
        type = normalized switch
        {
            "dish_image" => UploadFileType.DishImage,
            "merchant_logo" => UploadFileType.MerchantLogo,
            "user_avatar" => UploadFileType.UserAvatar,
            "review_image" => UploadFileType.ReviewImage,
            _ => UploadFileType.Other
        };

        return type != UploadFileType.Other || normalized == "other";
    }

    /// <summary>
    /// 将上传类型转换为路径片段。
    /// </summary>
    public static string ToFolderName(this UploadFileType type) => type switch
    {
        UploadFileType.DishImage => "dishes",
        UploadFileType.MerchantLogo => "merchants",
        UploadFileType.UserAvatar => "users",
        UploadFileType.ReviewImage => "reviews",
        _ => "files"
    };
}
