create or replace view sos.v_item as
  select
    "item".*,
    "image".image_uuid,
    "image".image_featured,
    "image".image_mime_type,
    "image".image_date_uploaded,
    "user".user_email,
    coalesce(num_added - num_removed, 0) as item_number_in_stock
  from sos."item"
    left join sos."image" on item.item_uuid = image.image_item_uuid
    left join sos."user"  on image.image_uploader_uuid = "user".user_uuid
    left join
      (
        select
          stockchange_item_uuid,
          coalesce(sum(case when stockchange_direction = 'added' then stockchange_change end)::int4, 0) as num_added,
          coalesce(sum(case when stockchange_direction = 'subtracted' then stockchange_change end)::int4, 0) as num_removed
        from sos."item_stockchange"
          group by stockchange_item_uuid
      ) stock_counts
    on stock_counts.stockchange_item_uuid = item.item_uuid;

create or replace view sos.v_cart as
  select * from sos.cart
    left join sos.cart_item on cart_item.cart_item_cart_uuid = cart.cart_uuid
    left join sos.v_item on cart_item.cart_item_item_uuid = v_item.item_uuid;

create or replace view sos.v_session as
  select
    "session".*,
    "session_user".user_email as session_user_email,
    "session_user".user_email_confirmed as session_user_email_confirmed,
    "session_user".user_password_hash as session_user_password_hash,
    "session_user".user_time_registered as session_user_time_registered,
    "session_user".user_time_email_confirmed as session_user_time_email_confirmed,
    "session_user".user_is_admin as session_user_is_admin,
    "login_link".*,
    v_cart.*
  from sos."session"
    left join sos."user" "session_user" on "session".session_user_uuid = "session_user".user_uuid
    left join sos."login_link" on "session".session_originating_link = "login_link".login_link_uuid
    left join sos.v_cart on v_cart.cart_uuid = "session".session_cart;

create or replace view sos.v_login_link as
  select
      *
    from sos."login_link"
      left join sos."user" on "login_link".login_link_user_uuid = "user".user_uuid;

create or replace view sos.v_category as
  select
    "category".*,
    "child_category".category_uuid as child_category_uuid,
    "child_category".category_name as child_category_name,
    "child_category".category_urlslug as child_category_urlslug,
    "parent_category".category_uuid as parent_category_uuid,
    "parent_category".category_name as parent_category_name,
    "parent_category".category_urlslug as parent_category_urlslug,
    v_item.*
  from sos."category"
    left join sos."category_category" "child_category_link" on "category".category_uuid = "child_category_link".category_category_parent_uuid
    left join sos."category_category" "parent_category_link" on "category".category_uuid = "parent_category_link".category_category_child_uuid
    left join sos."category" "child_category" on "child_category_link".category_category_child_uuid = "child_category".category_uuid
    left join sos."category" "parent_category" on "parent_category_link".category_category_parent_uuid = "parent_category".category_uuid
    left join sos."category_item" on "category".category_uuid = "category_item".category_item_category_uuid
    left join sos.v_item on "category_item".category_item_item_uuid = item_uuid;

create or replace view sos.v_cart_price as
  select cart_uuid,
      sum(carts.cart_item_price) as cart_price
    from (
      select cart_uuid, cart_item_uuid,
        sum(item_price_cents * cart_item_count) / count(cart_item_uuid) as cart_item_price
      from sos.v_cart
        group by cart_uuid, cart_item_uuid
    ) carts
    group by cart_uuid;


-- TODO: add coupon, delivery
create or replace view sos.v_order as
  select * from sos."order"
    left join sos."transaction" on transaction_order_uuid = order_uuid
    left join sos."coupon" on transaction_coupon_uuid = coupon_uuid
    left join sos."address" on order_address_uuid = address_uuid
    left join sos.v_cart on cart_uuid = transaction_cart_uuid;
