<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use App\Models\DTO\SearchData;

class SmsFavorite extends Model
{
    protected $table = 'tb_sms_favorite';
    protected $primaryKey = 'id';

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

    const CREATED_AT = 'created_at';
    const UPDATED_AT = 'updated_at';
    const DELETED_AT = null;

    protected $guarded = [];

    /**
     * 자주보내는 문자 조회
     */
    public function data(SearchData $params): object
    {
        $query = $this->query();
        $query->select('tb_sms_favorite.*');

        if($params->keyword) {
            switch ($params->field) {
                case 'tb_sms_favorite.id' :
                    $query->where($params->field, '=', $params->keyword);
                    break;
                case 'tb_sms_favorite.subject' :
                case 'tb_sms_favorite.content' :
                    $query->where($params->field, 'LIKE', "%{$params->keyword}%");
                    break;
            }
        }

        $query->orderByDesc('tb_sms_favorite.id');

        $list = $query->paginate($params->perPage, ['*'], 'page', $params->page);
        $total = $this->count();
        $rows = $list->count();

        return (object)[
            'total' => $total,
            'rows' => $rows,
            'list' => $list
        ];
    }
}
