<?php
/**
 * app/plugins/comments/models/comment.php
 *
 * This class is used for the creation, retrieval, and manipulation
 * of the comments table.
 *
 * @package TP Comments
 * @version 5.0.1
 * @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\Houdini\Classes\Views;
use TheTempusProject\Classes\DatabaseModel;
use TheTempusProject\TheTempusProject as App;
use TheTempusProject\Canary\Classes\CustomException;
use TheTempusProject\Houdini\Classes\Filters;

class Comments extends DatabaseModel {
    public $tableName = 'comments';
    public $databaseMatrix = [
        [ 'author', 'int', '11' ],
        [ 'contentID', 'int', '11' ],
        [ 'created', 'int', '10' ],
        [ 'edited', 'int', '10' ],
        [ 'approved', 'int', '1' ],
        [ 'contentType', 'varchar', '32' ],
        [ 'content', 'text', '' ],
    ];

    public function count( $contentType, $contentID ) {
        if ( !Check::id( $contentID ) ) {
            Debug::info( 'Comments: illegal ID.' );

            return false;
        }
        if ( !Check::dataTitle( $contentType ) ) {
            Debug::info( 'Comments: illegal Type.' );

            return false;
        }
        $where = ['contentType', '=', $contentType, 'AND', 'contentID', '=', $contentID];
        $data = self::$db->get( $this->tableName, $where );
        if ( !$data->count() ) {
            Debug::info( 'No comments found.' );

            return 0;
        }
        return $data->count();
    }

    public function display( $displayCount, $contentType, $contentID ) {
        if ( !Check::id( $contentID ) ) {
            Debug::info( 'Comments: illegal ID.' );

            return false;
        }
        if ( !Check::dataTitle( $contentType ) ) {
            Debug::info( 'Comments: illegal Type.' );

            return false;
        }
        $where = ['contentType', '=', $contentType, 'AND', 'contentID', '=', $contentID];
        $commentData = self::$db->get( $this->tableName, $where, 'created', 'DESC', [0, $displayCount] );
        if ( !$commentData->count() ) {
            Debug::info( 'No comments found.' );

            return false;
        }
        return $this->filter( $commentData->results() );
    }

    public function update( $id, $comment ) {
        if ( empty( self::$log ) ) {
            self::$log = new Log;
        }
        if ( !Check::id( $id ) ) {
            Debug::info( 'Comments: illegal ID.' );

            return false;
        }
        $fields = [
            'edited' => time(),
            'content' => $comment,
            'approved' => 1,
        ];
        if ( !self::$db->update( $this->tableName, $id, $fields ) ) {
            new CustomException( 'commentUpdate' );
            Debug::error( "Post: $id not updated: $fields" );

            return false;
        }
        self::$log->admin( "Updated Comment: $id" );
        return true;
    }

    public function create( $contentType, $contentID, $comment ) {
        if ( !Check::id( $contentID ) ) {
            Debug::info( 'Comments: illegal ID.' );

            return false;
        }
        if ( !Check::dataTitle( $contentType ) ) {
            Debug::info( 'Comments: illegal Type.' );

            return false;
        }
        $fields = [
            'author' => App::$activeUser->ID,
            'edited' => time(),
            'created' => time(),
            'content' => $comment,
            'contentType' => $contentType,
            'contentID' => $contentID,
            'approved' => 0,
        ];
        if ( !self::$db->insert( $this->tableName, $fields ) ) {
            new CustomException( 'commentCreate' );
            Debug::error( "Comments: $data not created: $fields" );

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

    public function filter( $data, $params = [] ) {
        foreach ( $data as $instance ) {
            if ( !is_object( $instance ) ) {
                $instance = $data;
                $end = true;
            }
            if ( App::$isAdmin || ( App::$isLoggedIn && $instance->author == App::$activeUser->ID ) ) {
                $instance->commentControl = Views::simpleView( 'comments.control', ['ID' => $instance->ID] );
            } else {
                $instance->commentControl = '';
            }
            $data = self::$db->get( $instance->contentType, ['ID', '=', $instance->contentID] )->results();
            if ( empty( $data ) ) {
                $title = 'Unknown';
            } elseif ( empty( $data[0]->title ) ) {
                $title = 'Unknown';
            } else {
                $title = $data[0]->title;
            }
            $authorName = self::$user->getUsername( $instance->author );
            $authorAvatar = self::$user->getAvatar( $instance->author );
            $instance->avatar = $authorAvatar;
            $instance->authorName = $authorName;
            $instance->contentTitle = $title;
            $instance->content = Filters::applyOne( 'mentions.0', $instance->content, true );
            $instance->content = Filters::applyOne( 'hashtags.0', $instance->content, true );
            $out[] = $instance;
            if ( !empty( $end ) ) {
                $out = $out[0];
                break;
            }
        }
        return $out;
    }

    public function recent( $contentType = 'all', $limit = null ) {
        if ( $contentType === 'all' ) {
            $where = ['ID', '>', '0'];
        } else {
            $where = ['contentType', '=', $contentType];
        }
        if ( empty( $limit ) ) {
            $commentData = self::$db->get( $this->tableName, $where, 'created', 'DESC' );
        } else {
            $commentData = self::$db->get( $this->tableName, $where, 'created', 'DESC', [0, $limit] );
        }
        if ( !$commentData->count() ) {
            Debug::info( 'No comments found.' );

            return false;
        }
        return $this->filter( $commentData->results() );
    }
}
