<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Cache;

class BoardGroupMeta extends Model
{
    protected $table = 'tb_board_group_meta';
    protected $primaryKey = 'board_group_id';

    public $keyType = 'int';
    public $incrementing = false;
    public $timestamps = false;

    public string $metaKey = 'key';
    public string $metaValue = 'value';
    public string $cachePrefix = 'board-group-meta-';
    public int $cacheTime = CACHE_EXPIRE_TIME;

    public function __set($name, $value)
    {
        $this->{$name} = $value;
    }

    public function __get($name)
    {
        return ($this->{$name} ?? null);
    }

    /**
     * 게시판 그룹 설정 정보 조회
     */
    public function data($boardGroupID = 0)
    {
        $res = $this->where($this->primaryKey, $boardGroupID)->get();
        if ($res->count() > 0) {
            foreach ($res as $val) {
                $this->{$val[$this->metaKey]} = $val[$this->metaValue];
            }
            return $this;
        }
        return null;
    }

    /**
     * 게시판 그룹 설정 값 조회
     */
    public function getAllMeta($boardGroupID = 0)
    {
        $cacheName = ($this->cachePrefix . $boardGroupID);
        if (!$ret = Cache::get($cacheName)) {
            $ret = $this->data($boardGroupID);
            Cache::put($cacheName, (object)$ret, $this->cacheTime);
        }
        return $ret;
    }

    /**
     * 저장
     */
    public function save($boardGroupID = 0, $saveData = [])
    {
        if ($saveData && is_array($saveData)) {
            foreach ($saveData as $column => $value) {
                $this->_metaUpdate($boardGroupID, $column, $value);
            }
        }
        Cache::forget($this->cachePrefix . $boardGroupID);
        $this->getAllMeta($boardGroupID);
    }

    /**
     * 삭제
     */
    public function deleteMeta($boardGroupID = 0)
    {
        $this->where($this->primaryKey, $boardGroupID)->delete();
        Cache::forget($this->cachePrefix . $boardGroupID);
    }

    /**
     * 조회
     */
    public function item($boardGroupID = 0, $column = '', $default = null)
    {
        if (empty($column)) {
            return false;
        }

        $return = $default;
        if (property_exists($this, $column)) {
            $return = $this->{$column};
        }

        return $return;
    }

    /**
     * 등록/수정
     */
    private function _metaUpdate($boardGroupID = 0, $column = '', $value = null)
    {
        if(!$boardGroupID) {
            return false;
        }

        $column = trim($column);
        if (empty($column)) {
            return false;
        }

        $items = $this->data($boardGroupID);
        if($items) {
            $oldValue = property_exists($items, $column) ? $items->{$column} : false;
        }else{
            $oldValue = false;
        }

        if (empty($value) && $value != 0) {
            $value = '';
        }

        if ($value === $oldValue) {
            return false;
        }

        if (false === $oldValue) {
            return $this->_addMeta($boardGroupID, $column, $value); // 새로 추가
        }

        return $this->_updateMeta($boardGroupID, $column, $value); // 수정
    }

    /**
     * 등록
     */
    private function _addMeta($boardGroupID = 0, $column = '', $value = '')
    {
        if(!$boardGroupID) {
            return false;
        }

        $column = trim($column);
        if (empty($column)) {
            return false;
        }

        $insertData = [
            $this->primaryKey => $boardGroupID,
            $this->metaKey => $column,
            $this->metaValue => $value
        ];

        return $this->insert($insertData);
    }

    /**
     * 수정
     */
    private function _updateMeta($boardGroupID = 0, $column = '', $value = '')
    {
        if(!$boardGroupID) {
            return false;
        }

        $column = trim($column);
        if (empty($column)) {
            return false;
        }

        $updateData = [
            $this->metaValue => $value
        ];

        return $this->where($this->primaryKey, $boardGroupID)->where($this->metaKey, $column)->update($updateData);
    }
}
