using MySql.Data.MySqlClient;
using System;
using System.Collections.Generic;
using System.Data.SQLite;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace AVS
{
    internal class WeightTolerance
    {


        private MySqlConnection connection;

        private string connectionString = "server=utopia-2.c5qech8o9lgg.us-east-1.rds.amazonaws.com;user=itemVerification;password=itemVerification01;database=item_verification_system;";
        public bool InsertOrUpdateWeightTolerance(string FNSKU, string market_place, double weight_tolerance , string avs_name)
        {
            try
            {
                OpenConnection();

                string query = @"INSERT INTO weight_tolerance (FNSKU, MARKET_PLACE, WEIGHT ,WEIGHT_DATETIME,WEIGHT_ADD_BY)
                         VALUES (@fnsku, @market_place, @weight_tolerance , @datetime  ,  @avs_name )
                         ON DUPLICATE KEY UPDATE WEIGHT = VALUES(WEIGHT)";

                using (MySqlCommand cmd = new MySqlCommand(query, connection))
                {
                    cmd.Parameters.AddWithValue("@fnsku", FNSKU);
                    cmd.Parameters.AddWithValue("@market_place", market_place);
                    cmd.Parameters.AddWithValue("@weight_tolerance", weight_tolerance);
                    cmd.Parameters.AddWithValue("@datetime", DateTime.Now);
                    cmd.Parameters.AddWithValue("@avs_name", avs_name);

                    cmd.ExecuteNonQuery();
                }

                return true;
            }
            catch (Exception ex)
            {
                Console.WriteLine($"InsertOrUpdate Error: {ex.Message}");
                return false;
            }
            finally
            {
                CloseConnection();
            }
        }

        public MySqlConnection OpenConnection()
        {
            try
            {
                
               
                // Open the Mysql connection
                connection = new MySqlConnection(connectionString);
                connection.Open();
                //MessageBox.Show("MySQL connection opened successfully", "Connection Status", MessageBoxButtons.OK, MessageBoxIcon.Information);
                Console.WriteLine("Mysql connection opened successfully");
                return connection;
            }
            catch (Exception ex)
            {
              
                Console.WriteLine($"Error opening SQLite connection: {ex.Message}");
               
                return null;
            }
        }

        public void CloseConnection()
        {
            try
            {
                connection = new MySqlConnection(connectionString);
                // Close the Mysql connection
                connection?.Close();
                Console.WriteLine("Mysql connection closed successfully");
            }
            catch (Exception ex)
            {
                Console.WriteLine($"Error closing SQLite connection: {ex.Message}");
            }
        }

        public double GetWeightTolerance(string fnsku, string marketPlace)
        {
            double weightTolerance = 0;

            try
            {
                OpenConnection();

                string query = @"
            SELECT WEIGHT 
            FROM weight_tolerance
            WHERE FNSKU = @fnsku AND MARKET_PLACE = @market_place";

                using (MySqlCommand cmd = new MySqlCommand(query, connection))
                {
                    cmd.Parameters.AddWithValue("@fnsku", fnsku);
                    cmd.Parameters.AddWithValue("@market_place", marketPlace);

                    var result = cmd.ExecuteScalar();

                    if (result != null && result != DBNull.Value)
                    {
                        weightTolerance = Convert.ToDouble(result);
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine($"Error fetching weight tolerance: {ex.Message}");
            }
            finally
            {
                CloseConnection();
            }

            return weightTolerance;
        }


    }
}
