<?php

namespace App\Domains\Admin\Controllers;

use App\Models\User;
use App\Scopes\CommonController;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;

class UserToggleActiveController extends CommonController
{
    public function __invoke(int $id, Request $request): JsonResponse
    {
        $user = User::findOrFail($id);

        if (auth()->id() === $user->id) {
            return response()->json([
                'status' => 'error',
                'message' => 'Du kannst dich nicht selbst deaktivieren.',
            ]);
        }

        $user->update(['active' => !$user->active]);

        $status = $user->active ? 'aktiviert' : 'deaktiviert';

        return response()->json([
            'status' => 'success',
            'message' => 'Benutzer*in wurde ' . $status . '.',
            'active' => $user->active,
        ]);
    }
}
