<?
namespace factory;
class Products
{
  static public function get_product_comments( $product_id )
  {
    global $mdb;
    return $mdb -> query( 'SELECT comment, date_add FROM products_comments WHERE product_id = \'' . $product_id . '\' ORDER BY date_add DESC' ) -> fetchAll( \PDO::FETCH_ASSOC );
  }

  static public function get_min_roas( $product_id )
  {
    global $mdb;
    return $mdb -> get( 'products', 'min_roas', [ 'id' => $product_id ] );
  }

  static public function get_client_bestseller_min_roas( $client_id )
  {
    global $mdb;
    return $mdb -> get( 'clients', 'bestseller_min_roas', [ 'id' => $client_id ] );
  }

  static public function save_client_bestseller_min_roas( $client_id, $min_roas )
  {
    global $mdb;
    return $mdb -> update( 'clients', [ 'bestseller_min_roas' => $min_roas ], [ 'id' => $client_id ] );
  }

  static public function save_min_roas( $product_id, $min_roas )
  {
    global $mdb;
    return $mdb -> update( 'products', [ 'min_roas' => $min_roas ], [ 'id' => $product_id ] );
  }

  static public function get_products( $client_id, $search, $limit, $start, $order_name, $order_dir )
  {
    global $mdb;

    if ( $search )
      return $mdb -> query( 'SELECT pt.*, p.offer_id, p.min_roas FROM products_temp AS pt INNER JOIN products AS p ON p.id = pt.product_id WHERE client_id = \'' . $client_id . '\' AND ( pt.name LIKE \'%' . $search . '%\' OR offer_id = ' . (int)$search . ' ) ORDER BY ' . $order_name . ' ' . $order_dir . ', id DESC LIMIT ' . $start . ', ' . $limit ) -> fetchAll();
    else
      return $mdb -> query( 'SELECT pt.*, p.offer_id, p.min_roas FROM products_temp AS pt INNER JOIN products AS p ON p.id = pt.product_id WHERE client_id = \'' . $client_id . '\' ORDER BY ' . $order_name . ' ' . $order_dir . ', id DESC LIMIT ' . $start . ', ' . $limit ) -> fetchAll();
  }

  static public function get_records_total_products( $client_id, $search )
  {
    global $mdb;

    if ( $search )
      return $mdb -> query( 'SELECT COUNT(0) FROM products_temp AS pt INNER JOIN products AS p ON p.id = pt.product_id WHERE client_id = \'' . $client_id . '\' AND  ( pt.name LIKE \'%' . $search . '%\' OR offer_id = ' . (int)$search . ' )' ) -> fetchColumn();
    else
      return $mdb -> query( 'SELECT COUNT(0) FROM products_temp AS pt INNER JOIN products AS p ON p.id = pt.product_id WHERE client_id = \'' . $client_id . '\'' ) -> fetchColumn();
  }

  static public function get_product_data( $product_id, $field )
  {
    global $mdb;
    return $mdb -> get( 'products_data', $field, [ 'product_id' => $product_id ] );
  }

  static public function set_product_data( $product_id, $field, $value )
  {
    global $mdb;

    if ( !$mdb -> count( 'products_data', [ 'product_id' => $product_id ] ) )
      $result = $mdb -> insert( 'products_data', [ 'product_id' => $product_id, $field => $value ] );
    else
      $result = $mdb -> update( 'products_data', [ $field => $value ], [ 'product_id' => $product_id ] );
    return $result;
  }

  static public function get_product_history( $client_id, $product_id, $start, $limit )
  {
    global $mdb;
    return $mdb -> query( 'SELECT * FROM products_history AS ph WHERE ph.product_id = \'' . $product_id . '\' ORDER BY ph.date_add DESC LIMIT ' . $start . ', ' . $limit ) -> fetchAll( \PDO::FETCH_ASSOC );
  }

  static public function get_records_total_product_history( $client_id, $product_id )
  {
    global $mdb;
    return $mdb -> query( 'SELECT COUNT(0) FROM products_history AS ph WHERE ph.product_id = \'' . $product_id . '\'' ) -> fetchColumn();
  }

  static public function get_product_history_30( $client_id, $product_id, $start, $limit )
  {
    global $mdb;
    return $mdb -> query( 'SELECT * FROM products_history_30 AS ph3 WHERE ph3.product_id = \'' . $product_id . '\' ORDER BY ph3.date_add ASC LIMIT ' . $start . ', ' . $limit ) -> fetchAll( \PDO::FETCH_ASSOC );
  }

  static public function get_impressions_30( $product_id )
  {
    global $mdb;
    return $mdb -> query( 'SELECT SUM(impressions) FROM products_history WHERE product_id = \'' . $product_id . '\' AND date_add >= \'' . date( 'Y-m-d', strtotime( '-30 days', time() ) ) . '\'' ) -> fetchColumn();
  }

  static public function get_clicks_30( $product_id )
  {
    global $mdb;
    return $mdb -> query( 'SELECT SUM(clicks) FROM products_history WHERE product_id = \'' . $product_id . '\' AND date_add >= \'' . date( 'Y-m-d', strtotime( '-30 days', time() ) ) . '\'' ) -> fetchColumn();
  }

  static public function add_product_comment( $product_id, $comment, $date = null )
  {
    global $mdb;

    if ( !$date )
      $date = date( 'Y-m-d' );
    else
      $date = date( 'Y-m-d', strtotime( $date ) );

    if ( $mdb -> count( 'products_comments', [ 'AND' => [ 'product_id' => $product_id, 'date_add' => $date ] ] ) )
      return $mdb -> update( 'products_comments', [ 'comment' => $comment ], [ 'AND' => [ 'product_id' => $product_id, 'date_add' => $date ] ] );
    else
      return $mdb -> insert( 'products_comments', [ 'product_id' => $product_id, 'comment' => $comment, 'date_add' => $date ] );
  }
}