<?php

class StranasSurveyAnswer extends \Phalcon\Mvc\Model
{
    const STATUS_INIT = 0;
    const STATUS_VERIFIED = 1;

    /**
     * Initialize method for model.
     */
    public function initialize()
    {
        $this->setSchema("jaga");
        $this->setSource("stranas_survey_answer");
        $this->belongsTo("id_survey", "StranasSurvey", "id");
        $this->belongsTo("id_question", "StranasTarget", "id");
        $this->hasMany("id", "StranasSurveyAnswerAttachment", "id_answer");
        $this->hasMany("id", "StranasSurveyAnswerComment", "id_answer");
        $this->belongsTo("verified_by", "UmUser", "um_id", ['alias' => 'verificator']);
    }

    public function getDetail()
    {
        $result = $this->toArray();

        $result['attachments'] = [];
        $result['attachments_verification'] = [];
        $result['comments'] = [];

        foreach ($this->stranasSurveyAnswerAttachment as $key => $answer_attachment) {
            if (!empty($answer_attachment->attachments)) {
                $key = $answer_attachment->tipe == StranasSurveyAnswerAttachment::TIPE_JAWABAN
                    ? 'attachments'
                    : 'attachments_verification';
                $result[$key][] = $answer_attachment->getDetail();
            }
        }

        foreach ($this->StranasSurveyAnswerComment as $key => $comment) {
            $result['comments'][] = $comment->getDetail();
        }

        $result['verificator'] = empty($this->verificator) ? null : $this->verificator->toArraySimple();

        return $result;
    }
}
