DO $$
DECLARE 
idformsoriginsinserted bigint;
idformsinserted bigint;
idfieldsinserted  bigint;
BEGIN
    SELECT id FROM formsorigins INTO idformsoriginsinserted WHERE name = 'WP6.MobileApp' and active = 1;

    --BEGIN FLOODZONES_FORM
    INSERT INTO forms(idformsorigins, code, name, description, dtcreation, active) values (idformsoriginsinserted, "FLOODZONES_FORM", "Flood Zones", "Flood Zones Form", DateTime(), 1)
        RETURNING id INTO idformsinserted;

    INSERT INTO fields(idfieldsdatatypes, name, description, fillingclue, active)
        SELECT fdt.id, 'id', 'id From FloodZones Form', '', 1 FROM fieldsdatatypes fdt WHERE fdt.name = 'integer' 
            RETURNING id INTO idfieldsinserted;

    INSERT INTO formsfields(id, idforms, idfields, active) VALUES (DEFAULT, idformsinserted, idfieldsinserted, 1);

    INSERT INTO fields(idfieldsdatatypes, name, description, fillingclue, active)
        SELECT fdt.id, 'description', 'description From FloodZones Form', '', 1 FROM fieldsdatatypes fdt WHERE fdt.name = 'text'
            RETURNING id INTO idfieldsinserted;

    INSERT INTO formsfields(id, idforms, idfields, active) VALUES (DEFAULT, idformsinserted, idfieldsinserted, 1);

    INSERT INTO fields(idfieldsdatatypes, name, description, fillingclue, active)
        SELECT fdt.id, 'Images', 'Images From FloodZones Form', '', 1 FROM fieldsdatatypes fdt WHERE fdt.name = 'text'
            RETURNING id INTO idfieldsinserted;

    INSERT INTO formsfields(id, idforms, idfields, active) VALUES (DEFAULT, idformsinserted, idfieldsinserted, 1);

    INSERT INTO fields(idfieldsdatatypes, name, description, fillingclue, active)
        SELECT fdt.id, 'Latitude', 'Latitude From FloodZones Form', '', 1 FROM fieldsdatatypes fdt WHERE fdt.name = 'real'
            RETURNING id INTO idfieldsinserted;

    INSERT INTO formsfields(id, idforms, idfields, active) VALUES (DEFAULT, idformsinserted, idfieldsinserted, 1);

    INSERT INTO fields(idfieldsdatatypes, name, description, fillingclue, active)
        SELECT fdt.id, 'Longitude', 'Longitude From FloodZones Form', '', 1 FROM fieldsdatatypes fdt WHERE fdt.name = 'real'
            RETURNING id INTO idfieldsinserted;

    INSERT INTO formsfields(id, idforms, idfields, active) VALUES (DEFAULT, idformsinserted, idfieldsinserted, 1);

    INSERT INTO fields(idfieldsdatatypes, name, description, fillingclue, active)
        SELECT fdt.id, 'Passable', 'Passable From FloodZones Form', '', 1 FROM fieldsdatatypes fdt WHERE fdt.name = 'integer'
            RETURNING id INTO idfieldsinserted;

    INSERT INTO formsfields(id, idforms, idfields, active) VALUES (DEFAULT, idformsinserted, idfieldsinserted, 1);

    INSERT INTO fields(idfieldsdatatypes, name, description, fillingclue, active)
        SELECT fdt.id, 'Date', 'Date From FloodZones Form', '', 1 FROM fieldsdatatypes fdt WHERE fdt.name = 'text'
            RETURNING id INTO idfieldsinserted;

    INSERT INTO formsfields(id, idforms, idfields, active) VALUES (DEFAULT, idformsinserted, idfieldsinserted, 1);

    INSERT INTO fields(idfieldsdatatypes, name, description, fillingclue, active)
        SELECT fdt.id, 'Time', 'Time From FloodZones Form', '', 1 FROM fieldsdatatypes fdt WHERE fdt.name = 'text'
            RETURNING id INTO idfieldsinserted;

    INSERT INTO formsfields(id, idforms, idfields, active) VALUES (DEFAULT, idformsinserted, idfieldsinserted, 1);

    INSERT INTO fields(idfieldsdatatypes, name, description, fillingclue, active)
        SELECT fdt.id, 'Address', 'Address From FloodZones Form', '', 1 FROM fieldsdatatypes fdt WHERE fdt.name = 'text'
            RETURNING id INTO idfieldsinserted;

    INSERT INTO formsfields(id, idforms, idfields, active) VALUES (DEFAULT, idformsinserted, idfieldsinserted, 1);
    --END FLOODZONES_FORM

    --BEGIN PLUVIOMETERS_FORM
    INSERT INTO forms(idformsorigins, code, name, description, dtcreation, active) values (idformsoriginsinserted, "PLUVIOMETERS_FORM", "Pluviometers", "Pluviometers Form", DateTime(), 1);
    --END PLUVIOMETERS_FORM

    --BEGIN RAIN_FORM
    INSERT INTO forms(idformsorigins, code, name, description, dtcreation, active) values (idformsoriginsinserted, "RAIN_FORM", "Rain", "Rain zones Form", DateTime(), 1);
    --END RAIN_FORM

    --BEGIN RIVERFLOOD_FORM
    INSERT INTO forms(idformsorigins, code, name, description, dtcreation, active) values (idformsoriginsinserted, "RIVERFLOOD_FORM", "River Flood", "River Flood Form", DateTime(), 1);
    --END RIVERFLOOD_FORM

    --BEGIN PLUVIOMETERS_REGISTRATION
    INSERT INTO forms(idformsorigins, code, name, description, dtcreation, active) values (idformsoriginsinserted, "PLUVIOMETERS_REGISTRATION", "Pluviometer registration", "Pluviometer registration", DateTime(), 1);        
    --END PLUVIOMETERS_REGISTRATION

END $$;




