using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Text.Json.Nodes;
using System.Threading.Tasks;

namespace HorseRacingPredictor.Football.Database
{
    internal class BetType : HorseRacingPredictor.Football.Manager.Database
    {
        public void Upsert(SqlConnection connection, JsonNode betType)
        {
            try
            {
                var id = betType["id"]?.GetValue<int>();
                if (id == null) return; // Salta il record se l'id è null

                var query = @"
                                IF EXISTS (SELECT 1 FROM BetType WHERE bet_type_id = @bet_type_id)
                                BEGIN
                                    UPDATE BetType SET name = @name WHERE bet_type_id = @bet_type_id
                                END
                                ELSE
                                BEGIN
                                    INSERT INTO BetType (bet_type_id, name) VALUES (@bet_type_id, @name)
                                END";
                using (var command = new SqlCommand(query, connection))
                {
                    command.Parameters.AddWithValue("@bet_type_id", id);
                    command.Parameters.AddWithValue("@name", betType["name"]?.GetValue<string>() ?? "");
                    command.ExecuteNonQuery();
                }
            }
            catch (Exception ex)
            {
                LogError("l'upsert del tipo di scommessa", ex);
            }
        }
    }
}
