using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using System.Text.Json.Serialization;
using Yuna.Website.Server.Model;
using Yuna.Website.Server.Services.DeviceSkillService;

namespace Yuna.Website.Server.API
{
    public class SkillsEndpoints
    {

        public void Define(WebApplication app)
        {
            app.MapPost("/api/skill", CreateSkill)
                .Produces(200)
                .WithTags("skill");

            app.MapDelete("/api/skill/{id:long}", () => { })
                .Produces(200)
                .WithTags("skill");

            app.MapGet("/api/skill/{id:long}", () => { })
                .Produces(200)
                .WithTags("skill");

            app.MapGet("/api/skill", GetAllSkills)
                .Produces(200)
                .WithTags("skill");
        }


        public class CreateSkillRequest
        {
            public string type { get; init; } = null!;

            [JsonPropertyName("measureName")]
            public string? MeasureName { get; init; } = null!;

            [JsonPropertyName("jsonValueName")]
            public string JsonValueName { get; init; } = null!;

            [JsonPropertyName("name")]
            public String Name { get; init; } = null!;

        }

        [Authorize]
        public async Task<IResult> CreateSkill([FromBody] CreateSkillRequest request, IPropService skillService)
        {
            Prop prop = new Prop()
            {
                JsonValueName = request.JsonValueName,
                MeasureName = request.MeasureName ?? "",
                Name = request.Name,
                Type = request.type.Equals("float") ? PropType.Float : PropType.Boolean
            };

            var result = await skillService.Create(prop);
            if (result is null) return Results.BadRequest();


            return Results.Ok(result); 
        }

        [Authorize]
        public async Task<IResult> GetAllSkills(IPropService skillService)
        {
            var result = await skillService.GetList();
            return Results.Ok(result);
        }
    }
}
