-- ============================================================================
-- Those are the requests to update the database.
-- ============================================================================

-- Verifying functions.

CREATE OR REPLACE FUNCTION is_existing_piece(
    toadd_id_piece INT
) RETURNS VOID AS $$
BEGIN

    IF NOT EXISTS(SELECT 1 FROM etre WHERE id_piece = toadd_id_piece) THEN
        RAISE NOTICE 'The % piece does not exist.', toadd_id_piece;
        RETURN;
    END IF;

END; $$
LANGUAGE plpgsql;

CREATE OR REPLACE FUNCTION is_existing_marque(
    toadd_id_marque INT
) RETURNS VOID AS $$
BEGIN
    
    IF NOT EXISTS(
        SELECT 1 FROM marques
        WHERE id_marque = toadd_id_marque
        )
        THEN
        RAISE NOTICE 'The % marque does not exist.', toadd_id_marque;
        RETURN;
    END IF;

END; $$
LANGUAGE plpgsql;

CREATE OR REPLACE FUNCTION is_existing_note(
    toadd_id_modele INT,
    toadd_id_membre INT
) RETURNS VOID AS $$
BEGIN

    IF NOT EXISTS(
        SELECT 1 FROM noter
        WHERE     id_modele = toadd_id_modele 
            AND   id_membre = toadd_id_membre
        )
        THEN
        RAISE NOTICE 'The notes does not exist.';
        RETURN;
    END IF;

END; $$
LANGUAGE plpgsql;

CREATE OR REPLACE FUNCTION is_existing_membre(
    toadd_id_membre INT
) RETURNS VOID AS $$
BEGIN

    IF NOT EXISTS(SELECT 1 FROM membres WHERE id_membre = toadd_id_membre) THEN
        RAISE NOTICE 'The % membre does not exist.', toadd_id_membre;
        RETURN;
    END IF;

END; $$
LANGUAGE plpgsql;

-- To add/remove a piece.

CREATE OR REPLACE FUNCTION add_piece(
    toadd_id_piece      INT,
    toadd_id_couleur    INT,
    toadd_id_forme      INT,
    toadd_id_marque     INT
) RETURNS VOID AS $$
BEGIN
    -- If the piece is already in the database, do nothing.
    IF EXISTS(SELECT 1 FROM etre WHERE id_piece = toadd_id_piece) THEN
        RAISE NOTICE 'The % piece is already in the db.', toadd_id_piece;
        RETURN;
    END IF;

    INSERT INTO pieces      VALUES (toadd_id_piece);
    INSERT INTO etre        VALUES (toadd_id_piece, toadd_id_marque);
    INSERT INTO colorer     VALUES (toadd_id_piece, toadd_id_couleur);
    INSERT INTO etre_forme  VALUES (toadd_id_forme, toadd_id_piece);

END; $$
LANGUAGE plpgsql;

CREATE OR REPLACE FUNCTION remove_piece(
    torem_id_piece INT
) RETURNS VOID AS $$
BEGIN
    -- If the piece does not exist.
    SELECT is_existing_piece(torem_id_piece);

    -- If the piece is complex.
    IF EXISTS(SELECT 1 FROM etre_complexe WHERE id_piece = torem_id_piece) THEN
        DELETE FROM piece_complexe  WHERE id_piece_complexe IN (
            SELECT id_piece_complexe 
            FROM etre_complexe 
            WHERE id_piece = torem_id_piece
        );

        DELETE FROM etre_complexe   WHERE id_piece = torem_id_piece;
    END IF;

    DELETE FROM colorer         WHERE id_piece = torem_id_piece;
    DELETE FROM avoir_motif     WHERE id_piece = torem_id_piece;
    DELETE FROM etre_forme      WHERE id_piece = torem_id_piece;
    DELETE FROM necessiter      WHERE id_piece = torem_id_piece;
    DELETE FROM acheter         WHERE id_piece = torem_id_piece;
    DELETE FROM perdre          WHERE id_piece = torem_id_piece;
    DELETE FROM contenir        WHERE id_piece = torem_id_piece;
    DELETE FROM etre            WHERE id_piece = torem_id_piece;
    DELETE FROM pieces          WHERE id_piece = torem_id_piece;

    RAISE NOTICE '% piece sucessfully removed.', torem_id_piece;

END; $$
LANGUAGE plpgsql;

-- To add a complex piece.

CREATE OR REPLACE FUNCTION add_piece_comp(
    toadd_id_piece      INT,
    toadd_id_piece_comp INT,
    toadd_id_couleur    INT,
    toadd_id_forme      INT,
    toadd_id_marque     INT
) RETURNS VOID AS $$
BEGIN
    -- If the piece is already complex.
    IF EXISTS(
        SELECT 1 FROM etre_complexe 
        WHERE id_piece_complexe = toadd_id_piece_comp
        ) THEN
        RAISE NOTICE 'The % piece is already in the db.', toadd_id_piece;
        RETURN;
    END IF;

    -- If the piece is already in the database, do nothing.
    IF EXISTS(SELECT 1 FROM etre WHERE id_piece = toadd_id_piece) THEN
        RAISE NOTICE 'The % piece is already in the db.', toadd_id_piece;
        RETURN;
    END IF;

    INSERT INTO pieces          VALUES (toadd_id_piece);
    INSERT INTO piece_complexes VALUES (toadd_id_piece_comp);
    INSERT INTO etre            VALUES (toadd_id_piece, toadd_id_marque);
    INSERT INTO etre_complexe   VALUES (toadd_id_piece, toadd_id_piece_comp);
    INSERT INTO colorer         VALUES (toadd_id_piece, toadd_id_couleur);
    INSERT INTO etre_forme      VALUES (toadd_id_forme, toadd_id_piece);

END; $$
LANGUAGE plpgsql;

-- To add/remove a member.

CREATE OR REPLACE FUNCTION add_membre(
    toadd_id_membre      INT,
    toadd_nom_membre     TEXT
) RETURNS VOID AS $$
BEGIN
    -- If the member is already in the database, do nothing.
    IF EXISTS(SELECT 1 FROM membres WHERE id_membre = toadd_id_membre) THEN
        RAISE NOTICE 'The % member is already in the db.', toadd_id_membre;
        RETURN;
    END IF;

    INSERT INTO membres         VALUES (toadd_id_membre, toadd_nom_membre);

END; $$
LANGUAGE plpgsql;

CREATE OR REPLACE FUNCTION remove_membre(
    torem_id_membre      INT
) RETURNS VOID AS $$
BEGIN
    -- If the membre does not exist.
    SELECT is_existing_membre(torem_id_membre);

    DELETE FROM acheter         WHERE id_membre = torem_id_membre;
    DELETE FROM enregister      WHERE id_membre = torem_id_membre;
    DELETE FROM messages        WHERE id_membre = torem_id_membre;
    DELETE FROM modeles         WHERE id_membre = torem_id_membre;
    DELETE FROM noter           WHERE id_membre = torem_id_membre;
    DELETE FROM perdre          WHERE id_membre = torem_id_membre;
    DELETE FROM membres         WHERE id_membre = torem_id_membre;

    RAISE NOTICE '% membre sucessfully removed.', torem_id_membre;

END; $$
LANGUAGE plpgsql;

-- To add/remove messages.

CREATE OR REPLACE FUNCTION add_message(
    toadd_id_message      INT,
    toadd_contenu         TEXT,
    toadd_id_membre       INT,
    toadd_id_fil          INT,
    toadd_id_message_2    INT DEFAULT NULL
) RETURNS VOID AS $$
BEGIN
    -- If the message is already in the database, do nothing.
    IF EXISTS(SELECT 1 FROM messages WHERE id_message = toadd_id_message) THEN
        RAISE NOTICE 'The % message is already in the db.', toadd_id_message;
        RETURN;
    END IF;

    -- To verify if the member exists.
    IF NOT EXISTS(SELECT 1 FROM membres WHERE id_membre = toadd_id_membre) THEN
        RAISE NOTICE 'The % membre does not exist.', toadd_id_membre;
        RETURN;
    END IF;

    -- To verify if the fil exists.
    IF NOT EXISTS(SELECT 1 FROM fils WHERE id_fil = toadd_id_fil) THEN
        RAISE NOTICE 'The % fil does not exist.', toadd_id_fil;
        RETURN;
    END IF;

    INSERT INTO messages        VALUES (
        toadd_id_message, 
        toadd_contenu,
        toadd_id_membre,
        toadd_id_fil,
        toadd_id_message_2
    );

END; $$
LANGUAGE plpgsql;

CREATE OR REPLACE FUNCTION remove_message(
    torem_id_message      INT
) RETURNS VOID AS $$
BEGIN

    -- If the message does not exist.
    IF NOT EXISTS(
        SELECT 1 FROM messages 
        WHERE id_messages = torem_id_messages
        )
        THEN
        RAISE NOTICE 'The % message does not exist.', torem_id_message;
        RETURN;
    END IF;

    DELETE FROM messages        WHERE id_message = torem_id_message;

    RAISE NOTICE '% message sucessfully removed.', torem_id_message;

END; $$
LANGUAGE plpgsql;

-- To change the name of a brand.

CREATE OR REPLACE FUNCTION change_brand_name(
    toadd_id_marque        INT,
    toadd_nom_marque       TEXT
) RETURNS VOID AS $$
BEGIN

    -- If the marque does not exist.
    SELECT is_existing_marque(toadd_id_marque);

    UPDATE marques
    SET nom_marque  = toadd_nom_marque
    WHERE id_marque = toadd_id_marque;

    RAISE NOTICE '% marque sucessfully changed.', toadd_id_marque;

END; $$
LANGUAGE plpgsql;


-- To change a notes.

CREATE OR REPLACE FUNCTION change_note(
    toadd_id_modele        INT,
    toadd_id_membre        INT,
    toadd_note             TEXT
) RETURNS VOID AS $$
BEGIN

    -- If the notes do not exist.
    SELECT is_existing_note(toadd_id_modele, toadd_id_membre);

    UPDATE noter
    SET note_noter  = toadd_note
    WHERE     id_modele = toadd_id_modele 
        AND   id_membre = toadd_id_membre;

    RAISE NOTICE 'Notes sucessfully changed';

END; $$
LANGUAGE plpgsql;

-- To add the pieces of a set to a member possession.

CREATE OR REPLACE FUNCTION add_pieces_to_member(
    toadd_id_membre        INT,
    toadd_id_boite         INT
) RETURNS VOID AS $$
DECLARE
    var_id_piece INT;
    var_quantite INT;
BEGIN
     -- If the membre does not exist.
    SELECT is_existing_piece(torem_id_membre);

    -- If the piece does not exist.
    SELECT is_existing_piece(torem_id_piece);

    FOR var_id_piece, var_quantite IN
        SELECT id_piece, quantite
        FROM contenir
        WHERE id_boite = toadd_id_boite
    LOOP

        IF EXISTS(SELECT 1 FROM acheter 
            WHERE   id_membre = toadd_id_membre 
                AND id_piece  = toadd_id_piece) 
        THEN

            UPDATE acheter
            SET quantite = quantite + var_quantite
            WHERE id_membre = toadd_id_membre AND id_piece = toadd_id_piece;

        ELSE

            INSERT INTO acheter VALUES (
                toadd_id_membre, 
                var_id_piece, 
                var_quantite
            );

        END IF;

    END LOOP;

    RAISE NOTICE 'Pieces sucessfully added.';

END; $$
LANGUAGE plpgsql;

-- ============================================================================

