/*
---------------------------------- Zusatzstoffklasse ----------------------------------
*/
LOAD DATA LOCAL INFILE 'Programmentwurf/Zusatzstoffe_Klassen.csv' INTO TABLE Zusatzstoffklase
    CHARACTER SET latin1
    FIELDS terminated by ';'
    LINES TERMINATED BY '\n'
    (@b, @g)
    SET Anwendungsgrund = REGEXP_REPLACE(@g, '[^a-zäöüßA-ZÄÖÜ]+', ''), Anwendungsgrundbezeichnung = TRIM(@b);


/*
---------------------------------- Zusatzstoff ----------------------------------
*/
DROP TEMPORARY TABLE IF EXISTS tmp_import_0;
CREATE TABLE IF NOT EXISTS tmp_import_0
(
    E_Nummer               varchar(5),
    Zusatzstoffbezeichnung varchar(100),
    Anwendungsgrund        varchar(5),
    primary key (E_Nummer)
);

LOAD DATA LOCAL INFILE 'Programmentwurf/E_nummern.csv' INTO TABLE tmp_import_0
    CHARACTER SET latin1
    FIELDS terminated by ';'
    LINES TERMINATED BY '\r\n'
    (@e, @z, @k)
    SET E_Nummer = TRIM(@e), Zusatzstoffbezeichnung = TRIM(@z), Anwendungsgrund = TRIM(@k);

INSERT INTO Zusatzstoff (SELECT E_Nummer, Zusatzstoffbezeichnung FROM tmp_import_0);

/*
---------------------------------- Produkt ----------------------------------
*/
DROP TEMPORARY TABLE IF EXISTS tmp_import_1;
CREATE TABLE IF NOT EXISTS tmp_import_1
(
    ProduktID   int AUTO_INCREMENT,
    Produktname VARCHAR(100),
    vegetarisch tinyint(1),
    vegan       tinyint(1),
    primary key (ProduktID)
);

LOAD DATA LOCAL INFILE 'Programmentwurf/MD_Produkte_Portionen.csv' INTO TABLE tmp_import_1
    CHARACTER SET latin1
    FIELDS terminated by ';'
    LINES TERMINATED BY '\n'
    IGNORE 2 LINES
    (@p, @dummy, @dummy)
    SET Produktname = TRIM(@p);

DELETE
FROM tmp_import_1
ORDER BY ProduktID DESC
LIMIT 4;

INSERT INTO Produkt (SELECT null, Produktname FROM tmp_import_1 GROUP BY Produktname);
INSERT INTO Produkt (Produktname)
VALUES ('Butter');
INSERT INTO Produkt (Produktname)
VALUES ('Snack Salat Classic');


/*
---------------------------------- Allergen ----------------------------------
*/
LOAD DATA LOCAL INFILE 'Programmentwurf/MD_produkte_Allergene.csv' INTO TABLE Allergen
    CHARACTER SET latin1
    FIELDS terminated by ';'
    LINES TERMINATED BY '\n'
    IGNORE 1 LINES
    (@a, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy,
     @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy)
    SET Allergenbezeichnung = TRIM(@a);


/*
---------------------------------- Produktallergen ----------------------------------
*/
DROP TEMPORARY TABLE IF EXISTS tmp_import_2;
CREATE TABLE IF NOT EXISTS tmp_import_2
(
    AllergenID        int,
    ProduktID         int,
    contains_allergen tinyint(1),
    foreign key (AllergenID) references Allergen (AllergenID),
    foreign key (ProduktID) references Produkt (ProduktID),
    primary key (AllergenID, ProduktID)
);

# Big Mac
LOAD DATA LOCAL INFILE 'Programmentwurf/MD_produkte_Allergene.csv' INTO TABLE tmp_import_2
    CHARACTER SET latin1
    FIELDS terminated by ';'
    LINES TERMINATED BY '\n'
    IGNORE 1 LINES
    (@a, @b, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy,
     @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy)
    SET ProduktID = (SELECT ProduktID
                     from Produkt
                     WHERE Produktname = 'Big Mac'), AllergenID = (SELECT AllergenID
                                                                   FROM Allergen
                                                                   WHERE Allergenbezeichnung = TRIM(@a)), contains_allergen = TRIM(@b);

# Cheeseburger
LOAD DATA LOCAL INFILE 'Programmentwurf/MD_produkte_Allergene.csv' INTO TABLE tmp_import_2
    CHARACTER SET latin1
    FIELDS terminated by ';'
    LINES TERMINATED BY '\n'
    IGNORE 1 LINES
    (@a, @dummy, @b, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy,
     @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy)
    SET ProduktID = (SELECT ProduktID
                     from Produkt
                     WHERE Produktname = 'Cheeseburger'), AllergenID = (SELECT AllergenID
                                                                        FROM Allergen
                                                                        WHERE Allergenbezeichnung = TRIM(@a)), contains_allergen = TRIM(@b);

# Chicken McNuggets
LOAD DATA LOCAL INFILE 'Programmentwurf/MD_produkte_Allergene.csv' INTO TABLE tmp_import_2
    CHARACTER SET latin1
    FIELDS terminated by ';'
    LINES TERMINATED BY '\n'
    IGNORE 1 LINES
    (@a, @dummy, @dummy, @b, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy,
     @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy)
    SET ProduktID = (SELECT ProduktID
                     from Produkt
                     WHERE Produktname = 'Chicken McNuggets'), AllergenID = (SELECT AllergenID
                                                                             FROM Allergen
                                                                             WHERE Allergenbezeichnung = TRIM(@a)), contains_allergen = TRIM(@b);

# Hamburger
LOAD DATA LOCAL INFILE 'Programmentwurf/MD_produkte_Allergene.csv' INTO TABLE tmp_import_2
    CHARACTER SET latin1
    FIELDS terminated by ';'
    LINES TERMINATED BY '\n'
    IGNORE 1 LINES
    (@a, @dummy, @dummy, @dummy, @b, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy,
     @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy)
    SET ProduktID = (SELECT ProduktID
                     from Produkt
                     WHERE Produktname = 'Hamburger'), AllergenID = (SELECT AllergenID
                                                                     FROM Allergen
                                                                     WHERE Allergenbezeichnung = TRIM(@a)), contains_allergen = TRIM(@b);

# Hamburger Royal TS
LOAD DATA LOCAL INFILE 'Programmentwurf/MD_produkte_Allergene.csv' INTO TABLE tmp_import_2
    CHARACTER SET latin1
    FIELDS terminated by ';'
    LINES TERMINATED BY '\n'
    IGNORE 1 LINES
    (@a, @dummy, @dummy, @dummy, @dummy, @b, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy,
     @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy)
    SET ProduktID = (SELECT ProduktID
                     from Produkt
                     WHERE Produktname = 'Hamburger Royal TS'), AllergenID = (SELECT AllergenID
                                                                              FROM Allergen
                                                                              WHERE Allergenbezeichnung = TRIM(@a)), contains_allergen = TRIM(@b);

# McChicken
LOAD DATA LOCAL INFILE 'Programmentwurf/MD_produkte_Allergene.csv' INTO TABLE tmp_import_2
    CHARACTER SET latin1
    FIELDS terminated by ';'
    LINES TERMINATED BY '\n'
    IGNORE 1 LINES
    (@a, @dummy, @dummy, @dummy, @dummy, @dummy, @b, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy,
     @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy)
    SET ProduktID = (SELECT ProduktID
                     from Produkt
                     WHERE Produktname = 'McChicken'), AllergenID = (SELECT AllergenID
                                                                     FROM Allergen
                                                                     WHERE Allergenbezeichnung = TRIM(@a)), contains_allergen = TRIM(@b);

# McRib
LOAD DATA LOCAL INFILE 'Programmentwurf/MD_produkte_Allergene.csv' INTO TABLE tmp_import_2
    CHARACTER SET latin1
    FIELDS terminated by ';'
    LINES TERMINATED BY '\n'
    IGNORE 1 LINES
    (@a, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @b, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy,
     @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy)
    SET ProduktID = (SELECT ProduktID
                     from Produkt
                     WHERE Produktname = 'McRib'), AllergenID = (SELECT AllergenID
                                                                 FROM Allergen
                                                                 WHERE Allergenbezeichnung = TRIM(@a)), contains_allergen = TRIM(@b);

# Pommes Frites
LOAD DATA LOCAL INFILE 'Programmentwurf/MD_produkte_Allergene.csv' INTO TABLE tmp_import_2
    CHARACTER SET latin1
    FIELDS terminated by ';'
    LINES TERMINATED BY '\n'
    IGNORE 1 LINES
    (@a, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @b, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy,
     @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy)
    SET ProduktID = (SELECT ProduktID
                     from Produkt
                     WHERE Produktname = 'Pommes Frites'), AllergenID = (SELECT AllergenID
                                                                         FROM Allergen
                                                                         WHERE Allergenbezeichnung = TRIM(@a)), contains_allergen = TRIM(@b);

# Apfeltasche
LOAD DATA LOCAL INFILE 'Programmentwurf/MD_produkte_Allergene.csv' INTO TABLE tmp_import_2
    CHARACTER SET latin1
    FIELDS terminated by ';'
    LINES TERMINATED BY '\n'
    IGNORE 1 LINES
    (@a, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @b, @dummy, @dummy, @dummy, @dummy, @dummy,
     @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy)
    SET ProduktID = (SELECT ProduktID
                     from Produkt
                     WHERE Produktname = 'Apfeltasche'), AllergenID = (SELECT AllergenID
                                                                       FROM Allergen
                                                                       WHERE Allergenbezeichnung = TRIM(@a)), contains_allergen = TRIM(@b);

# McSundae Karamell
LOAD DATA LOCAL INFILE 'Programmentwurf/MD_produkte_Allergene.csv' INTO TABLE tmp_import_2
    CHARACTER SET latin1
    FIELDS terminated by ';'
    LINES TERMINATED BY '\n'
    IGNORE 1 LINES
    (@a, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @b, @dummy, @dummy, @dummy, @dummy,
     @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy)
    SET ProduktID = (SELECT ProduktID
                     from Produkt
                     WHERE Produktname = 'McSundae Karamell'), AllergenID = (SELECT AllergenID
                                                                             FROM Allergen
                                                                             WHERE Allergenbezeichnung = TRIM(@a)), contains_allergen = TRIM(@b);

# McSundae Schoko
LOAD DATA LOCAL INFILE 'Programmentwurf/MD_produkte_Allergene.csv' INTO TABLE tmp_import_2
    CHARACTER SET latin1
    FIELDS terminated by ';'
    LINES TERMINATED BY '\n'
    IGNORE 1 LINES
    (@a, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @b, @dummy, @dummy, @dummy,
     @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy)
    SET ProduktID = (SELECT ProduktID
                     from Produkt
                     WHERE Produktname = 'McSundae Schoko'), AllergenID = (SELECT AllergenID
                                                                           FROM Allergen
                                                                           WHERE Allergenbezeichnung = TRIM(@a)), contains_allergen = TRIM(@b);

# Milchshake Erdbeergeschmack
LOAD DATA LOCAL INFILE 'Programmentwurf/MD_produkte_Allergene.csv' INTO TABLE tmp_import_2
    CHARACTER SET latin1
    FIELDS terminated by ';'
    LINES TERMINATED BY '\n'
    IGNORE 1 LINES
    (@a, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @b, @dummy, @dummy,
     @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy)
    SET ProduktID = (SELECT ProduktID
                     from Produkt
                     WHERE Produktname = 'Milchshake Erdbeergeschmack'), AllergenID = (SELECT AllergenID
                                                                                       FROM Allergen
                                                                                       WHERE Allergenbezeichnung = TRIM(@a)), contains_allergen = TRIM(@b);

# Milchshake Schokogeschmack
LOAD DATA LOCAL INFILE 'Programmentwurf/MD_produkte_Allergene.csv' INTO TABLE tmp_import_2
    CHARACTER SET latin1
    FIELDS terminated by ';'
    LINES TERMINATED BY '\n'
    IGNORE 1 LINES
    (@a, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @b, @dummy,
     @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy)
    SET ProduktID = (SELECT ProduktID
                     from Produkt
                     WHERE Produktname = 'Milchshake Schokogeschmack'), AllergenID = (SELECT AllergenID
                                                                                      FROM Allergen
                                                                                      WHERE Allergenbezeichnung = TRIM(@a)), contains_allergen = TRIM(@b);

# Milchshake Vanillegeschmack
LOAD DATA LOCAL INFILE 'Programmentwurf/MD_produkte_Allergene.csv' INTO TABLE tmp_import_2
    CHARACTER SET latin1
    FIELDS terminated by ';'
    LINES TERMINATED BY '\n'
    IGNORE 1 LINES
    (@a, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @b,
     @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy)
    SET ProduktID = (SELECT ProduktID
                     from Produkt
                     WHERE Produktname = 'Milchshake Vanillegeschmack'), AllergenID = (SELECT AllergenID
                                                                                       FROM Allergen
                                                                                       WHERE Allergenbezeichnung = TRIM(@a)), contains_allergen = TRIM(@b);

# Butter
LOAD DATA LOCAL INFILE 'Programmentwurf/MD_produkte_Allergene.csv' INTO TABLE tmp_import_2
    CHARACTER SET latin1
    FIELDS terminated by ';'
    LINES TERMINATED BY '\n'
    IGNORE 1 LINES
    (@a, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy,
     @b, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy)
    SET ProduktID = (SELECT ProduktID
                     from Produkt
                     WHERE Produktname = 'Butter'), AllergenID = (SELECT AllergenID
                                                                  FROM Allergen
                                                                  WHERE Allergenbezeichnung = TRIM(@a)), contains_allergen = TRIM(@b);

# Konfituere Aprikose
LOAD DATA LOCAL INFILE 'Programmentwurf/MD_produkte_Allergene.csv' INTO TABLE tmp_import_2
    CHARACTER SET latin1
    FIELDS terminated by ';'
    LINES TERMINATED BY '\n'
    IGNORE 1 LINES
    (@a, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy,
     @dummy, @b, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy)
    SET ProduktID = (SELECT ProduktID
                     from Produkt
                     WHERE Produktname = 'Konfituere Aprikose'), AllergenID = (SELECT AllergenID
                                                                               FROM Allergen
                                                                               WHERE Allergenbezeichnung = TRIM(@a)), contains_allergen = TRIM(@b);

# Konfituere Erdbeere
LOAD DATA LOCAL INFILE 'Programmentwurf/MD_produkte_Allergene.csv' INTO TABLE tmp_import_2
    CHARACTER SET latin1
    FIELDS terminated by ';'
    LINES TERMINATED BY '\n'
    IGNORE 1 LINES
    (@a, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy,
     @dummy, @dummy, @b, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy)
    SET ProduktID = (SELECT ProduktID
                     from Produkt
                     WHERE Produktname = 'Konfituere Erdbeere'), AllergenID = (SELECT AllergenID
                                                                               FROM Allergen
                                                                               WHERE Allergenbezeichnung = TRIM(@a)), contains_allergen = TRIM(@b);

# McCroissant
LOAD DATA LOCAL INFILE 'Programmentwurf/MD_produkte_Allergene.csv' INTO TABLE tmp_import_2
    CHARACTER SET latin1
    FIELDS terminated by ';'
    LINES TERMINATED BY '\n'
    IGNORE 1 LINES
    (@a, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy,
     @dummy, @dummy, @dummy, @b, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy)
    SET ProduktID = (SELECT ProduktID
                     from Produkt
                     WHERE Produktname = 'McCroissant'), AllergenID = (SELECT AllergenID
                                                                       FROM Allergen
                                                                       WHERE Allergenbezeichnung = TRIM(@a)), contains_allergen = TRIM(@b);

# Balsamico Dressing
LOAD DATA LOCAL INFILE 'Programmentwurf/MD_produkte_Allergene.csv' INTO TABLE tmp_import_2
    CHARACTER SET latin1
    FIELDS terminated by ';'
    LINES TERMINATED BY '\n'
    IGNORE 1 LINES
    (@a, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy,
     @dummy, @dummy, @dummy, @dummy, @b, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy)
    SET ProduktID = (SELECT ProduktID
                     from Produkt
                     WHERE Produktname = 'Balsamico Dressing'), AllergenID = (SELECT AllergenID
                                                                              FROM Allergen
                                                                              WHERE Allergenbezeichnung = TRIM(@a)), contains_allergen = TRIM(@b);

# Barbecue Sauce
LOAD DATA LOCAL INFILE 'Programmentwurf/MD_produkte_Allergene.csv' INTO TABLE tmp_import_2
    CHARACTER SET latin1
    FIELDS terminated by ';'
    LINES TERMINATED BY '\n'
    IGNORE 1 LINES
    (@a, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy,
     @dummy, @dummy, @dummy, @dummy, @dummy, @b, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy)
    SET ProduktID = (SELECT ProduktID
                     from Produkt
                     WHERE Produktname = 'Barbecue Sauce'), AllergenID = (SELECT AllergenID
                                                                          FROM Allergen
                                                                          WHERE Allergenbezeichnung = TRIM(@a)), contains_allergen = TRIM(@b);

# Chili Sauce
LOAD DATA LOCAL INFILE 'Programmentwurf/MD_produkte_Allergene.csv' INTO TABLE tmp_import_2
    CHARACTER SET latin1
    FIELDS terminated by ';'
    LINES TERMINATED BY '\n'
    IGNORE 1 LINES
    (@a, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy,
     @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @b, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy)
    SET ProduktID = (SELECT ProduktID
                     from Produkt
                     WHERE Produktname = 'Chili Sauce'), AllergenID = (SELECT AllergenID
                                                                       FROM Allergen
                                                                       WHERE Allergenbezeichnung = TRIM(@a)), contains_allergen = TRIM(@b);

# Curry Sauce
LOAD DATA LOCAL INFILE 'Programmentwurf/MD_produkte_Allergene.csv' INTO TABLE tmp_import_2
    CHARACTER SET latin1
    FIELDS terminated by ';'
    LINES TERMINATED BY '\n'
    IGNORE 1 LINES
    (@a, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy,
     @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @b, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy)
    SET ProduktID = (SELECT ProduktID
                     from Produkt
                     WHERE Produktname = 'Curry Sauce'), AllergenID = (SELECT AllergenID
                                                                       FROM Allergen
                                                                       WHERE Allergenbezeichnung = TRIM(@a)), contains_allergen = TRIM(@b);

# Ketchup
LOAD DATA LOCAL INFILE 'Programmentwurf/MD_produkte_Allergene.csv' INTO TABLE tmp_import_2
    CHARACTER SET latin1
    FIELDS terminated by ';'
    LINES TERMINATED BY '\n'
    IGNORE 1 LINES
    (@a, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy,
     @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @b, @dummy, @dummy, @dummy, @dummy, @dummy)
    SET ProduktID = (SELECT ProduktID
                     from Produkt
                     WHERE Produktname = 'Ketchup'), AllergenID = (SELECT AllergenID
                                                                   FROM Allergen
                                                                   WHERE Allergenbezeichnung = TRIM(@a)), contains_allergen = TRIM(@b);

# Mayonnaise (80%)
LOAD DATA LOCAL INFILE 'Programmentwurf/MD_produkte_Allergene.csv' INTO TABLE tmp_import_2
    CHARACTER SET latin1
    FIELDS terminated by ';'
    LINES TERMINATED BY '\n'
    IGNORE 1 LINES
    (@a, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy,
     @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @b, @dummy, @dummy, @dummy, @dummy)
    SET ProduktID = (SELECT ProduktID
                     from Produkt
                     WHERE Produktname = 'Mayonnaise (80%)'), AllergenID = (SELECT AllergenID
                                                                            FROM Allergen
                                                                            WHERE Allergenbezeichnung = TRIM(@a)), contains_allergen = TRIM(@b);

# Sauce suesssauer
LOAD DATA LOCAL INFILE 'Programmentwurf/MD_produkte_Allergene.csv' INTO TABLE tmp_import_2
    CHARACTER SET latin1
    FIELDS terminated by ';'
    LINES TERMINATED BY '\n'
    IGNORE 1 LINES
    (@a, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy,
     @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @b, @dummy, @dummy, @dummy)
    SET ProduktID = (SELECT ProduktID
                     from Produkt
                     WHERE Produktname = 'Sauce suesssauer'), AllergenID = (SELECT AllergenID
                                                                            FROM Allergen
                                                                            WHERE Allergenbezeichnung = TRIM(@a)), contains_allergen = TRIM(@b);

# Senfsauce
LOAD DATA LOCAL INFILE 'Programmentwurf/MD_produkte_Allergene.csv' INTO TABLE tmp_import_2
    CHARACTER SET latin1
    FIELDS terminated by ';'
    LINES TERMINATED BY '\n'
    IGNORE 1 LINES
    (@a, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy,
     @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @b, @dummy, @dummy)
    SET ProduktID = (SELECT ProduktID
                     from Produkt
                     WHERE Produktname = 'Senfsauce'), AllergenID = (SELECT AllergenID
                                                                     FROM Allergen
                                                                     WHERE Allergenbezeichnung = TRIM(@a)), contains_allergen = TRIM(@b);

# Snack Salat Classic
LOAD DATA LOCAL INFILE 'Programmentwurf/MD_produkte_Allergene.csv' INTO TABLE tmp_import_2
    CHARACTER SET latin1
    FIELDS terminated by ';'
    LINES TERMINATED BY '\n'
    IGNORE 1 LINES
    (@a, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy,
     @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @b, @dummy)
    SET ProduktID = (SELECT ProduktID
                     from Produkt
                     WHERE Produktname = 'Snack Salat Classic'), AllergenID = (SELECT AllergenID
                                                                               FROM Allergen
                                                                               WHERE Allergenbezeichnung = TRIM(@a)), contains_allergen = TRIM(@b);

# Warum ist das zweimal in MD_produkte_Allergene.csv?????
# # Apfeltasche
# LOAD DATA LOCAL INFILE 'Programmentwurf/MD_produkte_Allergene.csv' INTO TABLE tmp_import_2
# CHARACTER SET latin1
# FIELDS terminated by ';'
# LINES TERMINATED BY '\n'
# IGNORE 1 LINES
# (@a, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @b)
# SET ProduktID = (SELECT ProduktID from Produkt WHERE Produktname = 'Apfeltasche'), AllergenID = (SELECT AllergenID FROM Allergen WHERE Allergenbezeichnung = TRIM(@a)), contains_allergen = TRIM(@b);

INSERT INTO enthaeltAllergen (SELECT AllergenID, ProduktID FROM tmp_import_2 WHERE contains_allergen = 1);


/*
---------------------------------- Menge ----------------------------------
*/
LOAD DATA LOCAL INFILE 'Programmentwurf/MD_Produkte_Portionen.csv' INTO TABLE Menge
    CHARACTER SET latin1
    FIELDS terminated by ';'
    LINES TERMINATED BY '\n'
    IGNORE 2 LINES
    (@produkt, @menge1, @menge2)
    SET ProduktID = (SELECT ProduktID
                     from Produkt
                     WHERE Produktname = @produkt), Menge = (IF(@menge1 != '-', (REPLACE(@menge1, ' g', '')),
                                                                (REPLACE(@menge2, ' ml', '')))), Einheit = (IF(@menge1 != '-', 'g', 'ml'));

DELETE
FROM Menge
ORDER BY MengenID DESC
LIMIT 4;


/*
---------------------------------- enthält Zusatzstoff ----------------------------------
*/
DROP TABLE IF EXISTS tmp_import_3;
CREATE TABLE IF NOT EXISTS tmp_import_3
(
    Produkt_name VARCHAR(100),
    E_Nummern    VARCHAR(100)
);

LOAD DATA LOCAL INFILE 'Programmentwurf/MD_Produkte_Enummern.csv' REPLACE INTO TABLE tmp_import_3
    CHARACTER SET latin1
    FIELDS terminated by ';'
    LINES TERMINATED BY '\n'
    (@col1, @col2) SET Produkt_Name = trim(@col1), E_Nummern = trim(@col2);

DROP TABLE IF EXISTS tmp_import_4;
CREATE TABLE IF NOT EXISTS tmp_import_4
(
    id        int NOT NULL auto_increment,
    E_Nummern VARCHAR(100),
    PRIMARY KEY (id)
);

DROP TABLE IF EXISTS tmp_import_5;
CREATE TABLE IF NOT EXISTS tmp_import_5
(
    Produkt_Name varchar(100),
    E_nummern    VARCHAR(100)
);

/* Delete all seperators and create new rows */
INSERT INTO tmp_import_5(Produkt_Name, E_Nummern)
SELECT Produkt_Name,
       SUBSTRING_INDEX(SUBSTRING_INDEX(tmp_import_3.E_Nummern, ',', numbers.n), ',', -1)
FROM (SELECT 1 n
      UNION ALL
      SELECT 2
      UNION ALL
      SELECT 3
      UNION ALL
      SELECT 4) numbers
         INNER JOIN tmp_import_3
                    ON CHAR_LENGTH(tmp_import_3.E_Nummern)
                           - CHAR_LENGTH(REPLACE(tmp_import_3.E_Nummern, ',', '')) >= numbers.n - 1;

UPDATE tmp_import_5
SET Produkt_name = (SELECT ProduktID FROM Produkt WHERE Produktname = Produkt_name);

INSERT INTO enthaeltZusatzstoff (SELECT Produkt_Name,
                                        E_nummern,
                                        (SELECT Anwendungsgrund FROM tmp_import_0 WHERE E_Nummer = E_nummern)
                                 FROM tmp_import_5
                                 WHERE Produkt_Name IN (SELECT ProduktID FROM Produkt)
                                   AND E_nummern IN (SELECT E_Nummer FROM Zusatzstoff));

/*
---------------------------------- Naehrwerte ----------------------------------
*/
LOAD DATA LOCAL INFILE 'Programmentwurf/MD_Produkte_Naehrwerte.csv' INTO TABLE Naehrwerte
    CHARACTER SET latin1
    FIELDS terminated by ';'
    LINES TERMINATED BY '\n'
    IGNORE 2 LINES
    (@produkt, @menge1, @menge2, @kcal, @ew, @kh, @fett)
    SET ProduktID = (SELECT ProduktID
                     from Produkt
                     WHERE Produktname = @produkt), Menge = (IF(@menge1 != '-', (REPLACE(@menge1, ' g', '')),
                                                                (REPLACE(@menge2, ' ml', '')))), Einheit = (IF(@menge1 != '-', 'g', 'ml')), Kohlenhydrate = @kh, Eiweiss = @ew,
        Fett = CAST(REPLACE(REPLACE(@fett, CHAR(13), ''), CHAR(10), '')
            as double), Brennwert = @kcal;
# Issue at last four lines: ProduktID cannot be null, ...


/*
---------------------------------- eigene Datensätze ----------------------------------
---------------------------------- Personen ----------------------------------
*/
INSERT INTO Person (Personenname, Personenvorname, Naehrwertziel, Ernaerungsstil)
VALUES ('Kaiser', 'Florian', 5000, null);
INSERT INTO Person (Personenname, Personenvorname, Naehrwertziel, Ernaerungsstil)
VALUES ('Amtmann', 'Leon', 10000, null);
INSERT INTO Person (Personenname, Personenvorname, Naehrwertziel, Ernaerungsstil)
VALUES ('Mustermann', 'Max', 15000, null);


/*
---------------------------------- Allergie ----------------------------------
*/
INSERT INTO Allergie (PersonID, AllergenID)
VALUES ((SELECT PersonID FROM Person WHERE Personenname = 'Kaiser'),
        (SELECT AllergenID FROM Allergen WHERE Allergenbezeichnung = 'Glutenhaltiges Getreide'));
INSERT INTO Allergie (PersonID, AllergenID)
VALUES ((SELECT PersonID FROM Person WHERE Personenname = 'Kaiser'),
        (SELECT AllergenID FROM Allergen WHERE Allergenbezeichnung = 'Erdnuesse'));
INSERT INTO Allergie (PersonID, AllergenID)
VALUES ((SELECT PersonID FROM Person WHERE Personenname = 'Amtmann'),
        (SELECT AllergenID FROM Allergen WHERE Allergenbezeichnung = 'Sesamsamen'));


/*
---------------------------------- Menu ----------------------------------
*/
INSERT INTO Menu (Menuname)
VALUES ('Test Menu');
INSERT INTO Menu (Menuname)
VALUES ('Test Menu #2');
INSERT INTO Menu (Menuname)
VALUES ('Test Menu #3');


/*
---------------------------------- Menucontent ----------------------------------
*/
INSERT INTO Menucontent (MenuID, ProduktID)
VALUES ((SELECT MenuID FROM Menu WHERE Menuname = 'Test Menu'),
        (SELECT ProduktID FROM Produkt WHERE Produktname = 'Butter'));
INSERT INTO Menucontent (MenuID, ProduktID)
VALUES ((SELECT MenuID FROM Menu WHERE Menuname = 'Test Menu'),
        (SELECT ProduktID FROM Produkt WHERE Produktname = 'Pommes Frites'));
INSERT INTO Menucontent (MenuID, ProduktID)
VALUES ((SELECT MenuID FROM Menu WHERE Menuname = 'Test Menu'),
        (SELECT ProduktID FROM Produkt WHERE Produktname = 'Curry Sauce'));

INSERT INTO Menucontent (MenuID, ProduktID)
VALUES ((SELECT MenuID FROM Menu WHERE Menuname = 'Test Menu #2'),
        (SELECT ProduktID FROM Produkt WHERE Produktname = 'Ketchup'));
INSERT INTO Menucontent (MenuID, ProduktID)
VALUES ((SELECT MenuID FROM Menu WHERE Menuname = 'Test Menu #2'),
        (SELECT ProduktID FROM Produkt WHERE Produktname = 'Big Mac'));
INSERT INTO Menucontent (MenuID, ProduktID)
VALUES ((SELECT MenuID FROM Menu WHERE Menuname = 'Test Menu #2'),
        (SELECT ProduktID FROM Produkt WHERE Produktname = 'Cheeseburger'));

INSERT INTO Menucontent (MenuID, ProduktID)
VALUES ((SELECT MenuID FROM Menu WHERE Menuname = 'Test Menu #3'),
        (SELECT ProduktID FROM Produkt WHERE Produktname = 'Cheeseburger'));
INSERT INTO Menucontent (MenuID, ProduktID)
VALUES ((SELECT MenuID FROM Menu WHERE Menuname = 'Test Menu #3'),
        (SELECT ProduktID FROM Produkt WHERE Produktname = 'McSundae Karamell'));
INSERT INTO Menucontent (MenuID, ProduktID)
VALUES ((SELECT MenuID FROM Menu WHERE Menuname = 'Test Menu #3'),
        (SELECT ProduktID FROM Produkt WHERE Produktname = 'Big Mac'));


/*
---------------------------------- konsumiert ----------------------------------
*/
INSERT INTO konsumiert (PersonID, MenuID, Datum)
VALUES ((SELECT PersonID FROM Person WHERE Personenname = 'Kaiser'),
        (SELECT MenuID FROM Menu WHERE Menuname = 'Test Menu'), now());
INSERT INTO konsumiert (PersonID, MenuID, Datum)
VALUES ((SELECT PersonID FROM Person WHERE Personenname = 'Amtmann'),
        (SELECT MenuID FROM Menu WHERE Menuname = 'Test Menu #2'), now());
INSERT INTO konsumiert (PersonID, MenuID, Datum)
VALUES ((SELECT PersonID FROM Person WHERE Personenname = 'Kaiser'),
        (SELECT MenuID FROM Menu WHERE Menuname = 'Test Menu #2'), now());


/*
---------------------------------- Zusatzstoffunvertraeglichkeit ----------------------------------
*/
INSERT INTO Zusatzstoffunvertraeglichkeit (PersonID, E_Nummer)
VALUES ((SELECT PersonID FROM Person WHERE Personenname = 'Kaiser'),
        (SELECT E_Nummer FROM Zusatzstoff WHERE Zusatzstoffbezeichnung = 'Eisenoxide und Eisenhydroxide'));
INSERT INTO Zusatzstoffunvertraeglichkeit (PersonID, E_Nummer)
VALUES ((SELECT PersonID FROM Person WHERE Personenname = 'Kaiser'),
        (SELECT E_Nummer FROM Zusatzstoff WHERE Zusatzstoffbezeichnung = 'Gold'));
INSERT INTO Zusatzstoffunvertraeglichkeit (PersonID, E_Nummer)
VALUES ((SELECT PersonID FROM Person WHERE Personenname = 'Amtmann'),
        (SELECT E_Nummer FROM Zusatzstoff WHERE Zusatzstoffbezeichnung = 'Gold'));


/*
---------------------------------- Clean up ----------------------------------
*/
DROP TABLE IF EXISTS tmp_import_0;
DROP TABLE IF EXISTS tmp_import_1;
DROP TABLE IF EXISTS tmp_import_2;
DROP TABLE IF EXISTS tmp_import_3;
DROP TABLE IF EXISTS tmp_import_4;
DROP TABLE IF EXISTS tmp_import_5;