<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Models\User; 
use Illuminate\Validation\Rule;
use Illuminate\Support\Facades\Validator;

class UserController extends Controller
{   
    
    public function index()
    {
        dd(session('current_user'));
        return view('admin.users'); // Blade file below
    }

    // Returns JSON list; supports category, search, status filters
    public function list(Request $request)
    {
        $q = $request->query('q');
        $category = $request->query('category'); // District/Regional/National or null
        $statuses = $request->query('status') ?: []; // array

        $query = User::query();

        if ($category) $query->where('category', $category);
        if ($q) {
            $query->where(function($sub) use ($q){
                $sub->where('name','like',"%{$q}%")
                    ->orWhere('email','like',"%{$q}%")
                    ->orWhere('id','like',"%{$q}%");
            });
        }
        if (!empty($statuses)) $query->whereIn('status', $statuses);

        $users = $query->orderBy('last_activity_at','desc')->paginate(25);

        return response()->json($users);
    }

    public function show($id)
    {
        $user = User::findOrFail($id);
        return response()->json($user);
    }

    public function store(Request $request)
    {
        $v = Validator::make($request->all(), [
            'name'=>'required|string|max:255',
            'email'=>'required|email|unique:users,email',
            'category'=>['required', Rule::in(['District','Regional','National'])],
        ]);
        if ($v->fails()) return response()->json(['errors'=>$v->errors()], 422);

        $user = User::create($request->only(['name','email','category']) + ['status'=>'Active', 'password'=>bcrypt('temporary')]);
        return response()->json($user, 201);
    }

    public function update(Request $request, $id)
    {
        $user = User::findOrFail($id);
        $v = Validator::make($request->all(), [
            'name'=>'required|string|max:255',
            'email'=>['required','email', Rule::unique('users','email')->ignore($user->id)],
            'category'=>['required', Rule::in(['District','Regional','National'])],
        ]);
        if ($v->fails()) return response()->json(['errors'=>$v->errors()], 422);

        $user->update($request->only(['name','email','category']));
        return response()->json($user);
    }

    public function move(Request $request)
    {
        $v = Validator::make($request->all(), [
            'ids'=>'required|array',
            'target'=>'required|in:District,Regional,National',
        ]);
        if ($v->fails()) return response()->json(['errors'=>$v->errors()], 422);

        User::whereIn('id', $request->ids)->update(['category'=>$request->target]);
        return response()->json(['moved'=>count($request->ids)]);
    }

    public function deactivate(Request $request)
    {
        $v = Validator::make($request->all(), [
            'ids'=>'required|array',
        ]);
        if ($v->fails()) return response()->json(['errors'=>$v->errors()], 422);

        User::whereIn('id', $request->ids)->update(['status'=>'Suspended']);
        return response()->json(['deactivated'=>count($request->ids)]);
    }

    public function destroy($id)
    {
        $user = User::findOrFail($id);
        $user->delete();
        return response()->json(['deleted'=>true]);
    }
    
}
