<?php
/**
 * app/plugins/reviews/models/review_category.php
 *
 * This class is used for the manipulation of the review_categories 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\Models\Review;
use TheTempusProject\TheTempusProject as App;
use TheTempusProject\Canary\Classes\CustomException;

class ReviewCategory extends DatabaseModel {
    public $tableName = 'review_categories';
    public $databaseMatrix = [
        [ 'name', 'varchar', '128' ],
        [ 'slug', 'varchar', '64' ],
        [ 'createdBy', 'int', '11' ],
        [ 'createdAt', 'int', '11' ],
    ];
    public $plugin;

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

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

    public function update( $id, $name, $slug ) {
        if ( ! $this->plugin->checkEnabled() ) {
            Debug::info( 'Reviews are disabled in the config.' );
            return false;
        }
        if ( !Check::dataTitle( $slug ) ) {
            Debug::info( 'Review Categories: illegal title.' );
            return false;
        }
        $fields = [
            'name' => $name,
            'slug' => $slug,
        ];
        if ( !self::$db->update( $this->tableName, $id, $fields ) ) {
            new CustomException( 'reviewCategoryUpdate' );
            Debug::error( "Review Categories: $id not updated: $fields" );
            return false;
        }
        return true;
    }

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

    public function simple() {
        $categories = self::$db->get( $this->tableName, '*' );
        if ( !$categories->count() ) {
            Debug::warn( 'Could not find any categories' );
            return false;
        }
        
        $categories = $categories->results();
        $out = [];
        foreach ( $categories as &$category ) {
            $out[ $category->name ] = $category->ID;
        }
        return $out;
    }

    public function simpleUnreviewed() {
        $categories = self::$db->get( $this->tableName, '*' );
        if ( !$categories->count() ) {
            Debug::warn( 'Could not find any categories' );
            return false;
        }
        
        $categories = $categories->results();

        $review = new Review;

        $reviews = $review->reviewedCategoriesByUser();

        $out = [];
        foreach ( $categories as &$category ) {
            
            if ( ! empty( $reviews) && in_array( $category->ID, $reviews ) ) {
                continue;
            }

            $out[ $category->name ] = $category->ID;
        }
        return $out;
    }
}
