<?php

namespace Modules\Recruit\Http\Controllers;

use App\Http\Controllers\Controller;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Modules\Admin\Repositories\DepartmentInterface;
use Modules\Admin\Repositories\DesignationInterface;
use Modules\Recruit\Repositories\JobPostInterface;
use Modules\Recruit\Repositories\JobPostRepository;
use Yoeunes\Toastr\Facades\Toastr;

class JobPostController extends Controller
{
    private $jobPostRepository;
    private $departmentRepository;
    private $designationRepository;

    public function __construct(JobPostInterface $jobPostRepository, DepartmentInterface $departmentRepository, DesignationInterface $designationRepository)
    {
        $this->jobPostRepository = $jobPostRepository;
        $this->departmentRepository = $departmentRepository;
        $this->designationRepository = $designationRepository;

    }

    /**
     * Display a listing of the resource.
     */
    public function index()
    {
        $data['title'] = 'Job Post Lists';
        $data['jobPostLists'] = $this->jobPostRepository->findAll();

        return view('recruit::jobposts.index', $data);
    }

    /**
     * Show the form for creating a new resource.
     */
    public function create()
    {
        $data['title'] = 'Create Job Post';
        $data['editable'] = false;
        $data['departmentLists'] = $this->departmentRepository->pluck();
        $data['designationLists'] = $this->designationRepository->pluck();
        return view('recruit::jobposts.create', $data);
    }

    /**
     * Store a newly created resource in storage.
     */
    public function store(Request $request): RedirectResponse
    {
        $request->mergeIfMissing([
            'createdBy' => auth()->user()->employee?->id,
        ]);

        try {
            $this->jobPostRepository->create($request->all());
            toastr()->success('JobPost Created Succesfully');
        } catch (\Throwable $th) {
            toastr()->error($th->getMessage());
        }
        return redirect()->route('jobPost.index');
    }

    /**
     * Show the specified resource.
     */
    public function show($id)
    {
        return view('recruit::jobposts.show');
    }

    /**
     * Show the form for editing the specified resource.
     */
    public function edit($id)
    {
        $data['title'] = 'Edit Job Post';

        $data['editable'] = true;

        $data['departmentLists'] = $this->departmentRepository->pluck();
        $data['designationLists'] = $this->designationRepository->pluck();

        $data['jobPost'] = $this->jobPostRepository->getJobPostById($id);

        return view('recruit::jobposts.edit', $data);
    }

    /**
     * Update the specified resource in storage.
     */
    public function update(Request $request, $id): RedirectResponse
    {
        $request->mergeIfMissing([
            'updateBy' => auth()->user()->employee?->id,
        ]);

        $inputData = $request->all();

        try {
            $this->jobPostRepository->update($id, $inputData);

            toastr()->success('Job Post Updated Succesfully');

        } catch (\Throwable $th) {
            toastr()->error($th->getMessage());
        }
        return redirect()->route('jobPost.index');
    }

    /**
     * Remove the specified resource from storage.
     */
    public function destroy($id)
    {
        try {
            $this->jobPostRepository->delete($id);

            toastr()->success('Job Post Deleted Succesfully');
        } catch (\Throwable $th) {
            //throw $th;
            toastr()->error($th->getMessage());
        }

    }
}