<?php

namespace App\Http\Middleware;

use App\Models\Admin;
use Closure;
use Illuminate\Http\Request;
use Symfony\Component\HttpFoundation\Response;

class AdminAuth
{
    public function handle(Request $request, Closure $next): Response
    {
        $adminId = $request->session()->get('admin_id');

        if (! $adminId) {
            return redirect('/admin/login');
        }

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

        if (! $admin || ! $admin->isPlatformAdmin()) {
            abort(403, '当前账号没有总台管理访问权限');
        }

        $request->session()->put('admin_scope', $admin->platformLabel());
        $request->session()->put('admin_role', $admin->role);
        $request->session()->put('admin_name', $admin->name);
        $request->session()->put('admin_email', $admin->email);
        $request->session()->put('admin_merchant_id', null);

        return $next($request);
    }
}
