<?php
namespace factory;

class CampaignAlerts
{
  static public function get_alerts_count( $client_id = 0 )
  {
    global $mdb;

    $where = [];
    $client_id = (int) $client_id;

    if ( $client_id > 0 )
    {
      $where['client_id'] = $client_id;
    }

    return (int) $mdb -> count( 'campaign_alerts', $where );
  }

  static public function get_unseen_count()
  {
    global $mdb;

    return (int) $mdb -> count( 'campaign_alerts', [ 'unseen' => 1 ] );
  }

  static public function mark_all_seen()
  {
    global $mdb;

    $mdb -> update( 'campaign_alerts', [ 'unseen' => 0 ], [ 'unseen' => 1 ] );
  }

  static public function get_clients()
  {
    global $mdb;

    return $mdb -> select( 'clients', [ 'id', 'name' ], [
      'deleted' => 0,
      'ORDER' => [ 'name' => 'ASC' ]
    ] );
  }

  static public function get_alerts( $client_id = 0, $limit = 15, $offset = 0 )
  {
    global $mdb;

    $client_id = (int) $client_id;
    $limit = max( 1, (int) $limit );
    $offset = max( 0, (int) $offset );

    $sql = 'SELECT
              ca.id,
              ca.client_id,
              ca.campaign_id,
              ca.campaign_external_id,
              ca.ad_group_id,
              ca.ad_group_external_id,
              ca.product_id,
              ca.alert_type,
              ca.message,
              ca.meta_json,
              ca.date_detected,
              ca.date_add AS date_created,
              cl.name AS client_name,
              c.campaign_name,
              ag.ad_group_name
            FROM campaign_alerts AS ca
            LEFT JOIN clients AS cl ON cl.id = ca.client_id
            LEFT JOIN campaigns AS c ON c.id = ca.campaign_id
            LEFT JOIN campaign_ad_groups AS ag ON ag.id = ca.ad_group_id';

    if ( $client_id > 0 )
    {
      $sql .= ' WHERE ca.client_id = :client_id';
    }

    $sql .= ' ORDER BY ca.date_detected DESC, ca.id DESC LIMIT ' . $offset . ', ' . $limit;

    $stmt = $mdb -> pdo -> prepare( $sql );

    if ( !$stmt )
    {
      return [];
    }

    if ( $client_id > 0 )
    {
      $stmt -> bindValue( ':client_id', $client_id, \PDO::PARAM_INT );
    }

    $ok = $stmt -> execute();

    if ( !$ok )
    {
      return [];
    }

    $rows = $stmt -> fetchAll( \PDO::FETCH_ASSOC );

    return is_array( $rows ) ? $rows : [];
  }

  static public function delete_old_alerts( $days = 30 )
  {
    global $mdb;

    $mdb -> delete( 'campaign_alerts', [
      'date_detected[<]' => date( 'Y-m-d', strtotime( '-' . (int) $days . ' days' ) )
    ] );
  }

  static public function delete_alerts( array $ids )
  {
    global $mdb;

    $ids = array_map( 'intval', $ids );
    $ids = array_filter( $ids, function( $id ) { return $id > 0; } );

    if ( empty( $ids ) )
    {
      return 0;
    }

    $before = (int) $mdb -> count( 'campaign_alerts', [ 'id' => $ids ] );
    if ( $before <= 0 )
    {
      return 0;
    }

    $mdb -> delete( 'campaign_alerts', [ 'id' => $ids ] );

    $after = (int) $mdb -> count( 'campaign_alerts', [ 'id' => $ids ] );

    return max( 0, $before - $after );
  }
}
