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

namespace HorseRacingPredictor.Football.Database
{
    internal class H2H : HorseRacingPredictor.Football.Manager.Database
    {
        public void Insert(SqlConnection connection, int predictionId, int h2hFixtureId)
        {
            try
            {
                var query = @"
                                IF NOT EXISTS (SELECT 1 FROM H2H WHERE prediction_id = @prediction_id AND fixture_id = @fixture_id)
                                BEGIN
                                    INSERT INTO H2H (prediction_id, fixture_id)
                                    VALUES (@prediction_id, @fixture_id)
                                END";

                using (var command = new SqlCommand(query, connection))
                {
                    command.Parameters.AddWithValue("@prediction_id", predictionId);
                    command.Parameters.AddWithValue("@fixture_id", h2hFixtureId);
                    command.ExecuteNonQuery();
                }
            }
            catch (Exception ex)
            {
                LogError("l'inserimento della relazione H2H", ex);
            }
        }

        public void DeleteForPrediction(SqlConnection connection, int predictionId)
        {
            try
            {
                var query = "DELETE FROM H2H WHERE prediction_id = @prediction_id";
                using (var command = new SqlCommand(query, connection))
                {
                    command.Parameters.AddWithValue("@prediction_id", predictionId);
                    command.ExecuteNonQuery();
                }
            }
            catch (Exception ex)
            {
                LogError("l'eliminazione delle relazioni H2H", ex);
            }
        }
    }
}
