<?php
namespace Jaga\Models\PAK;

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

class PAKIsianSekolahDetail extends BaseModel
{
    use JagaTimestamp, SoftDelete;

    const METODE_INSERSI = 1;
    const METODE_HABITUASI = 2;
    const METODE_LAINNYA = 3;

    public static $throwableModelName = 'Isian Sekolah Detail';

    public function initialize()
    {
        $this->setSchema("jaga");
        $this->setSource("pak_isian_sekolah_detail");
        $this->belongsTo(
            "isian_id",
            "Jaga\Models\PAK\PAKIsianSekolah",
            "id"
        );
        $this->hasMany(
            "id",
            "Jaga\Models\PAK\PAKIsianSekolahAttachment",
            "isian_detail_id",
            [
                 'reusable' => true, // cache related data
                 'alias'    => 'attachments',
            ]
        );
    }

    public function toArray($columns = null)
    {
        $result = parent::toArray($columns);

        switch ($result['metode_id']) {
            case self::METODE_INSERSI:
                $result['metode'] = 'Insersi Mata Pelajaran';
                break;
            case self::METODE_HABITUASI:
                $result['metode'] = 'Habituasi atau Pembiasaan';
                break;

            default:
                $result['metode'] = 'Metode Lainnya';
                break;
        }

        return $result;
    }

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

        $result['documents'] = [];
        $result['images'] = [];
        if (!empty($this->attachments)) {
            foreach ($this->attachments as $a) {
                if ($a->tipe == PAKIsianSekolahAttachment::FILE_TYPE_DOCUMENT) {
                    $result['documents'][] = $a->toArray(['id', 'attachment_id']);
                }
                if ($a->tipe == PAKIsianSekolahAttachment::FILE_TYPE_IMAGE) {
                    $result['images'][] = $a->toArray(['id', 'attachment_id']);
                }
            }
        }

        return $result;
    }

    public function syncAttachments($doc_ids, $img_ids, $minio = null)
    {
        $currentStacks = PAKIsianSekolahAttachment::find([
            'conditions' => 'isian_detail_id = :id:',
            'bind' => ['id' => $this->id]
        ]);

        if ($currentStacks->count() > 0) {
            foreach ($currentStacks as $attachment) {

                if ($attachment->tipe == PAKIsianSekolahAttachment::FILE_TYPE_DOCUMENT
                    && !in_array($attachment->id, $doc_ids)
                ) {
                    try {
                        \AttachmentHelper::deletion($attachment->attachment, false, $minio, 'users_profpic/pak-isian-sekolah-attachment/document' . DIRECTORY_SEPARATOR);
                    } catch (\Exception $e) {
                    }

                    $attachment->forceDelete();
                }

                if ($attachment->tipe == PAKIsianSekolahAttachment::FILE_TYPE_IMAGE
                    && !in_array($attachment->id, $img_ids)
                ) {
                    try {
                        \AttachmentHelper::deletion($attachment->attachment, false, $minio, 'users_profpic/pak-isian-sekolah-attachment/image' . DIRECTORY_SEPARATOR);
                    } catch (\Exception $e) {
                    }

                    $attachment->forceDelete();
                }
            }
        }
    }
}
