<?php
namespace App\Controllers\Admin;

use Core\Request;
use Core\Response;
use Core\View;
use App\Models\User;

class UserController
{
    public function index()
    {
        View::make('admin/users_list')
            ->setLayout('admin_layout')
            ->with([
                'title' => 'System Users',
                'users' => User::all()
            ])->render();
    }

    public function updateStatus($id)
    {
        $data = Request::all();
        // Allow admin to promote users to admin or demote them
        if (isset($data['role']) && in_array($data['role'], ['admin', 'victim', 'responder'])) {
            User::update($id, ['role' => $data['role']]);
            return Response::json(['status' => 'success', 'message' => 'User role updated.']);
        }
        return Response::json(['status' => 'error', 'message' => 'Invalid role.'], Response::BAD_REQUEST);
    }
}