using System.Net;
using System.Text;
using MySql.Data.MySqlClient;
namespace Server
{
    public class CreateProcedure
    {
        public static void run(MySqlConnection conn, HttpListenerRequest request, HttpListenerResponse response)
        {
            try
            {
                conn.Open();
                MySqlCommand cmd = new MySqlCommand();
                cmd.Connection = conn;
                cmd.CommandText = @"
CREATE PROCEDURE fill_timelog ()
BEGIN
  DECLARE j INT DEFAULT 1;
  DECLARE users INT DEFAULT 1;
  DECLARE logs INT;
  DECLARE hours FLOAT;
  DECLARE project INT;
  DECLARE curDate DATE DEFAULT '2024-11-18';
  DECLARE h2 INT;

  WHILE users <= 100 DO
    SET logs = FLOOR(1 + (RAND() * 20));
    SET j = 1;
    WHILE j <= logs DO
      SET project = FLOOR(1 + (RAND() * 3));
      SET curDate = DATE_ADD('2020-01-01', INTERVAL FLOOR(RAND() * DATEDIFF('2020-02-01', '2020-01-01')) DAY);
      SET hours = (RAND() * (8 - 0.25)) + 0.25;

      SELECT SUM(time) INTO h2
      FROM Timelog
      WHERE date = curdate && user = users;

      WHILE(h2 + hours) > 8 DO
        SET curDate = DATE_ADD('2020-01-01', INTERVAL FLOOR(RAND() * DATEDIFF('2020-02-01', '2020-01-01')) DAY);

        SELECT SUM(time)INTO h2
        FROM Timelog
        WHERE date = curdate && user = users;

      END WHILE;
      INSERT INTO Timelog(user, project, date, time) VALUES(users, project, curDate, hours);
      SET j = j + 1;
    END WHILE;
    SET users = users + 1;
  END WHILE;
END;";
                cmd.ExecuteNonQuery();
                cmd.CommandText = @"CREATE PROCEDURE InitDB()
BEGIN
  DECLARE i INT DEFAULT 1;
  TRUNCATE TABLE Timelog;
  TRUNCATE TABLE Project;
  SET foreign_key_checks = 0;
  TRUNCATE TABLE User;
  SET foreign_key_checks = 1;

  INSERT INTO Project(name) VALUES('My own'),('Outcons'),('Free Time');

  CREATE TEMPORARY TABLE temp_fname (fname VARCHAR(255));
  INSERT INTO temp_fname (fname) VALUES 
    ( 'John' ),
    ( 'Gringo' ),
    ( 'Mark' ),
    ( 'Lisa' ),
    ( 'Maria' ),
    ( 'Sonya' ),
    ( 'Philip' ),
    ( 'Jose' ),
    ( 'Lorenzo' ),
    ( 'George' ),
    ( 'Justin' );

  CREATE TEMPORARY TABLE temp_lname (lname VARCHAR(255));
  INSERT INTO temp_lname (lname) VALUES 
    ( 'Johnson' ),
    ( 'Lamas' ),
    ( 'Jackson' ),
    ( 'Brown' ),
    ( 'Mason' ),
    ( 'Rodriguez' ),
    ( 'Roberts' ),
    ( 'Thomas' ),
    ( 'Rose' ),
    ( 'McDonalds' );

  CREATE TEMPORARY TABLE temp_mail (mail VARCHAR(255));
  INSERT INTO temp_mail (mail) VALUES 
    ( 'hotmail.com' ),
    ( 'gmail.com' ),
    ( 'live.com' );
    
  WHILE i <= 100 DO
    INSERT INTO User (f_name, l_name, mail)
    SELECT 
        (SELECT fname FROM temp_fname ORDER BY RAND() LIMIT 1),
        (SELECT lname FROM temp_lname ORDER BY RAND() LIMIT 1),
        (SELECT mail FROM temp_mail ORDER BY RAND() LIMIT 1);
    SET i = i + 1;
  END WHILE;

  UPDATE User
  SET User.mail = CONCAT(User.f_name,'.', User.l_name,'@', User.mail);

  CALL fill_timelog();
  DROP TABLE temp_mail;
  DROP TABLE temp_fname;
  DROP TABLE temp_lname;
  END;";
                cmd.ExecuteNonQuery();
                // prepare response
                response.StatusCode = (int)HttpStatusCode.OK;
                response.StatusDescription = "Status OK";
            }
            catch (Exception ex)
            {
                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
            {
                // close db connection
                conn.Close();
            }
        }
    }
}
