DROP VIEW IF EXISTS offer_translators_view_es;
CREATE VIEW offer_translators_view_es(
    id,
    user_id,
    offer_status,
    participant_type,
    partitipant_name,
    offer_id,
    totalRateTranslator,
    totalArticleScoringTranslator
) as
SELECT
        offer_partitipants.id,
        offer_partitipants.user_id,
        offer_partitipants.offer_status,
        offer_partitipants.participant_type,
        offer_partitipants.partitipant_name,
        offer_partitipants.offer_id,
        translators_rate_view.totalRateTranslator,
        translators_rate_view.totalArticleScoringTranslator
    FROM
        offer_partitipants
    LEFT JOIN project_offers offer ON
        offer_partitipants.offer_id = offer.id
    LEFT JOIN user ON offer_partitipants.user_id = user.id
    LEFT JOIN authors author ON
        user.id = author.user_id
    LEFT JOIN editors editor ON
        user.id = editor.user_id
    LEFT JOIN offer_deliveries offerDeliveries ON
        offer.id = offerDeliveries.offer_id
    LEFT JOIN (
        SELECT
            (
                COALESCE(puntuality_rate.rate, 0) + COALESCE(attitude_rate.rate, 0) + COALESCE(quality_rate.rate, 0)
            ) AS totalRateTranslator,
            COALESCE(
                total_article_scoring_translator_view.total_scoring,
                0
            ) AS totalArticleScoringTranslator,
            userInfo.id AS translator_id
        FROM
            translators_view
        LEFT JOIN user userInfo ON
            translators_view.user_id = userInfo.id
        LEFT JOIN scoring_puntuality_rate_view puntuality_rate ON
            (
                translators_view.user_id = puntuality_rate.user_id
            ) AND(
                puntuality_rate.participant_type = 4
            )
        LEFT JOIN scoring_attitude_rate_view attitude_rate ON
            (
                translators_view.user_id = attitude_rate.user_id
            ) AND(
                attitude_rate.participant_type = 4
            )
        LEFT JOIN scoring_quality_rate_view quality_rate ON
            (
                translators_view.user_id = quality_rate.user_id
            ) AND(
                quality_rate.participant_type = 4
            )
        LEFT JOIN total_article_scoring_translator_view ON total_article_scoring_translator_view.user_id = userInfo.id
        GROUP BY
            translators_view.id
    ) as translators_rate_view ON translators_rate_view.translator_id = user.id
    WHERE participant_type = 4
    GROUP BY
        offer_id,
        user_id;