<?php

class DiskusiComment extends \Phalcon\Mvc\Model
{

    /**
     *
     * @var integer
     */
    public $id;

    /**
     *
     * @var integer
     */
    public $user_id;

    /**
     *
     * @var integer
     */
    public $tenant_id;

    /**
     *
     * @var integer
     */
    public $diskusi_id;

    /**
     *
     * @var integer
     */
    public $level;

    /**
     *
     * @var string
     */
    public $comment;

    /**
     *
     * @var integer
     */
    public $comment_id;

    /**
     *
     * @var string
     */
    public $order_str;

    /**
     *
     * @var string
     */
    public $uuid;

    /**
     *
     * @var string
     */
    public $created_at;

    /**
     *
     * @var string
     */
    public $updated_at;

    /**
     *
     * @var integer
     */
    public $is_deleted;

    /**
     * Initialize method for model.
     */
    public function initialize()
    {
        $this->setSchema("jaga");
        $this->setSource("diskusi_comment");
        $this->hasMany('id', 'DiskusiCommentLike', 'comment_id', ['alias' => 'diskusiCommentLike']);
        $this->hasMany('id', 'CommentAttachment', 'comment_id', ['alias' => 'commentAttachment']);
        $this->hasOne('id', 'DiskusiBansosHistory', 'id_komentar', ['alias' => 'diskusiBansosHistory']);
        $this->belongsTo('user_id', '\UmUser', 'um_id', ['alias' => 'umUser', 'params' => ['conditions' => 'is_active = true']]);
        $this->belongsTo('diskusi_id', '\Diskusi', 'id', ['alias' => 'diskusi']);
    }

    /**
     * Returns table name mapped in the model.
     *
     * @return string
     */
    public function getSource()
    {
        return 'diskusi_comment';
    }

    /**
     * Allows to query a set of records that match the specified conditions
     *
     * @param mixed $parameters
     * @return DiskusiComment[]|DiskusiComment|\Phalcon\Mvc\Model\ResultSetInterface
     */
    public static function find($parameters = null)
    {
        return parent::find($parameters);
    }

    /**
     * Allows to query the first record that match the specified conditions
     *
     * @param mixed $parameters
     * @return DiskusiComment|\Phalcon\Mvc\Model\ResultInterface
     */
    public static function findFirst($parameters = null)
    {
        return parent::findFirst($parameters);
    }

    public function report()
    {
        return self::getReport($this->id);
    }


    public function isDeleted()
    {
        return $this->is_deleted > 0;
    }

    public static function getReport($id)
    {
        $status = DiskusiLapor::STATUS_DISETUJUI;
        return DiskusiLapor::findFirst([
            "id_komentar=:id_komentar: AND status = :status: AND id_user_terlapor IS NULL",
            'bind' => [
                'id_komentar' => $id,
                'status' => $status
            ]
        ]);
    }


    public function toArrayComplete()
    {
        $result = $this->toArray();
        $result['diskusi'] = $this->diskusi;
        $result['owner'] = $this->umUser ? $this->umUser->toArraySimple() : null;
        $result["count_like"] = DiskusiCommentLike::count([
            'comment_id = :id:',
            'bind' => [
                'id' => $this->id
            ]
        ]);
        $result['laporan'] = $this->report();
        // $result['diskusi_bansos_history'] = $this->diskusiBansosHistory;
        // $result['profile_picture_url'] = empty($this->umUser->foto) ? null : MediaHelper::getPublicPath($this->config['profile_picture_base_url'], $this->umUser->foto);

        return $result;
    }

    public static function getCreationReportBetween($start, $end, $type = Diskusi::TYPE_DISKUSI)
    {
        $bind = [
            'start' => $start . ' 00:00:00',
            'end' => $end . ' 23:59:59'
        ];
        switch ($type) {
            case Diskusi::TYPE_DISKUSI:
                $sql = "SELECT c.created_at::date, count(c.*) from jaga.diskusi_comment c left join jaga.diskusi d on d.id = c.diskusi_id AND d.is_deleted = 0 left join jaga.diskusi_bansos b on b.id_diskusi = d.id AND b.deleted_at IS NULL where c.is_deleted = 0 AND c.created_at >= :start AND c.created_at <= :end AND d.type = :type AND b.id IS NULL group by 1 order by 1 asc";
                $bind["type"] = $type;
                break;
            case Diskusi::TYPE_BERITA:
                $sql = "SELECT c.created_at::date, count(c.*) from jaga.diskusi_comment c left join jaga.diskusi d on d.id = c.diskusi_id AND d.is_deleted = 0 where c.is_deleted = 0 AND c.created_at >= :start AND c.created_at <= :end AND d.type = :type group by 1 order by 1 asc";
                $bind["type"] = $type;
                break;
            case Diskusi::TYPE_KELUHAN:
                $sql = "SELECT c.created_at::date, count(c.*) from jaga.diskusi_comment c left join jaga.diskusi d on d.id = c.diskusi_id AND d.is_deleted = 0 left join jaga.diskusi_bansos b on b.id_diskusi = d.id AND b.deleted_at IS NULL where c.is_deleted = 0 AND c.created_at >= :start AND c.created_at <= :end AND d.type = :type AND b.id IS NOT NULL group by 1 order by 1 asc";
                $bind["type"] = Diskusi::TYPE_DISKUSI;
                break;
            default:
                $sql = "SELECT created_at::date, count(*) from jaga.diskusi_comment where c.is_deleted = 0 AND created_at >= :start AND created_at <= :end group by 1 order by 1 asc";
                break;
        }

        $record = new DiskusiComment();

        return $record->getReadConnection()->query($sql, $bind)->fetchAll(\PDO::FETCH_ASSOC);
    }
}
