<?php
/**
 * app/plugins/bugreport/models/bugreport.php
 *
 * This class is used for the manipulation of the bugreports database table.
 *
 * @package TP BugReports
 * @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\Functions\Check;
use TheTempusProject\Canary\Bin\Canary as Debug;
use TheTempusProject\Canary\Classes\CustomException;
use TheTempusProject\Classes\DatabaseModel;
use TheTempusProject\Plugins\Bugreport as Plugin;
use TheTempusProject\TheTempusProject as App;

class Bugreport extends DatabaseModel {
    public $tableName = 'bugreports';
    public $databaseMatrix = [
        [ 'userID', 'int', '11' ],
        [ 'time', 'int', '10' ],
        [ 'repeat', 'varchar', '5' ],
        [ 'ourl', 'varchar', '256' ],
        [ 'url', 'varchar', '256' ],
        [ 'ip', 'varchar', '15' ],
        [ 'description', 'text', '' ],
    ];
    public $plugin;

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

    /**
     * This function parses the bug reports description and
     * separates it into separate keys in the array.
     *
     * @param  array $data - The data being parsed.
     *
     * @return array
     */
    public function filter( $data, $params = [] ) {
        foreach ( $data as $instance ) {
            if ( !is_object( $instance ) ) {
                $instance = $data;
                $end = true;
            }
            $instance->submittedBy = self::$user->getUsername( $instance->userID );
            $instance->repeatText = ( $instance->repeat ? 'yes' : 'no' );
            $out[] = $instance;
            if ( !empty( $end ) ) {
                $out = $out[0];
                break;
            }
        }
        return $out;
    }

    /**
     * Logs a Bug Report form.
     *
     * @param  int $ID           the user ID submitting the form
     * @param  string $url          the url
     * @param  string $o_url        the original url
     * @param  int $repeat       is repeatable?
     * @param  string $description_ description of the event.
     *
     * @return null
     */
    public function create( $ID, $url, $oUrl, $repeat, $description ) {
        if ( !$this->plugin->checkEnabled() ) {
            Debug::info( 'Bug Reporting is disabled in the config.' );
            return false;
        }
        $fields = [
            'userID' => App::$activeUser->ID,
            'time' => time(),
            'repeat' => $repeat,
            'ourl' => $oUrl,
            'url' => $url,
            'ip' => $_SERVER['REMOTE_ADDR'],
            'description' => $description,
        ];
        if ( !self::$db->insert( $this->tableName, $fields ) ) {
            new CustomException( 'bugreportsCreate' );

            return false;
        }
        return self::$db->lastId();
    }
}
