<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Pagination\LengthAwarePaginator;
use Illuminate\Support\Facades\Request;
use Nwidart\Modules\Facades\Module as ModuleManager;

class Module extends Model
{

    /**
     * Notes   : 自定义返回数据
     * @Date   : 2021/3/11 9:59 上午
     * @Author : < Jason.C >
     * @return \Illuminate\Pagination\LengthAwarePaginator
     */
    public function paginate(): LengthAwarePaginator
    {
        $perPage = Request::get('per_page', 20);
        // $page    = Request::get('page', 1);
        // $start   = ($page - 1) * $perPage;

        $data = ModuleManager::toCollection();

        $movies = $data->map(function ($module) {
            return [
                'id'          => $module->getName(),
                'name'        => $module->getName(),
                'alias'       => $module->getAlias(),
                'description' => $module->getDescription(),
                'priority'    => $module->getPriority(),
                'keywords'    => $module->get('keywords'),
                'requires'    => $module->getRequires(),
                'enabled'     => $module->isEnabled(),
                'version'     => $module->get('version'),
                'author'      => $module->get('author'),
            ];
        });

        $movies = static::hydrate($movies->toArray());

        $paginator = new LengthAwarePaginator($movies, ModuleManager::count(), $perPage);

        $paginator->setPath(url()->current());

        return $paginator;
    }

}
