<?php
namespace controls;

class FacebookAds
{
  static private function sanitize_level( $level )
  {
    $level = strtolower( trim( (string) $level ) );
    if ( in_array( $level, [ 'campaign', 'adset', 'ad' ], true ) )
    {
      return $level;
    }

    return 'campaign';
  }

  static public function main_view()
  {
    return \Tpl::view( 'facebook_ads/main_view', [
      'clients' => \factory\FacebookAds::get_clients_for_reports(),
    ] );
  }

  static public function get_entities()
  {
    $client_id = (int) \S::get( 'client_id' );
    $level = self::sanitize_level( \S::get( 'level' ) );
    $campaign_id = (int) \S::get( 'campaign_id' );
    $ad_set_id = (int) \S::get( 'ad_set_id' );

    echo json_encode( [
      'level' => $level,
      'entities' => \factory\FacebookAds::get_entities_with_latest_metrics( $client_id, $level, [
        'campaign_id' => $campaign_id,
        'ad_set_id' => $ad_set_id
      ] )
    ] );
    exit;
  }

  static public function get_history_data_table()
  {
    $level = self::sanitize_level( \S::get( 'level' ) );
    $entity_id = (int) \S::get( 'entity_id' );
    $start = (int) \S::get( 'start' );
    $length = (int) \S::get( 'length' );

    $rows = \factory\FacebookAds::get_entity_history( $level, $entity_id, $start, $length, false );
    $records_total = \factory\FacebookAds::get_entity_history_total( $level, $entity_id );

    $result = [
      'draw' => \S::get( 'draw' ),
      'recordsTotal' => $records_total,
      'recordsFiltered' => $records_total,
      'data' => []
    ];

    $is_campaign = $level === 'campaign';

    foreach ( $rows as $row )
    {
      $result['data'][] = [
        (string) ( $row['date_add'] ?? '' ),
        number_format( (float) ( $row['spend'] ?? 0 ), 2, ',', ' ' ),
        (int) ( $row['impressions'] ?? 0 ),
        (int) ( $row['clicks'] ?? 0 ),
        number_format( (float) ( $row['ctr'] ?? 0 ), 3, ',', ' ' ),
        number_format( (float) ( $row['cpc'] ?? 0 ), 3, ',', ' ' ),
        number_format( (float) ( $row['conversion_value'] ?? 0 ), 2, ',', ' ' ),
        number_format( (float) ( $row['roas'] ?? 0 ), 2, ',', ' ' ),
        $is_campaign ? number_format( (float) ( $row['roas_all_time'] ?? 0 ), 2, ',', ' ' ) : ''
      ];
    }

    $result['is_campaign'] = $is_campaign;

    echo json_encode( $result );
    exit;
  }

  static public function get_history_data_chart()
  {
    $level = self::sanitize_level( \S::get( 'level' ) );
    $entity_id = (int) \S::get( 'entity_id' );

    $rows = \factory\FacebookAds::get_entity_history( $level, $entity_id, 0, 1000, true );

    $is_campaign = $level === 'campaign';

    $dates = [];
    $spend = [];
    $impressions = [];
    $clicks = [];
    $ctr = [];
    $cpc = [];
    $conversion_value = [];
    $roas = [];
    $roas_all_time = [];

    foreach ( $rows as $row )
    {
      $dates[] = (string) ( $row['date_add'] ?? '' );
      $spend[] = (float) ( $row['spend'] ?? 0 );
      $impressions[] = (int) ( $row['impressions'] ?? 0 );
      $clicks[] = (int) ( $row['clicks'] ?? 0 );
      $ctr[] = (float) ( $row['ctr'] ?? 0 );
      $cpc[] = (float) ( $row['cpc'] ?? 0 );
      $conversion_value[] = (float) ( $row['conversion_value'] ?? 0 );
      $roas[] = (float) ( $row['roas'] ?? 0 );
      if ( $is_campaign )
      {
        $roas_all_time[] = (float) ( $row['roas_all_time'] ?? 0 );
      }
    }

    $chart_data = [
      [ 'name' => 'Spend', 'data' => $spend, 'visible' => false ],
      [ 'name' => 'Impressions', 'data' => $impressions, 'visible' => false ],
      [ 'name' => 'Clicks', 'data' => $clicks, 'visible' => false ],
      [ 'name' => 'CTR', 'data' => $ctr, 'visible' => false ],
      [ 'name' => 'CPC', 'data' => $cpc, 'visible' => false ],
      [ 'name' => 'Wartosc konwersji', 'data' => $conversion_value, 'visible' => false ],
      [ 'name' => 'ROAS (30 dni)', 'data' => $roas, 'visible' => true ]
    ];

    if ( $is_campaign )
    {
      $chart_data[] = [ 'name' => 'ROAS (all time)', 'data' => $roas_all_time, 'visible' => true ];
    }

    echo json_encode( [
      'dates' => $dates,
      'chart_data' => $chart_data
    ] );
    exit;
  }
}
