{{
  config(
    materialized = 'view'
  )
}}

WITH source AS (
    SELECT * FROM {{ ref('int_dim_clip') }}
)

, public_clip_data AS (
    SELECT DISTINCT
        clip_id
        , true AS is_public
    FROM {{ source('rds', 'rds_public_clip') }}
)

SELECT
    source.clip_id
    , source.p_date
    , source.p_hour
    , source.created_at
    , source.user_id
    , source.request_id
    , source.s3_id
    , source.status
    , source.platform
    , source.display_index
    , source.initial_title
    , source.time_used_sec
    , source.type
    , source.task
    , source.priority
    , source.duration_sec
    , source.is_refund_credits
    , source.is_bot
    , source.credit_cost
    , source.lyrics_at_gen
    , source.simple_prompt
    , source.lyrics_final
    , source.is_simple
    , source.is_make_instrumental
    , source.style_tags
    , source.negative_style_tags
    , source.request_models
    , source.model_name
    , source.experiment_inf_param
    , source.experiment_version
    , source.history
    , source.concat_history
    , source.error_type
    , source.error_message
    , source.upload_has_vocal
    , source.edit_session_id
    , source.edited_clip_id
    , source.task_details
    , source.creation_source
    , source.augmented_tags
    , source.free_quota_category
    , source.lyrics_language
    , source.simple_lyrics_model
    , source.created_session_id
    , source.is_audio_watermarked
    , source.user_tier
    , source.is_deleted
    , source.create_mode
    , source.combined_from_clip_ids
    , source.is_audio_watermark
    , source.clip_uid
    , source.metadata
    , source.web_client_pathname
    -- Public flag from rds_public_clip (join on clip_id only, not p_date/p_hour since rds_public_clip partitions by export time)
    , Coalesce(public_clip.is_public, false) AS is_public

    -- categorizing clip better
    , CASE
        WHEN source.create_mode IS NOT null THEN source.create_mode
        WHEN source.is_simple THEN 'simple'
        ELSE 'custom'
    END AS create_mode_cleaned
    , CASE
        WHEN source.model_name LIKE '%v3p5%' OR source.model_name LIKE '%v3-5%' THEN 'v3.5'
        WHEN source.model_name LIKE '%v4%' OR source.model_name LIKE '%chirp-up%' THEN 'v4'
        WHEN source.model_name LIKE '%auk%' OR source.model_name LIKE '%ahi%' THEN 'v4.5'
        WHEN source.model_name LIKE '%bluejay%' OR source.model_name LIKE '%bass%' THEN 'v4.5+'
        WHEN source.model_name LIKE '%crow%' OR source.model_name LIKE '%carp%' THEN 'v5'
        WHEN source.model_name LIKE '%stem%' THEN 'stem'
        WHEN source.model_name LIKE '%v3%' OR source.model_name LIKE '%v2%' THEN 'older_model'
        WHEN source.model_name = '' THEN 'no_model'
        ELSE source.model_name
    END AS model_category
    , CASE
        WHEN source.type = 'gen' AND (source.task IS null OR source.task = '') THEN 'normal_gen'
        WHEN source.task IN ('cover') THEN 'cover_gen'
        WHEN source.task IN ('extend', 'upload_extend') THEN 'extend_gen'
        WHEN source.type = 'edit_speed' THEN 'remix_edit'
        WHEN source.task = 'infill' THEN 'infill_gen'
        WHEN source.task IN ('artist_consistency', 'artist_cover', 'artist_extend') THEN 'persona_related_gen'
        WHEN source.task = 'gen_stem' THEN 'stem_gen'
        WHEN startswith(source.type, 'edit') THEN 'edit'
        WHEN source.task IN ('image_to_song', 'video_to_song') THEN 'iv2s_gen'
        WHEN source.type = 'upsample' THEN 'upsample'
        WHEN startswith(source.type, 'concat') THEN 'concat'
        WHEN source.task IN ('overpainting', 'underpainting') THEN 'over_under_painting'
        WHEN source.task = 'playlist_condition' THEN 'playlist_condition'
        ELSE coalesce(source.type, '') || coalesce(source.task, '')
    END AS category

    -- about creation process
    , array_size(parse_json(source.history)) AS history_length
    , source.edit_session_id IS NOT null AS is_made_in_edit
    , len(source.augmented_tags) > 0 AS has_augmented_tags

FROM source
LEFT JOIN public_clip_data AS public_clip
    ON source.clip_id = public_clip.clip_id
