using System.Net;
using System.Text;
using MySql.Data.MySqlClient;

namespace Server
{
    public class Reset
    {
        public static void run(MySqlConnection conn, HttpListenerRequest request, HttpListenerResponse response)
        {
            try
            {
                // open connection
                conn.Open();
                // prepare SQL query
                MySqlCommand cmd = new MySqlCommand();
                cmd.Connection = conn;
                cmd.CommandText = "CALL InitDB";
                // execute query
                cmd.ExecuteNonQuery();
                // set up and send response
                response.StatusCode = (int)HttpStatusCode.OK;
                response.StatusDescription = "Status OK";
            }
            catch (Exception ex)
            {
                response.StatusCode = (int)HttpStatusCode.BadRequest;
                string errorMessage = $"Error: {ex.Message}";
                byte[] buffer = Encoding.UTF8.GetBytes(errorMessage);
                response.ContentType = "text/plain";
                response.ContentLength64 = buffer.Length;
                response.OutputStream.Write(buffer, 0, buffer.Length);
            }
            finally
            {
                conn.Close();
            }
        }
    }
}
