<?php
/**
 * app/plugins/reviews/models/review.php
 *
 * This class is used for the manipulation of the reviews database table.
 *
 * @package TP Reviews
 * @version 3.0
 * @author  Joey Kimsey <Joey@thetempusproject.com>
 * @link    https://TheTempusProject.com
 * @license https://opensource.org/licenses/MIT [MIT LICENSE]
 */
namespace TheTempusProject\Models;

use TheTempusProject\Bedrock\Classes\Config;
use TheTempusProject\Bedrock\Functions\Check;
use TheTempusProject\Canary\Bin\Canary as Debug;
use TheTempusProject\Classes\DatabaseModel;
use TheTempusProject\Plugins\Reviews as Plugin;
use TheTempusProject\TheTempusProject as App;
use TheTempusProject\Canary\Classes\CustomException;

class Review extends DatabaseModel {
    public $tableName = 'reviews';
    public $databaseMatrix = [
        [ 'review_category_id', 'int', '11' ],
        [ 'title', 'varchar', '128' ],
        [ 'rating', 'int', '1' ],
        [ 'review', 'text', '' ],
        [ 'createdBy', 'int', '11' ],
        [ 'createdAt', 'int', '11' ],
        [ 'approvedAt', 'int', '11' ],
        [ 'approvedBy', 'int', '11' ],
    ];
    public $plugin;

    /**
     * The model constructor.
     */
    public function __construct() {
        parent::__construct();
        $this->plugin = new Plugin;
    }

    public function create( $title, $rating, $review, $review_category_id = '0' ) {
        if ( ! $this->plugin->checkEnabled() ) {
            Debug::info( 'Reviews are disabled in the config.' );
            return false;
        }
        $fields = [
            'title' => $title,
            'rating' => $rating,
            'review' => $review,
            'review_category_id' => $review_category_id,
            'createdAt' => time(),
            'createdBy' => App::$activeUser->ID,
        ];
        if ( ! self::$db->insert( $this->tableName, $fields ) ) {
            Debug::info( 'Reviews::create - failed to insert to db' );
            return false;
        }
        return self::$db->lastId();
    }

    public function update( $id, $title, $rating, $review, $review_category_id = null ) {
        if ( ! $this->plugin->checkEnabled() ) {
            Debug::info( 'Reviews are disabled in the config.' );
            return false;
        }
        $fields = [
            'title' => $title,
            'rating' => $rating,
            'review' => $review
        ];
        if ( ! empty( $review_category_id ) ) {
            $fields['review_category_id'] = $review_category_id;
        }
        if ( !self::$db->update( $this->tableName, $id, $fields ) ) {
            new CustomException( 'reviewUpdate' );
            Debug::error( "Review: $id not updated: $fields" );
            return false;
        }
        return true;
    }

    public function approve( $ID  ) {
        if ( !Check::id( $ID ) ) {
            Debug::info( 'Review Categories: illegal ID.' );
            return false;
        }
        $fields = [
            'approvedAt' => time(),
            'approvedBy' => App::$activeUser->ID,
        ];
        if ( !self::$db->update( $this->tableName, $ID, $fields ) ) {
            new CustomException( $this->tableName );
            Debug::error( "Review Categories: $ID Approved: $fields" );
            return false;
        }
        return true;
    }

    public function hide( $ID ) {
        // if ( !Check::id( $ID ) ) {
        //     Debug::info( 'Review Categories: illegal ID.' );
        //     return false;
        // }
        // $fields = [
        //     'approvedAt' => null,
        //     'approvedBy' => null,
        // ];
        // if ( !self::$db->update( $this->tableName, $ID, $fields ) ) {
        //     new CustomException( $this->tableName );
        //     Debug::error( "Review Categories: $ID not Approved: $fields" );
        //     return false;
        // }
        return true;
    }

    public function byUser( $limit = null ) {
        $whereClause = ['createdBy', '=', App::$activeUser->ID];
        if ( empty( $limit ) ) {
            $reviews = self::$db->get( $this->tableName, $whereClause );
        } else {
            $reviews = self::$db->get( $this->tableName, $whereClause, 'ID', 'DESC', [0, $limit] );
        }
        if ( !$reviews->count() ) {
            Debug::info( 'No Reviews found.' );
            return false;
        }
        return $this->filter( $reviews->results() );
    }

    public function reviewedCategoriesByUser() {
        $whereClause = ['createdBy', '=', App::$activeUser->ID];
        $reviews = self::$db->action( 'SELECT ID,review_category_id', $this->tableName, $whereClause );

        if ( !$reviews->count() ) {
            Debug::info( 'No Reviews found.' );
            return false;
        }
        $categoryIds = [];
        foreach ($reviews->results() as $key => $result) {
            if ( ! in_array( $result->review_category_id, $categoryIds ) ) {
                $categoryIds[] = $result->review_category_id;
            }
        }
        return $categoryIds;
    }

    public function byCategory( $id, $limit = null ) {
        $whereClause = [ 'review_category_id', '=', $id ];
        if ( empty( $limit ) ) {
            $reviews = self::$db->get( $this->tableName, $whereClause );
        } else {
            $reviews = self::$db->get( $this->tableName, $whereClause, 'ID', 'DESC', [0, $limit] );
        }
        if ( !$reviews->count() ) {
            Debug::info( 'No Reviews found.' );
            return false;
        }
        return $this->filter( $reviews->results() );
    }
}
