-- Intermediate: Monthly purchase data enriched with dimensions
-- Joins purchases with time and district dimensions for analysis

with purchases as (
    select * from {{ ref('stg_trreb__purchases') }}
),

time_dim as (
    select * from {{ ref('stg_dimensions__time') }}
),

district_dim as (
    select * from {{ ref('stg_dimensions__trreb_districts') }}
),

enriched as (
    select
        p.purchase_id,

        -- Time attributes
        t.date_key,
        t.full_date,
        t.year,
        t.month,
        t.quarter,
        t.month_name,

        -- District attributes
        d.district_key,
        d.district_code,
        d.district_name,
        d.area_type,

        -- Metrics
        p.sales_count,
        p.dollar_volume,
        p.avg_price,
        p.median_price,
        p.new_listings,
        p.active_listings,
        p.days_on_market,
        p.sale_to_list_ratio,

        -- Calculated metrics
        case
            when p.active_listings > 0
            then round(p.sales_count::numeric / p.active_listings, 3)
            else null
        end as absorption_rate,

        case
            when p.sales_count > 0
            then round(p.active_listings::numeric / p.sales_count, 1)
            else null
        end as months_of_inventory

    from purchases p
    inner join time_dim t on p.date_key = t.date_key
    inner join district_dim d on p.district_key = d.district_key
)

select * from enriched
