<?php

namespace Modules\User\Controllers\Admin;

/** Ajax Request */

use Exception;
use Illuminate\View\View;
use Illuminate\Http\Request;
use Illuminate\Http\JsonResponse;
use App\Http\Controllers\Controller;
use Modules\PaymentLog\Enums\PaymentLogAccStatusEnum;
use Modules\School\Services\SchoolService;
use Modules\Address\Services\AddressService;
use Modules\User\Enums\UserGenderEnum;
use Modules\User\Enums\UserRoleEnum;
use Modules\User\Requests\Admin\ChildRequest;
use Modules\User\Services\UserService;
use App\Services\Response\ResponseService;
use Modules\User\Requests\Admin\ParentRequest;

class ParentController extends Controller
{

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

    public function __construct(
        private UserService $userService,
        private SchoolService $schoolService,
        private AddressService $addressService,
    )
    {}

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

    /**
     * return data of the parents.
     *
     * @param Request $request
     * @return View
     */
    public function grid(Request $request) :View
    {
        $parent_status = PaymentLogAccStatusEnum::PARENT->value;
        $request->merge(['paginate'=>true,'role'=>UserRoleEnum::PARENT->value]);
        $parents= $this->userService->get($request->all());

        return view($this->viewDirectory.'grid',get_defined_vars());
    }

    /**
     * Show the form for creating a new User.
     *
     * @return View
     */
    public function create() :View
    {
        $action= route('parents.store');
        $head= __('parents.new');
        return view( $this->viewDirectory.'form', get_defined_vars() );
    }


    /**
     * Store a newly created User in storage.
     * @param ParentRequest $request
     * @return JsonResponse
     */
    public function store(ParentRequest $request) :JsonResponse
    {
        $data  = $request->validated();
        $data['role'] = UserRoleEnum::PARENT->value;
        $data['points'] = 0;
        $this->userService->create($data);

        session()->flash('message', __('dashboard.saveDone'));
        $response = [
            'status'=>200,
            'message'=>__('web.success'),
            'data'=> route('parents.index'),
        ];
        return ResponseService::response($response);
    }

    /**
     * Show the form for editing the specified User.
     *
     * @param  int $id
     * @return View
     */
    public function edit(int $id) :View
    {
        $parent = $this->userService->getItem($id);
        if (!$parent)
        {
            abort(404);
        }
        $action= route('parents.update', $parent->id);
        $head= __('parents.edit');
        $children = $this->userService->get(['role'=>UserRoleEnum::CHILD->value,'parent_id'=>$id]);
        return view( $this->viewDirectory.'form', get_defined_vars() );
    }

    public function show(int $id) :View
    {
        $parent = $this->userService->getItem($id);
        if (!$parent)
        {
            abort(404);
        }
        $head= __('members.addresses');
        $addresses = $this->addressService->get([
            'user_id'=>$id
        ]);
        return view( $this->viewDirectory.'addresses', get_defined_vars() );
    }
    /**
     * Update the specified User in storage.
     * @param ParentRequest $request
     * @param int $id
     * @return JsonResponse
     */
    public function update(ParentRequest $request, int $id) :JsonResponse
    {
        $member = $this->userService->checkItem($id);
        if (!$member)
        {
            abort(404);
        }
        $this->userService->update($request->validated(), $id);
        session()->flash('message', __('dashboard.saveDone'));
        $response = [
            'status'=>200,
            'message'=>__('web.success'),
            'data'=> route('parents.index'),
        ];
        return ResponseService::response($response);
    }

    public function child($id)
    {
        $child = $this->userService->getItem($id);
        if (!$child)
        {
            abort(404);
        }
        $action= route('parents.children.update', $child->id);
        $head= __('parents.child_edit');
        $genders = UserGenderEnum::values();
        $schools = $this->schoolService->get();
        return view( $this->viewDirectory.'child', get_defined_vars() );
    }

    public function childUpdate(ChildRequest $request, int $id) :JsonResponse
    {
        $child = $this->userService->checkItem($id);
        if (!$child)
        {
            abort(404);
        }
        $this->userService->update($request->validated(), $id);
        session()->flash('message', __('dashboard.saveDone'));
        $response = [
            'status'=>200,
            'message'=>__('web.success'),
            'data'=> route('parents.edit',$child->parent_id),
        ];
        return ResponseService::response($response);
    }

    /**
     * Remove the specified User from storage.
     * @param int $id
     * @return JsonResponse
     * @throws Exception
     */
    public function destroy(int $id) :JsonResponse
    {
        $this->userService->destroy($id);
        session()->flash('message', __('dashboard.saveDone'));
        $response = [
            'status'=>200,
            'message'=>__('web.success'),
            'data'=> route('parents.index'),
        ];
        return ResponseService::response($response);
    }

    /**
     * Remove several parents by IDs.
     *
     * @param Request $request
     * @return JsonResponse
     */
    public function destroyAll(Request $request) :JsonResponse
    {
        $this->userService->destroyAll([
            'ids' => $request->ids,
            'status' => 1
        ]);
        $response = [
            'status'=>200,
            'message'=>__('web.success'),
            'data'=> route('parents.index'),
        ];
        return ResponseService::response($response);
    }

    /**
     * Restore the specified User from storage.
     *
     * @param int $id
     * @return JsonResponse
     */
    public function restore(int $id) :JsonResponse
    {
        $this->userService->destroy($id);
        $response = [
            'status'=>200,
            'message'=>__('web.success'),
            'data'=> route('parents.index'),
        ];
        return ResponseService::response($response);
    }

    /**
     * Restore several parents by IDs.
     *
     * @param Request $request
     * @return JsonResponse
     */
    public function restoreAll(Request $request) :JsonResponse
    {
        $this->userService->destroyAll([
            'ids' => $request->ids,
            'status' => 0
        ]);
        $response = [
            'status'=>200,
            'message'=>__('web.success'),
            'data'=> route('parents.index'),
        ];
        return ResponseService::response($response);
    }
}
