# frozen_string_literal: true

module Activities
  class CostsCalculation
    def compute_costs(activity, campaign)
      activity_costs = ActiveRecord::Base.connection.execute <<~SQL
        SELECT
          activity_id,
          activity_productions_campaign_id,
          campaigns_interventions_campaign_id,
          ROUND(SUM(inputs), 2) AS inputs,
          ROUND(SUM(doers), 2) AS doers,
          ROUND(SUM(tools), 2) AS tools,
          ROUND(SUM(receptions), 2) AS receptions,
          ROUND(SUM(total), 2) AS total
        FROM (
          SELECT DISTINCT
            activity_productions.id,
            activity_productions.activity_id AS activity_id,
            activity_productions_campaigns.campaign_id AS activity_productions_campaign_id,
            campaigns_interventions.campaign_id AS campaigns_interventions_campaign_id,
            SUM(intervention_costings.inputs_cost * intervention_targets.imputation_ratio) AS inputs,
            SUM(intervention_costings.doers_cost * intervention_targets.imputation_ratio) AS doers,
            SUM(intervention_costings.tools_cost * intervention_targets.imputation_ratio) AS tools,
            SUM(intervention_costings.receptions_cost * intervention_targets.imputation_ratio) AS receptions,
            SUM((intervention_costings.inputs_cost + intervention_costings.doers_cost + intervention_costings.tools_cost + intervention_costings.receptions_cost) * intervention_targets.imputation_ratio) AS total
          FROM
            activity_productions
          INNER JOIN products ON products.activity_production_id = activity_productions.id
          INNER JOIN intervention_parameters AS intervention_targets
            ON intervention_targets.product_id = products.id
            AND intervention_targets.type = 'InterventionTarget'
          INNER JOIN interventions ON interventions.id = intervention_targets.intervention_id
          INNER JOIN campaigns_interventions ON campaigns_interventions.intervention_id = interventions.id
          INNER JOIN intervention_costings ON interventions.costing_id = intervention_costings.id
          INNER JOIN activity_productions_campaigns
            ON activity_productions_campaigns.activity_production_id = activity_productions.id
          -- only with interventions with nature record and state is not rejected
          WHERE interventions.state != 'rejected'
          AND interventions.nature = 'record'
          GROUP BY activity_productions.id, activity_productions_campaigns.campaign_id, campaigns_interventions.campaign_id
        ) subquery
        WHERE activity_id = #{activity.id}
        AND activity_productions_campaign_id = #{campaign.id}
        AND campaigns_interventions_campaign_id = #{campaign.id}
        GROUP BY activity_id, activity_productions_campaign_id,campaigns_interventions_campaign_id ;
      SQL

      return { inputs: 0.0, doers: 0.0, tools: 0.0, receptions: 0.0, total: 0.0 } if activity_costs.values.empty?

      activity_costs.first.symbolize_keys
        .except(:id, :activity_id, :activity_productions_campaign_id, :campaigns_interventions_campaign_id)
        .transform_values(&:to_f)
    end
  end
end
