DROP VIEW IF EXISTS verified_authors_view_es;
CREATE VIEW verified_authors_view_es(
    id,
    user_id,
    first_name,
    gender,
    city,
    cv,
    payment_method,
    ranking,
    author_status,
    birth_country_id,
    edu_level_id,
    specialization_id,
    avg_test,
    native_language,
    date_start,
    author_experiences,
    editor_first_name,
    editor_last_name,
    puntuality_rate,
    attitude_rate,
    quality_rate,
    total_rate,
    total_article_scoring,
    alias,
    author_status_id,
    lang_id,
    checking_user_id,
    payment_transferwise_email,
    email
) as
WITH cte_puntuality_rate as (
	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
), cte_attitude_rate as (
    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
  ), cte_quality_rate as (
  	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
  ), cte_total_article_scoring_author_view as (
  Select
            COUNT(sc.id) as total_scoring, sc.user_id 
        from scoring as sc 
            where sc.participant_type = 1 and sc.scoring_type = 1 and sc.scoring > 0
        GROUP by sc.user_id
  )
SELECT
        authors_view.id, authors_view.user_id, 
        authors_view.first_name, authors_view.gender,
        authors_view.city, authors_view.cv, 
        authors_view.payment_method, authors_view.ranking, 
        authors_view.author_status,
        authors_view.birth_country_id, authors_view.edu_level_id, 
        authors_view.specialization_id, 
        authors_view.avg_test,
        languages.language AS native_language,
        user.created_at AS date_start,
        experience.experience AS author_experiences,
        editors.first_name AS editor_first_name,
        editors.last_name AS editor_last_name,
        COALESCE (cte_puntuality_rate.rate, 0) AS puntuality_rate,
        COALESCE (cte_attitude_rate.rate, 0) AS attitude_rate,
        COALESCE (cte_quality_rate.rate,0) AS quality_rate,
        (COALESCE(cte_puntuality_rate.rate, 0) + COALESCE(cte_attitude_rate.rate, 0) + COALESCE(cte_quality_rate.rate, 0)) AS total_rate,
        COALESCE(cte_total_article_scoring_author_view.total_scoring, 0) AS total_article_scoring,
        userInfo.alias,
        authorInfo.author_status_id,
        user_lang.lang_id,
        tests.checking_user_id,
        authors_view.payment_transferwise_email,
        authors_view.email
    FROM
        authors_view
    LEFT JOIN countries ON authors_view.birth_country_id = countries.id
    LEFT JOIN user userInfo ON
        authors_view.user_id = userInfo.id
    LEFT JOIN user_lang ON userInfo.id = user_lang.user_id
    LEFT JOIN languages ON user_lang.lang_id = languages.id
    LEFT JOIN author_experience ON authors_view.id = author_experience.author_id
    LEFT JOIN experience ON author_experience.experience_id = experience.id
    LEFT JOIN tests ON authors_view.id = tests.author_id
    LEFT JOIN user ON tests.checking_user_id = user.id
    LEFT JOIN editors ON user.id = editors.user_id
    LEFT JOIN authors authorInfo ON authors_view.id = authorInfo.id
    LEFT JOIN cte_puntuality_rate ON (authors_view.user_id = cte_puntuality_rate.user_id) AND (cte_puntuality_rate.participant_type = 1)
    LEFT JOIN cte_attitude_rate ON (authors_view.user_id = cte_attitude_rate.user_id) AND (cte_attitude_rate.participant_type = 1)
    LEFT JOIN cte_quality_rate ON (authors_view.user_id = cte_quality_rate.user_id) AND (cte_quality_rate.participant_type = 1)
    LEFT JOIN cte_total_article_scoring_author_view ON cte_total_article_scoring_author_view.user_id = userInfo.id
    WHERE
        authorInfo.author_status_id = 5
    GROUP BY
        id,
        authorInfo.id;