<?php

namespace App\Models;

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

class BannerGroup extends Model
{
    protected $table = 'tb_banner_group';
    protected $primaryKey = 'id';

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

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

    protected $guarded = [];

    public function user()
    {
        return $this->belongsTo(User::class)->withDefault();
    }

    public function banner()
    {
        return $this->hasMany(Banner::class);
    }

    /**
     * 배너 그룹 조회
     */
    public function data(SearchData $params): object
    {
        $query = $this->query();
        $query->select('*');
        $list = $query->paginate($params->perPage, ['*'], 'page', $params->page);
        $total = $this->count();
        $rows = $list->count();

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