DROP VIEW IF EXISTS report_editors_view;
CREATE VIEW report_editors_view (
    id,
    email,
    first_last_name,
    status,
    registration_date,
    language,
    city,
    country,
    lists,
    user_language_code,
    revisers_ranking,
    revisers_number_jobs,
    editors_ranking,
    editors_number_jobs,
    pms_ranking,
    pms_number_jobs
)
as 
SELECT
    editors_view.id,
    editors_view.email,
    editors_view.first_last_name,
	CASE WHEN editors_view.user_status = 0 THEN 'Inactive'
    	 WHEN editors_view.user_status = 10 THEN 'Accepted'
    ELSE editors_view.user_status 
    END as 'status',
    from_unixtime(user.created_at,'%Y-%m-%d') as 'registration_date',
    COALESCE(languages.language, '') AS language,
      if(editors.city is null or editors.city = '', '- -', editors.city) as city,
      if(c.country is null or c.country = '', '- -', c.country) as country,
      if(lists.name is null or lists.name = '', '- -', lists.name) as 'lists',
      if(languages.language_code is null or languages.language_code = '', '- -', languages.language_code) AS user_language_code,
    TRUNCATE(COALESCE((
        COALESCE(
            reviser_puntuality_rate.rate,
            0
        ) + COALESCE(reviser_attitude_rate.rate, 0) + COALESCE(reviser_quality_rate.rate, 0)
    ),0),2) AS revisers_ranking,
    COALESCE(reviser_total_article_view.reviserTotalArticleScoring, 0) as revisers_number_jobs,
    TRUNCATE(COALESCE((
        COALESCE(editor_puntuality_rate.rate, 0) + COALESCE(editor_attitude_rate.rate, 0) + COALESCE(editor_quality_rate.rate, 0)
    ),0),2) AS editors_ranking,
    COALESCE(editor_total_article_view.editorTotalArticleScoring,0) as editors_number_jobs,
    TRUNCATE(COALESCE((
        COALESCE(pm_puntuality_rate.rate, 0) + COALESCE(pm_attitude_rate.rate, 0) + COALESCE(pm_quality_rate.rate, 0)
    ),0),2) AS pms_ranking,
    TRUNCATE(COALESCE(pm_total_article_view.pmTotalArticleScoring,0),0) as pms_number_jobs
FROM
    editors_view
LEFT JOIN user ON editors_view.user_id = user.id
LEFT JOIN editors on editors_view.id = editors.id
LEFT JOIN user_lang ON user.id = user_lang.user_id
LEFT JOIN languages ON user_lang.lang_id = languages.id
LEFT JOIN countries as c on c.id = editors.country_id
LEFT JOIN user_lists as lu on lu.user_id = editors_view.user_id
LEFT JOIN lists on lists.id = lu.list_id
LEFT JOIN (
    select
            scoring_amount.user_id AS user_id,
            scoring_amount.participant_type AS participant_type,
            (scoring_amount.total_scoring / scoring_count.count_scoring) AS rate
        from ((
            (
                select sc.user_id, sc.participant_type, sc.scoring_type, sc.total_scoring 
                    from sum_scoring_puntuality_rate_view as sc
                    
            )
            )
            scoring_amount join (
                                select sc.user_id, sc.participant_type, sc.scoring_type, sc.count_scoring 
                                from count_scoring_puntuality_rate_view as sc
                            )
            scoring_count on(
                                    (
                                        (scoring_amount.user_id = scoring_count.user_id) 
                                        and (scoring_amount.participant_type = scoring_count.participant_type) 
                                        and (scoring_amount.scoring_type = scoring_count.scoring_type)
                                    )
                                )
            )
            where scoring_amount.scoring_type = 1
) as reviser_puntuality_rate ON (editors_view.user_id = reviser_puntuality_rate.user_id) AND (reviser_puntuality_rate.participant_type = 6)
LEFT JOIN (
    select 
            scoring_amount.user_id AS user_id,
            scoring_amount.participant_type AS participant_type,
            (scoring_amount.total_scoring / scoring_count.count_scoring) AS rate
        from ((
            (
                select sc.user_id ,sc.participant_type ,sc.scoring_type, sc.total_scoring 
                    from sum_scoring_attitude_rate_view	as sc
            )
            )
            scoring_amount join (
                                select sc.user_id, sc.participant_type, sc.scoring_type, sc.count_scoring 
                                from count_scoring_attitude_rate_view sc
                            )
            scoring_count on(
                                    (
                                        (scoring_amount.user_id = scoring_count.user_id) 
                                        and (scoring_amount.participant_type = scoring_count.participant_type) 
                                        and (scoring_amount.scoring_type = scoring_count.scoring_type)
                                    )
                                )
            )
        where scoring_amount.scoring_type = 2
) as reviser_attitude_rate ON (editors_view.user_id = reviser_attitude_rate.user_id) AND (reviser_attitude_rate.participant_type = 6)
LEFT JOIN (
        select 
            scoring_amount.user_id AS user_id,
            scoring_amount.participant_type AS participant_type,
            (scoring_amount.total_scoring / scoring_count.count_scoring) AS rate
        from ((
            (
                select sc.user_id ,sc.participant_type ,sc.scoring_type, sc.total_scoring 
                    from sum_scoring_quality_rate_view sc 
                    
            )
            )
            scoring_amount join (
                                select sc.user_id, sc.participant_type, sc.scoring_type, sc.count_scoring 
                                from count_scoring_quality_rate_view sc
                            )
            scoring_count on(
                                    (
                                        (scoring_amount.user_id = scoring_count.user_id) 
                                        and (scoring_amount.participant_type = scoring_count.participant_type) 
                                        and (scoring_amount.scoring_type = scoring_count.scoring_type)
                                    )
                                )
            )
        where scoring_amount.scoring_type = 3
) as reviser_quality_rate ON (editors_view.user_id = reviser_quality_rate.user_id) AND (reviser_quality_rate.participant_type = 6)
LEFT JOIN (
    select
            scoring_amount.user_id AS user_id,
            scoring_amount.participant_type AS participant_type,
            (scoring_amount.total_scoring / scoring_count.count_scoring) AS rate
        from ((
            (
                select sc.user_id, sc.participant_type, sc.scoring_type, sc.total_scoring 
                    from sum_scoring_puntuality_rate_view as sc
                    
            )
            )
            scoring_amount join (
                                select sc.user_id, sc.participant_type, sc.scoring_type, sc.count_scoring 
                                from count_scoring_puntuality_rate_view as sc
                            )
            scoring_count on(
                                    (
                                        (scoring_amount.user_id = scoring_count.user_id) 
                                        and (scoring_amount.participant_type = scoring_count.participant_type) 
                                        and (scoring_amount.scoring_type = scoring_count.scoring_type)
                                    )
                                )
            )
            where scoring_amount.scoring_type = 1
) as editor_puntuality_rate ON (editors_view.user_id = editor_puntuality_rate.user_id) AND (editor_puntuality_rate.participant_type = 2)
LEFT JOIN (
     select 
            scoring_amount.user_id AS user_id,
            scoring_amount.participant_type AS participant_type,
            (scoring_amount.total_scoring / scoring_count.count_scoring) AS rate
        from ((
            (
                select sc.user_id ,sc.participant_type ,sc.scoring_type, sc.total_scoring 
                    from sum_scoring_attitude_rate_view	as sc
            )
            )
            scoring_amount join (
                                select sc.user_id, sc.participant_type, sc.scoring_type, sc.count_scoring 
                                from count_scoring_attitude_rate_view sc
                            )
            scoring_count on(
                                    (
                                        (scoring_amount.user_id = scoring_count.user_id) 
                                        and (scoring_amount.participant_type = scoring_count.participant_type) 
                                        and (scoring_amount.scoring_type = scoring_count.scoring_type)
                                    )
                                )
            )
        where scoring_amount.scoring_type = 2
) as editor_attitude_rate ON (editors_view.user_id = editor_attitude_rate.user_id) AND (editor_attitude_rate.participant_type = 2)
LEFT JOIN (
    select 
            scoring_amount.user_id AS user_id,
            scoring_amount.participant_type AS participant_type,
            (scoring_amount.total_scoring / scoring_count.count_scoring) AS rate
        from ((
            (
                select sc.user_id ,sc.participant_type ,sc.scoring_type, sc.total_scoring 
                    from sum_scoring_quality_rate_view sc 
                    
            )
            )
            scoring_amount join (
                                select sc.user_id, sc.participant_type, sc.scoring_type, sc.count_scoring 
                                from count_scoring_quality_rate_view sc
                            )
            scoring_count on(
                                    (
                                        (scoring_amount.user_id = scoring_count.user_id) 
                                        and (scoring_amount.participant_type = scoring_count.participant_type) 
                                        and (scoring_amount.scoring_type = scoring_count.scoring_type)
                                    )
                                )
            )
        where scoring_amount.scoring_type = 3
) as editor_quality_rate ON (editors_view.user_id = editor_quality_rate.user_id) AND (editor_quality_rate.participant_type = 2)
LEFT JOIN (
    select
            scoring_amount.user_id AS user_id,
            scoring_amount.participant_type AS participant_type,
            (scoring_amount.total_scoring / scoring_count.count_scoring) AS rate
        from ((
            (
                select sc.user_id, sc.participant_type, sc.scoring_type, sc.total_scoring 
                    from sum_scoring_puntuality_rate_view as sc
                    
            )
            )
            scoring_amount join (
                                select sc.user_id, sc.participant_type, sc.scoring_type, sc.count_scoring 
                                from count_scoring_puntuality_rate_view as sc
                            )
            scoring_count on(
                                    (
                                        (scoring_amount.user_id = scoring_count.user_id) 
                                        and (scoring_amount.participant_type = scoring_count.participant_type) 
                                        and (scoring_amount.scoring_type = scoring_count.scoring_type)
                                    )
                                )
            )
            where scoring_amount.scoring_type = 1
) as pm_puntuality_rate ON (editors_view.user_id = pm_puntuality_rate.user_id) AND (pm_puntuality_rate.participant_type = 5)
LEFT JOIN (
     select 
            scoring_amount.user_id AS user_id,
            scoring_amount.participant_type AS participant_type,
            (scoring_amount.total_scoring / scoring_count.count_scoring) AS rate
        from ((
            (
                select sc.user_id ,sc.participant_type ,sc.scoring_type, sc.total_scoring 
                    from sum_scoring_attitude_rate_view	as sc
            )
            )
            scoring_amount join (
                                select sc.user_id, sc.participant_type, sc.scoring_type, sc.count_scoring 
                                from count_scoring_attitude_rate_view sc
                            )
            scoring_count on(
                                    (
                                        (scoring_amount.user_id = scoring_count.user_id) 
                                        and (scoring_amount.participant_type = scoring_count.participant_type) 
                                        and (scoring_amount.scoring_type = scoring_count.scoring_type)
                                    )
                                )
            )
        where scoring_amount.scoring_type = 2
) as pm_attitude_rate ON (editors_view.user_id = pm_attitude_rate.user_id) AND (pm_attitude_rate.participant_type = 5)
LEFT JOIN (
    select 
            scoring_amount.user_id AS user_id,
            scoring_amount.participant_type AS participant_type,
            (scoring_amount.total_scoring / scoring_count.count_scoring) AS rate
        from ((
            (
                select sc.user_id ,sc.participant_type ,sc.scoring_type, sc.total_scoring 
                    from sum_scoring_quality_rate_view sc 
                    
            )
            )
            scoring_amount join (
                                select sc.user_id, sc.participant_type, sc.scoring_type, sc.count_scoring 
                                from count_scoring_quality_rate_view sc
                            )
            scoring_count on(
                                    (
                                        (scoring_amount.user_id = scoring_count.user_id) 
                                        and (scoring_amount.participant_type = scoring_count.participant_type) 
                                        and (scoring_amount.scoring_type = scoring_count.scoring_type)
                                    )
                                )
            )
        where scoring_amount.scoring_type = 3
) as pm_quality_rate ON (editors_view.user_id = pm_quality_rate.user_id) AND (pm_quality_rate.participant_type = 5)
LEFT JOIN (
    SELECT
        count(*) as reviserTotalArticleScoring,
        scoring.user_id as user_id
    FROM
        scoring
    WHERE
        scoring.participant_type = 6 AND scoring.scoring_type = 1 AND scoring.scoring > 0
    GROUP BY
        scoring.user_id
) as reviser_total_article_view ON reviser_total_article_view.user_id = user.id
LEFT JOIN (
    SELECT
        count(*) as editorTotalArticleScoring,
        scoring.user_id as user_id
    FROM
        scoring
    WHERE
        scoring.participant_type = 2 AND scoring.scoring_type = 1 AND scoring.scoring > 0
    GROUP BY
        scoring.user_id
) as editor_total_article_view ON editor_total_article_view.user_id = user.id
LEFT JOIN (
    SELECT
        count(*) as pmTotalArticleScoring,
        scoring.user_id as user_id
    FROM
        scoring
    WHERE
        scoring.participant_type = 5 AND scoring.scoring_type = 1 AND scoring.scoring > 0
    GROUP BY
        scoring.user_id
) as pm_total_article_view ON pm_total_article_view.user_id = user.id
 GROUP BY editors_view.user_id;