"""aggregate_views

Revision ID: 6c67a8bea936
Revises: 53fb4e66bf09
Create Date: 2018-10-08 14:38:30.383613

"""
from alembic import op
import sqlalchemy as sa
from sqlalchemy.dialects import postgresql

# revision identifiers, used by Alembic.
revision = '6c67a8bea936'
down_revision = '53fb4e66bf09'
branch_labels = None
depends_on = None


def upgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    create_view_statements = '''
    create view offer_summary
    as
    select 
      deal_table_id, 
      count(*) offer_count,
      sum(coalesce((offer.offer->>'investmentTarget')::numeric, 0)) investment_target,
      sum(coalesce((offer.offer->>'minimumInvestmentTarget')::numeric, 0)) minimum_investment_target,
      sum(coalesce((offer.offer->>'maximumInvestmentTarget')::numeric, 0)) maximum_investment_target
    from offer group by 1;


    create view project_summary
    as
    select 
        project.deal_table_id,
        count(*) as project_count,
        sum(asset_count) asset_count,
        sum(asset_value) asset_value
    from project 
    left join 
       (select 
          deal_table_id, 
          count(*) as asset_count, 
          sum(coalesce((asset.value->>'totalValue')::numeric, 0)) asset_value 
       from 
          project, 
          jsonb_array_elements(project->'assets') as asset group by 1
       ) assets 
       on assets.deal_table_id = project.deal_table_id 
    group by 1;


    create view grant_summary
    as
    select 
      deal_table_id, 
      count(*) grant_count,
      sum(coalesce(("grant"."grant"->>'amountCommitted')::numeric, 0)) grant_amount_committed
    from "grant" group by 1;


    create view equity_summary
    as
    select 
      deal_table_id, 
      count(*) equity_count,
      sum(coalesce(("equity"."equity"->>'value')::numeric, 0)) equity_value
    from "equity" group by 1;


    create view credit_summary
    as
    select 
      deal_table_id, 
      count(*) credit_count,
      sum(coalesce(("credit"."credit"->>'estimatedValue')::numeric, 0)) credit_estimated_value
    from "credit" group by 1;


    create view deal_summary
    as
    select 
       id deal_table_id,
       deal_id,
       collection,
       coalesce(offer_count, 0) as offer_count,
       coalesce(investment_target, 0) as investment_target,
       coalesce(minimum_investment_target, 0) as minimum_investment_target,
       coalesce(maximum_investment_target, 0) as maximum_investment_target,
       coalesce(project_count, 0) as project_count,
       coalesce(asset_count, 0) as asset_count,
       coalesce(asset_value, 0) as asset_value,
       coalesce(grant_count, 0) as grant_count,
       coalesce(grant_amount_committed, 0) as grant_amount_committed,
       coalesce(equity_count, 0) as equity_count,
       coalesce(equity_value, 0) as equity_value,
       coalesce(credit_count, 0) as credit_count,
       coalesce(credit_estimated_value, 0) as credit_estimated_value
    from deal
       left join offer_summary os on os.deal_table_id = deal.id
       left join project_summary ps on ps.deal_table_id = deal.id
       left join grant_summary gs on gs.deal_table_id = deal.id
       left join equity_summary es on es.deal_table_id = deal.id
       left join credit_summary cs on cs.deal_table_id = deal.id;


    create view collection_summary
    as
    select 
       collection,
       count(*) deal_count,
       sum(offer_count) offer_count,
       sum(investment_target) investment_target,
       sum(minimum_investment_target) minimum_investment_target,
       sum(maximum_investment_target) maximum_investment_target,
       sum(project_count) project_count,
       sum(asset_count) asset_count,
       sum(asset_value) asset_value,
       sum(grant_count) grant_count,
       sum(grant_amount_committed) grant_amount_committed,
       sum(equity_count) equity_count,
       sum(equity_value) equity_value,
       sum(credit_count) credit_count,
       sum(credit_estimated_value) credit_estimated_value
    from deal_summary
    group by 1
    '''

    op.execute(create_view_statements)
    # ### end Alembic commands ###


def downgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    drop_view_statements = '''
    drop view collection_summary;
    drop view deal_summary;
    drop view credit_summary;
    drop view equity_summary;
    drop view grant_summary;
    drop view project_summary;
    drop view offer_summary;
    '''

    op.execute(drop_view_statements)
    # ### end Alembic commands ###
