<?php

namespace App\Http\Controllers\Admin;

/** Ajax Request */

use App\Exports\MemberExport;
use App\Http\Requests\Admin\GroupRequest;
use App\Models\Group;
use App\Services\Interfaces\GroupRepositoryInterface;
use App\Services\Response\ResponseService;
use Exception;
use Illuminate\View\View;
use Illuminate\Http\Request;
use Illuminate\Http\JsonResponse;
use Illuminate\Routing\Redirector;
use Illuminate\Http\RedirectResponse;
use App\Http\Controllers\Controller;
use Maatwebsite\Excel\Facades\Excel;

class GroupController extends Controller
{

    /** Redirect to this path after each operation success*/
    private $redirectSuccessPath= '/admin/groups';
    /** View folder */
    private $viewDirectory= 'admin.groups.';
    //
    private GroupRepositoryInterface $groupRepository;

    public function __construct(GroupRepositoryInterface $groupRepository)
    {
        $this->groupRepository = $groupRepository;
    }

    /**
     * Display a listing of the groups.
     *
     * @param Request $request
     * @return View
     */
    public function index(Request $request) :View
    {
        $title= __('groups.head');
        $groups= $this->groupRepository->get($request);
        return view($this->viewDirectory.'index', get_defined_vars());
    }

    /**
     * return data of the groups.
     *
     * @param Request $request
     * @return View
     */
    public function grid(Request $request) //:View
    {
        $request->merge(['paginate'=> true]);
        $groups= $this->groupRepository->get($request);

        return view($this->viewDirectory.'grid',compact('groups'));
    }
    
    public function edit(Request $request, Group $group) :View
    {
        $head = __('groups.edit_order');
        $action = route('groups.update',$group->id);
        return view($this->viewDirectory.'form', get_defined_vars());
    }
    
    public function update(Request $request, Group $group) :JsonResponse
    {
        $this->groupRepository->updateOrder($request,$group);
        
        session()->flash('message', __('dashboard.saveDone'));
        
        $response = [
            'status'=>200,
            'message'=>__('web.success'),
            'data'=> route('groups.index'),
        ];
        return ResponseService::response($response);
    }
    /**
     * De-Activate the specified Group from storage.
     * @param Request $request
     * @param int $id
     * @return JsonResponse
     * @throws Exception
     */
    public function toggleStatus(Request $request, int $id) :JsonResponse
    {
        $return= $this->groupRepository->toggleStatus($id);
        return response()->json($return[0], $return[1] ?? 200);
    }

    /**
     * Remove the specified Group from storage.
     * @param Request $request
     * @param int $id
     * @return JsonResponse
     * @throws Exception
     */
    public function destroy(Request $request, int $id) :JsonResponse
    {
        $return= $this->groupRepository->destroy($request, $id);
        return response()->json($return[0], $return[1] ?? 200);
    }

    /**
     * Remove several groups by IDs.
     *
     * @param Request $request
     * @return JsonResponse
     */
    public function destroyAll(Request $request) :JsonResponse
    {
        $return= $this->groupRepository->destroyAll($request);
        return response()->json($return[0], $return[1] ?? 200);
    }

    /**
     * Restore the specified Group from storage.
     *
     * @param Request $request
     * @param int $id
     * @return JsonResponse
     */
    public function restore(Request $request, int $id) :JsonResponse
    {
        $return= $this->groupRepository->restore($request, $id);
        return response()->json($return[0], $return[1] ?? 200);
    }

    /**
     * Restore several groups by IDs.
     *
     * @param Request $request
     * @return JsonResponse
     */
    public function restoreAll(Request $request) :JsonResponse
    {
        $return= $this->groupRepository->restoreAll($request);
        return response()->json($return[0], $return[1] ?? 200);
    }
}
