package dev.peerat.backend.repository;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

public enum DatabaseQuery {
	
	//TRIGGER
	FIRST_TRY("CREATE OR REPLACE TRIGGER FirstTry\r\n"
			+ "AFTER INSERT\r\n"
			+ "   ON completions FOR EACH ROW\r\n"
			+ "BEGIN\r\n"
			+ "	DECLARE badge INT;\r\n"
			+ "    DECLARE contain INT;\r\n"
			+ "    \r\n"
			+ "    SELECT id_badge\r\n"
			+ "    INTO @badge\r\n"
			+ "    FROM badges\r\n"
			+ "    WHERE name = 'FirstTry';\r\n"
			+ "    \r\n"
			+ "    IF @badge is not null THEN\r\n"
			+ "        \r\n"
			+ "        SELECT count(*)\r\n"
			+ "        INTO @contain\r\n"
			+ "        FROM containsBadges\r\n"
			+ "        WHERE fk_badge = @badge AND fk_player = NEW.fk_player;\r\n"
			+ "        \r\n"
			+ "        IF (@contain = 0) THEN\r\n"
			+ "            INSERT INTO containsBadges(fk_player, fk_badge) VALUES (NEW.fk_player, @badge);\r\n"
			+ "        END IF;\r\n"
			+ "    END IF;\r\n"
			+ "END;"),
	EventParticipation("CREATE OR REPLACE TRIGGER EventParticipation\r\n"
			+ "AFTER INSERT\r\n"
			+ "   ON completions FOR EACH ROW\r\n"
			+ "BEGIN\r\n"
			+ "	DECLARE badge INT;\r\n"
			+ "	DECLARE endDate datetime;\r\n"
			+ "    DECLARE player INT;\r\n"
			+ "    DECLARE contain INT;\r\n"
			+ "\r\n"
			+ "    SELECT id_badge\r\n"
			+ "    INTO badge\r\n"
			+ "    FROM badges\r\n"
			+ "    WHERE name = 'EventParticipation';\r\n"
			+ "\r\n"
			+ "    IF @badge is not null THEN\r\n"
			+ "\r\n"
			+ "        SELECT c.end_date, i.fk_player\r\n"
			+ "        INTO endDate, player\r\n"
			+ "        FROM inserted i\r\n"
			+ "        JOIN puzzles p ON i.fk_puzzle = p.id_puzzle\r\n"
			+ "        JOIN chapters c on p.fk_chapter = c.id_chapter;\r\n"
			+ "\r\n"
			+ "        IF @endDate is not null THEN\r\n"
			+ "\r\n"
			+ "            SELECT count(*)\r\n"
			+ "            INTO contain\r\n"
			+ "            FROM containsBadges\r\n"
			+ "            WHERE fk_badge = badge AND fk_player = player;\r\n"
			+ "\r\n"
			+ "            IF (@contain = 0) THEN\r\n"
			+ "                INSERT INTO containsBadges(fk_player, fk_badge) VALUES (@player, @badge);\r\n"
			+ "            END IF;\r\n"
			+ "        END IF;\r\n"
			+ "    END IF;\r\n"
			+ "END;");
	
	private String request;

	DatabaseQuery(DatabaseQuery parent, String request) {
		this.request = parent.request + request;
	}

	DatabaseQuery(String request) {
		this.request = request;
	}

	public PreparedStatement prepare(Connection con) throws SQLException {
		return con.prepareStatement(this.request);
	}

	@Override
	public String toString() {
		return this.request;
	}
}


/*
 * 
 
 SELECT p.*, scores.score, scores.completions, scores.tries, scores.rank, g.*
 FROM players p,
 	  (SELECT fk_player, SUM(c.score) AS score,
 	  COUNT(c.id_completion) AS completions,
 	  SUM(c.tries) AS tries,
 	  rank() over(ORDER BY score DESC) AS rank
 	  FROM completions c GROUP BY c.fk_player)
 	  AS scores
 	  LEFT OUTER JOIN containsGroups cg ON scores.fk_player = cg.fk_player
 	  LEFT OUTER JOIN groups g ON cg.fk_group = g.id_group
 	  WHERE p.id_player = scores.fk_player AND "),
 
 
 * 
 */

/*			TRIGGER PLACE
 * 

CREATE OR REPLACE TRIGGER FirstTry
AFTER INSERT
   ON completions FOR EACH ROW
BEGIN
	DECLARE badge INT;
    DECLARE contain INT;

    SELECT id_badge
    INTO @badge
    FROM badges
    WHERE name = 'FirstTry';

    IF @badge is not null THEN

        SELECT count(*)
        INTO @contain
        FROM containsBadges
        WHERE fk_badge = @badge AND fk_player = NEW.fk_player;

        IF (@contain = 0) THEN

            IF (NEW.score >= 0 AND NEW.tries < 2) THEN
                INSERT INTO containsBadges(fk_player, fk_badge) VALUES (NEW.fk_player, @badge);
            END IF;
        END IF;
    END IF;
END;



CREATE OR REPLACE TRIGGER EventParticipation
AFTER INSERT
   ON completions FOR EACH ROW
BEGIN
	DECLARE badge INT;
	DECLARE endDate datetime;
    DECLARE player INT;
    DECLARE contain INT;

    SELECT id_badge
    INTO badge
    FROM badges
    WHERE name = 'EventParticipation';

    IF @badge is not null THEN

        SELECT c.end_date, i.fk_player
        INTO endDate, player
        FROM inserted i
        JOIN puzzles p ON i.fk_puzzle = p.id_puzzle
        JOIN chapters c on p.fk_chapter = c.id_chapter;

        IF @endDate is not null THEN

            SELECT count(*)
            INTO contain
            FROM containsBadges
            WHERE fk_badge = badge AND fk_player = player;

            IF (@contain = 0) THEN
                INSERT INTO containsBadges(fk_player, fk_badge) VALUES (@player, @badge);
            END IF;
        END IF;
    END IF;
END;


 * 
 */