using Microsoft.AspNetCore.Mvc.ViewFeatures;
using Microsoft.AspNetCore.Razor.TagHelpers;

namespace WorkFlowCheck.Web.Helpers
{
    [HtmlTargetElement("input", Attributes = "asp-for-datatype")]
    public class AspForDataTypeTagHelper : TagHelper
    {
        [HtmlAttributeName("asp-for-datatype")]
        public ModelExpression For { get; set; }

        public override void Process(TagHelperContext context, TagHelperOutput output)
        {
            if (For == null) return;

            var type = For.ModelExplorer.ModelType;

            string dataType = GetDataTypeString(type);
            output.Attributes.SetAttribute("data-type", dataType);

            // 🔥 EZ HIÁNYZIK → Rakd be!
            output.Attributes.RemoveAll("asp-for-datatype");
        }


        private string GetDataTypeString(Type type)
        {
            // Nullable<T> esetén nyerjük ki az alaptípust
            if (Nullable.GetUnderlyingType(type) is Type underlyingType)
            {
                type = underlyingType;
            }

            if (type == typeof(string)) return "string";
            if (type == typeof(int) || type == typeof(long) || type == typeof(float) ||
                type == typeof(double) || type == typeof(decimal)) return "number";
            if (type == typeof(bool)) return "bool";
            if (type == typeof(DateTime)) return "date";

            return "string"; // default fallback
        }

    }
}
