<?php

use Jaga\Models\BaseModel;
use Jaga\Traits\JagaTimestamp;
use Jaga\Traits\SoftDelete;

/**
 * Class PancekAnswerAttachment
 * @property PancekAnswer $answer
 * @property Attachments $attachment
 */
class PancekAnswerAttachment extends BaseModel
{
    use JagaTimestamp, SoftDelete;

    public $id;
    public $answer_id;
    public $attachment_id;
    public $created_at;
    public $updated_at;
    public $deleted_at;

    public static $throwableModelName = 'Attachment Answer PanCek';

    const DOCUMENT_DIR = "pancek-answer-attachment";

    public function initialize()
    {
        $this->setSchema("jaga");
        $this->setSource("pancek_answer_attachment");

        $this->belongsTo(
            'answer_id',
            \PancekAnswer::class,
            'id',
            [
                'reusable' => true, // cache related data
                'alias'    => 'answer',
            ]
        );
        $this->belongsTo(
            'attachment_id',
            \Attachments::class,
            'id',
            [
                'reusable' => true, // cache related data
                'alias'    => 'attachment',
            ]
        );
    }

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

        $result['file'] = $this->attachment->toArray(['id','uuid','filename']);

        return $result;
    }

    public function hardDelete($minio) {
        $attachment = $this->attachment;
        if (!$attachment) {
            throw new CustomErrorException(BaseResponse::DATA_NOT_FOUND, "Lampiran tidak ditemukan.");
        }
        $this->forceDelete();
        AttachmentHelper::delete($attachment, false, $minio, PancekAnswerAttachment::DOCUMENT_DIR);
    }
}
