<?php

namespace App\Http\Controllers\Admin;

use App\Career;
use App\CareerReply;
use App\Http\Requests\CareerRequest;
use App\VisitorRequestReply;
use Exception;
use App\VisitorMessage;
use Illuminate\Support\Facades\Auth;
use Illuminate\View\View;
use Illuminate\Http\Request;
use App\Jobs\VisitorMessagesJob;
use Illuminate\Http\JsonResponse;
use Illuminate\Routing\Redirector;
use App\Http\Controllers\Controller;
use Illuminate\Http\RedirectResponse;
use App\Http\Requests\VisitorMessageRequest;

class CareerController extends Controller
{
    /** Redirect to this path after each operation success*/
    private $redirectSuccessPath= '/admin/careers';
    /** View folder */
    private $viewDirectory= 'admin.careers.';

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

    /**
     * return data of the newsletters.
     *
     * @param Request $request
     * @return View
     */
    public function grid(Request $request) :View
    {
        $query = Career::latest();

        if ($request->date_from) {
            $query->whereDate('careers.created_at', '>=', $request->date_from);
        }

        if ($request->date_to) {
            $query->whereDate('careers.created_at', '<=', $request->date_to);
        }

        if ($request->name) {
            $query->where('careers.name', 'LIKE', '%'.$request->name.'%');
        }

        if ($request->email) {
            $query->where('careers.email', 'LIKE', '%'.$request->email.'%');
        }
        if ($request->phone) {
            $query->where('careers.phone', 'LIKE', '%'.$request->phone.'%');
        }


        if ( !is_null($request->status) && $request->status == 0) {
            $query->onlyTrashed();
        }

        $careers= $query->paginate(10);

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

    /**
     * Show the form for creating a new resource.
     *
     * @return View
     */
    public function create() :View
    {
        $action= route('careers.store');
        $head = metaFields('careers', 'add_new', getCurrentLocale()) ?? __('careers.edit');

        return view( $this->viewDirectory.'form', compact('action', 'head') );
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param VisitorMessageRequest $request
     * @return RedirectResponse|Redirector
     */
    public function store(CareerRequest $request) :RedirectResponse
    {
        $this->saveData($request);
        return redirect($this->redirectSuccessPath)->with('message', __('careers.email_success'));
    }


    /**
     * Show the form for editing the specified resource.
     *
     * @param Career $career
     * @return View
     */
    public function edit(Career $career) :View
    {
        $replies = $career->replies;
        $career->update(['read' => 1]);
        $action= route('careers.update', $career->id);
        return view( $this->viewDirectory.'form', compact('career','replies', 'action') );
    }

    /**
     * Update the specified resource in storage.
     *
     * @param VisitorMessageRequest $request
     * @param VisitorMessage $career
     * @return RedirectResponse|Redirector
     */
    public function update(CareerRequest $request, Career $career) :RedirectResponse
    {
        $this->saveData($request, $career);
        return redirect($this->redirectSuccessPath)->with('message', __('careers.email_success'));
    }

    /**
     * Handle Save form data
     *
     * @param VisitorMessageRequest $request
     * @param $career
     * @return void
     */

    private function saveData(CareerRequest $request, $career = null):void
    {
        $this->dispatch(new VisitorMessagesJob($request->all(), $this->viewDirectory.'email'));

        CareerReply::create([
            'reply' => $request->reply,
            'visitor_email' => $request->visitor_email,
            'user_id' => Auth::id(),
            'career_id' => $career ? $career->id : null,
        ]);

        $career ? $career->update(['reply' => 1]) : null;
    }


    /**
     * Remove the specified resource from storage.
     *
     * @param Request $request
     * @param int $id
     * @return JsonResponse
     * @throws Exception
     */
    public function destroy(Request $request, int $id) :JsonResponse
    {
        $career = Career::withTrashed()->find($id);
        if ($career) {
            if ($career->deleted_at) {
                $career->forceDelete();
            } else {
                $career->delete();
            }
            return response()->json(['message'=> __('dashboard.deletedDone')]);
        } else{
            return response()->json(['message'=> __('dashboard.noResult')], 400);
        }
    }

    /**
     * Remove several pages by IDs.
     *
     * @param Request $request
     * @return JsonResponse
     */
    public function destroyAll(Request $request) :JsonResponse
    {
        $ids= $request->ids;
        if ($request->force) {
            Career::onlyTrashed()->whereIn('id', $ids)->forceDelete();
        } else {
            Career::whereIn('id', $ids)->delete();
        }

        return response()->json(['message'=> __('dashboard.deletedDone')]);
    }

    /**
     * Restore the specified category from storage
     *
     * @param Request $request
     * @param int $id
     * @return JsonResponse
     */
    public function restore(Request $request, int $id) :JsonResponse
    {
        $career = Career::withTrashed()->find($id);

        if ($career){
            $career->restore();
            return response()->json(['message'=> __('dashboard.saveDone')]);
        }
        return response()->json(['message'=> __('dashboard.noResult')], 400);
    }

    /**
     * Restore several pages by IDs.
     *
     * @param Request $request
     * @return JsonResponse
     */
    public function restoreAll(Request $request) :JsonResponse
    {
        $careers = Career::whereIn('id', $request->ids)->onlyTrashed();
        if ($careers){
            $careers->restore();
            return response()->json(['message'=> __('dashboard.saveDone')]);
        }
    }
}
