<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models;
use App\Utilities\ApiCalls;
use Session;
use Illuminate\Pagination\LengthAwarePaginator;
use Illuminate\Support\Facades\Hash;


class ClientUsersController extends Controller
{
    public function indexBak(){
        $user_list = Models\ClientSession::get();
        $data = [
            'page_title' => 'Users',
        ];

        return view('client-users.index', $data);

    }
    public function index()
    {
         $data = [
            'page_title' => 'Users',
        ];
        return view('client-users.index', $data);
    }

    // Read: Fetch data for the jQuery table
    // public function fetch()
    // {
    //     $sessions = Models\ClientSession::orderBy('id', 'desc')->paginate(5);
    //     return response()->json(['sessions' => $sessions]);
    // }
    public function fetch(Request $request)
    {
        $query = Models\ClientSession::query();

        // Check if the search parameter has a value
        if ($request->has('search') && !empty($request->search)) {
            $search = $request->search;
            $query->where(function($q) use ($search) {
                $q->where('email', 'LIKE', "%{$search}%")
                  ->orWhere('role', 'LIKE', "%{$search}%");
            });
        }
        $sessions = $query->orderBy('id', 'desc')->paginate(5); 
        
        return response()->json(['sessions' => $sessions]);
    }
    public function store(Request $request)
    {
        $request->validate([
            'email' => 'required|email|unique:client_sessions,email',
            'role'  => 'required|string',
            'password' => 'required|string'
        ]);

        $session = Models\ClientSession::create([
            'email' => $request->email,
            'role'  => $request->role,
            'client_id' => session('current_user.org_id'),
            'password' => Hash::make($request->password) 
        ]);

        return response()->json(['status' => 'success', 'message' => 'User created successfully!']);
    }

    public function edit($id)
    {
        $session = Models\ClientSession::findOrFail($id);
        return response()->json(['session' => $session]);
    }

    public function update(Request $request, $id)
    {
        $request->validate([
            'email' => 'required|email|unique:client_sessions,email,' . $id,
            'role'  => 'required|string',
            'password' => 'required|string'
        ]);

        $session = Models\ClientSession::findOrFail($id);
        $session->update([
            'email' => $request->email,
            'role'  => $request->role,
            'client_id' => session('current_user.org_id'),
            'password' => Hash::make($request->password) 
        ]);

        return response()->json(['status' => 'success', 'message' => 'User updated successfully!']);
    }

    // Delete: Remove record
    public function destroy($id)
    {
        Models\ClientSession::findOrFail($id)->delete();
        return response()->json(['status' => 'success', 'message' => 'User deleted successfully!']);
    }
}
