<?php namespace MeowCrew\CancellationOffers\SurveyAnswers;

use Exception;
use MeowCrew\CancellationOffers\Core\ServiceContainerTrait;
use WP_Error;

class SurveyAnswersRepository {
	
	use ServiceContainerTrait;
	
	/**
	 * Get feedbacks
	 *
	 * @param  int  $offset
	 * @param  int  $limit
	 * @param  string  $where
	 *
	 * @return SurveyAnswer[]
	 * @throws Exception
	 */
	public static function get(
		int $offset = 0,
		int $limit = 10,
		string $where = ''
	): array {
		global $wpdb;
		
		// because phpcs has too many checking for wpdb;
		$database = $wpdb;
		
		$tableName = SurveyAnswersTable::getTableName();
		$where     = ' WHERE 1=1 ' . $where;
		
		$feedbacks = $database->get_results( $database->prepare( "SELECT * FROM {$tableName} {$where} ORDER BY `date_created` DESC LIMIT %d OFFSET %d",
			$limit, $offset ), ARRAY_A );
		
		if ( $feedbacks instanceof WP_Error ) {
			throw new Exception( 'Feedbacks error' );
		}
		
		return array_filter( array_map( function ( $feedback ) {
			try {
				return SurveyAnswer::buildFromArray( $feedback );
			} catch ( Exception $e ) {
				return null;
			}
		}, $feedbacks ) );
	}
	
	public static function getWhere( $where ): array {
		global $wpdb;
		
		// because phpcs has too many checking for wpdb;
		$database = $wpdb;
		
		$tableName = SurveyAnswersTable::getTableName();
		$feedbacks = $database->get_results( "SELECT * FROM {$tableName} {$where} ", ARRAY_A );
		
		if ( $feedbacks instanceof WP_Error ) {
			throw new Exception( 'Feedbacks error' );
		}
		
		return array_filter( array_map( function ( $feedback ) {
			try {
				return SurveyAnswer::buildFromArray( $feedback );
			} catch ( Exception $e ) {
				return null;
			}
		}, $feedbacks ) );
	}
	
	public static function getById( $feedbackId ) {
		return self::getByColumn( 'id', $feedbackId );
	}
	
	public static function getByColumn( $column, $value ) {
		global $wpdb;
		// because phpcs has too many checking for wpdb;
		$database = $wpdb;
		
		$rawFeedback = $database->get_row( $database->prepare( 'SELECT * FROM ' . SurveyAnswersTable::getTableName() . ' WHERE ' . $column . ' = %d',
			$value ), ARRAY_A );
		
		if ( $rawFeedback ) {
			try {
				return SurveyAnswer::buildFromArray( $rawFeedback );
			} catch ( Exception $e ) {
				return null;
			}
		}
		
		return null;
	}
	
	public static function getUnseenSurveyAnswersCount(): int {
		global $wpdb;
		// because phpcs has too many checking for wpdb;
		$database = $wpdb;
		
		return (int) $database->get_var( 'SELECT COUNT(*) FROM ' . SurveyAnswersTable::getTableName() . ' WHERE is_seen = 0' );
	}
	
	public static function getAllSurveyAnswersCount( $offerId = null, $retainStatus = null ): int {
		
		global $wpdb;
		$database = $wpdb;
		
		$where = 'WHERE 1=1 ';
		
		if ( $offerId ) {
			$where .= $wpdb->prepare( ' AND offer_id = %d', $offerId );
		}
		
		if ( $retainStatus ) {
			$where .= $wpdb->prepare( ' AND is_discount_offer_accepted = %d', 'retained' === $retainStatus ? 1 : 0 );
		}
		
		$query = $wpdb->prepare( 'SELECT COUNT(*) FROM %i', SurveyAnswersTable::getTableName() ) . $where;
		
		return (int) $database->get_var( $query );
	}
	
	
	public static function markAllAsSeen( array $ids = array() ) {
		global $wpdb;
		// because phpcs has too many checking for wpdb;
		$database = $wpdb;
		
		$idsWhere = '';
		
		if ( ! empty( $ids ) ) {
			$idsWhere = ' WHERE id IN (' . implode( ',', array_map( 'absint', $ids ) ) . ')';
		}
		
		$database->query( 'UPDATE ' . SurveyAnswersTable::getTableName() . ' SET is_seen = 1 ' . $idsWhere );
		
	}
	
	public static function bulkDelete( array $feedbacksIds ) {
		global $wpdb;
		// because phpcs has too many checking for wpdb;
		$database = $wpdb;
		
		$ids = implode( ',', array_map( 'absint', $feedbacksIds ) );
		$database->query( 'DELETE FROM ' . SurveyAnswersTable::getTableName() . " WHERE ID IN($ids)" );
	}
}
