<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Models\User;
use App\Services\Admin\UserService;
use App\Enums\UserRole;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Inertia\Inertia;
use Inertia\Response;


// ══════════════════════════════════════════════════════════════════
//  InPractice — Admin\UserController
//  Location: app/Http/Controllers/Admin/UserController.php
//
//  Thin controller — Project Bible compliant.
//  receive → call service → return response. Zero logic here.
//
//  Methods:
//    index()        → member management table with filters
//    toggleActive() → suspend or reactivate a member account
//                     returns JSON so Vue can update without reload
// ══════════════════════════════════════════════════════════════════

class UserController extends Controller
{
    public function __construct(
        private readonly UserService $userService
    ) {}

    /**
     * Show paginated member list with filters.
     * GET /admin/users
     */
    public function index(Request $request): Response
    {
            $data = $this->userService->index($request->only([
            'search', 'hub_id', 'experience_level', 'status',
        ]));

        return Inertia::render('Admin/Users/Index', $data);
    }

    /**
     * Toggle a member's active status (suspend / reactivate).
     * PATCH /admin/users/{user}/toggle-active
     *
     * Returns JSON — Vue updates the row in place, no page reload.
     */
    public function toggleActive(User $user): JsonResponse
    {
        // Only members can be toggled — never touch admin accounts
        abort_if($user->role !== UserRole::Member->value, 403);

        $newStatus = $this->userService->toggleActive($user);

        return response()->json([
            'is_active' => $newStatus,
            'message'   => $newStatus
                ? __('admin.member_activated')
                : __('admin.member_suspended'),
        ]);
    }
}
