<?php

namespace App\Http\Controllers\Concerns;

use App\Models\Admin;
use Illuminate\Http\Request;

trait ResolvesPlatformAdminContext
{
    protected function platformAdmin(Request $request): ?Admin
    {
        $adminId = $request->session()->get('admin_id');

        if (! $adminId) {
            return null;
        }

        $admin = Admin::query()->find($adminId);

        return $admin && $admin->isPlatformAdmin() ? $admin : null;
    }

    protected function platformAdminId(Request $request): ?int
    {
        return $this->platformAdmin($request)?->id;
    }

    protected function ensurePlatformAdmin(Request $request): Admin
    {
        $admin = $this->platformAdmin($request);

        abort_unless($admin, 403, '当前账号没有总台管理访问权限');

        return $admin;
    }
}
