DROP VIEW IF EXISTS `gm_settings_view`;
DROP VIEW IF EXISTS `gm_subsettings_view`;
DROP VIEW IF EXISTS `gm_raw_subsettings_view_for_bo`;
DROP VIEW IF EXISTS `gm_events_affected_by_latest_mod_settings_view`;
DROP VIEW IF EXISTS `gm_evscheds_affected_by_latest_mod_settings_view`;


--
-- Structure for view `gm_settings_view`
-- this view shows the settings but includes a validation
--

CREATE VIEW gm_settings_view AS
SELECT gm_id,
    gm_mod_stat,
    CASE WHEN gm_mod_stat = 0 THEN 0 ELSE noshow_psub_stat END AS noshow_psub_stat,
    CASE WHEN gm_mod_stat = 0 THEN 0 ELSE res_psub_stat END AS res_psub_stat,
    
    (CASE WHEN gm_mod_stat = 0 THEN 0 ELSE
        CASE WHEN (noshow_psub_stat = 1 OR res_psub_stat = 1) THEN event_cat_stat
        ELSE 0 END 
    END) AS event_cat_stat,
    
    (CASE WHEN gm_mod_stat = 0 THEN 0 ELSE 
        CASE WHEN (noshow_psub_stat = 1 OR res_psub_stat = 1) THEN event_loc_stat
        ELSE 0 END 
    END) AS event_loc_stat,
    
    (CASE WHEN gm_mod_stat = 0 THEN 0 ELSE 
        CASE WHEN (noshow_psub_stat = 1 OR res_psub_stat = 1) THEN event_month_stat
        ELSE 0 END 
    END) AS event_month_stat,
    
    CASE WHEN gm_mod_stat = 0 THEN 0 ELSE flex_mod_stat END AS flex_mod_stat,
    date_created,
(
    CASE WHEN gm_id IN((
          SELECT evmod.gm_id 
          FROM gm_evsched_setting evmod
          WHERE (evmod.gm_id = gm.gm_id))) THEN 1
    ELSE 0
    END
) AS is_used
from global_moderation gm;

-- --------------------------------------------------------

--
-- Structure for view `gm_subsettings_view`
-- Subsettings that contain some validations
--

CREATE VIEW gm_subsettings_view AS
SELECT gm_id,
    CASE WHEN noshow_psub_stat = 0 THEN NULL 
	 ELSE gnp.max_noshow END AS max_noshow,
	 CASE WHEN noshow_psub_stat = 0 THEN NULL 
    ELSE gnp.nshow_period END AS nshow_period,

    CASE WHEN res_psub_stat = 0 THEN NULL 
	 ELSE grp.max_res END AS max_res,
    CASE WHEN res_psub_stat = 0 THEN NULL 
	 ELSE grp.res_period_type END AS res_period_type,

    CASE WHEN event_cat_stat = 0 THEN NULL ELSE 
    (SELECT GROUP_CONCAT(event_cat,'') FROM gm_event_cat WHERE gm_id = gmv.gm_id GROUP BY gm_id) 
    END AS event_cats,
    
    CASE WHEN event_loc_stat = 0 THEN NULL ELSE 
    (SELECT GROUP_CONCAT(event_location,'') FROM gm_event_loc WHERE gm_id = gmv.gm_id GROUP BY gm_id) 
    END AS locations,
    
    CASE WHEN event_month_stat = 0 THEN NULL ELSE 
    (SELECT GROUP_CONCAT(event_month,'') FROM gm_event_month WHERE gm_id = gmv.gm_id GROUP BY gm_id) 
    END AS months,
    
    CASE WHEN flex_mod_stat = 0 THEN NULL 
	 ELSE gfm.hrs_bef_event_closes END AS hrs_bef_event_closes,
    CASE WHEN flex_mod_stat = 0 THEN NULL 
	 ELSE gfm.places_volume END AS places_volume,
	 
	 is_used
	 
FROM gm_settings_view gmv
LEFT JOIN gm_nshow_psubs gnp USING (gm_id)
LEFT JOIN gm_res_psubs grp USING (gm_id)
LEFT JOIN gm_flex_mod gfm USING (gm_id)

GROUP BY gm_id;

--
-- Structure for view `gm_raw_subsettings_view_for_bo`
-- Raw sub settings data, from actual tables, no validation/unfiltered
--

CREATE VIEW gm_raw_subsettings_view_for_bo AS
SELECT gm_id,
gnp.max_noshow, gnp.nshow_period ,
grp.max_res, grp.res_period_type,

(SELECT GROUP_CONCAT(event_cat,'') FROM gm_event_cat WHERE gm_id = gm.gm_id GROUP BY gm_id) AS event_cats,

(SELECT GROUP_CONCAT(event_location,'') FROM gm_event_loc WHERE gm_id = gm.gm_id GROUP BY gm_id) AS locations,

(SELECT GROUP_CONCAT(event_month,'') FROM gm_event_month WHERE gm_id = gm.gm_id GROUP BY gm_id) AS months,
    
gfm.hrs_bef_event_closes, gfm.places_volume

FROM global_moderation gm
LEFT JOIN gm_nshow_psubs gnp USING (gm_id)
LEFT JOIN gm_res_psubs grp USING (gm_id)
LEFT JOIN gm_flex_mod gfm USING (gm_id)

GROUP BY gm_id;

--
-- Structure for view `gm_events_affected_by_latest_mod_settings_view`
-- List of events affected by current global moderation settings 
-- But settings are not yet applied to them
--

CREATE VIEW gm_evscheds_affected_by_latest_mod_settings_view AS
SELECT 
(SELECT MAX(gm_id) 
    FROM gm_settings_view
) latest_gm_sett,

evsched.event_schedule_id, 
ev.event_id, ev.title, 

ev.event_type_id event_cat,
(CASE 
    WHEN ev.use_different_address_per_date = 0 THEN ev.city_location
    ELSE evsched.city_location
END) AS event_loc,

DATE_FORMAT(evsched.start_date_time, '%c') event_month

FROM event_schedule evsched
INNER JOIN event ev ON (ev.event_id = evsched.event_id AND ev.event_category IN ('REGULAR_EVENT'))
HAVING 
event_cat IN (
	SELECT event_cat
   FROM gm_subsettings_view
   WHERE event_cats REGEXP CONCAT('[[:<:]]',event_cat,'[[:>:]]')
   AND gm_id = latest_gm_sett
) 
OR
event_loc IN (
	SELECT event_loc
   FROM gm_subsettings_view
   WHERE locations REGEXP CONCAT('[[:<:]]',event_loc,'[[:>:]]')
   AND gm_id = latest_gm_sett
)
OR
event_month IN (
	SELECT event_month
   FROM gm_subsettings_view
   WHERE months REGEXP CONCAT('[[:<:]]',event_month,'[[:>:]]')
   AND gm_id = latest_gm_sett
) 
