<?php

namespace Mehrab\Service\Http\Controllers;

use App\Http\Controllers\Controller;
use Illuminate\Http\JsonResponse;

use Illuminate\Http\Request;
use Mehrab\City\Repositories\CityRepo;
use Mehrab\Media\Models\Media;
use Mehrab\Media\Services\MediaFileService;
use Mehrab\Region\Models\Region;
use Mehrab\Region\Repositories\RegionRepo;
use Mehrab\Service\Http\Requests\service_description_store_request;
use Mehrab\Service\Http\Requests\service_price_store_request;
use Mehrab\Service\Http\Requests\service_question_store_request;
use Mehrab\Service\Http\Requests\service_store_parent_request;
use Mehrab\Service\Http\Requests\service_store_request;
use Mehrab\Service\Http\Requests\service_sync_region_request;
use Mehrab\Service\Http\Requests\service_update_parent_request;
use Mehrab\Service\Http\Requests\service_update_request;
use Mehrab\Service\Http\Requests\set_price_on_option_question_request;
use Mehrab\Service\Http\Requests\set_priority_on_question_request;
use Mehrab\Service\Http\Requests\update_price_on_option_question_request;
use Mehrab\Service\Models\Service;
use Mehrab\Service\Models\ServiceMeta;
use Mehrab\Service\Repositories\QuestionRepo;
use Mehrab\Service\Repositories\ServiceMetaRepo;
use Mehrab\Service\Repositories\ServiceRepo;
use Mehrab\Service\Repositories\UnitRepo;


class ServiceController extends Controller
{
    private $serviceRepo;

    public function __construct(ServiceRepo $serviceRepo)
    {
        $this->serviceRepo = $serviceRepo;
    }

    public function index(Request $request)
    {

        $this->authorize('manage', Service::class);
        $services = $this->serviceRepo->all($request);
        return view('Service::Admin.list_service', compact('services'));
    }

    public function show($id)
    {
        $this->authorize('manage', Service::class);
        $service = $this->serviceRepo->find_with_all_relations($id);
        return view('Service::Admin.show_service', compact('service'));
    }

    public function create()
    {
        $this->authorize('create', Service::class);
        $services = $this->serviceRepo->all_Parent();
        return view('Service::Admin.create_service', compact('services'));
    }

    public function store(service_store_request $request)
    {
        $this->authorize('create', Service::class);
        if ($request->img) {
            $idMedia = (MediaFileService::publicUpload($request->img)->id);
        }
        $parent = $this->serviceRepo->findById($request->parent_id);
        $this->serviceRepo->store($request, $parent, $idMedia ?? null);
        return redirect()->route('service.index')->with('status', 'created');
    }

    public function store_parent(service_store_parent_request $request)
    {
        $this->authorize('create', Service::class);
        if ($request->img_parent) {
            $idMedia = (MediaFileService::publicUpload($request->img_parent)->id);
        }
        $this->serviceRepo->store_parent($request, $idMedia ?? null);
        return redirect()->back()->with('status', 'created');
    }

    public function edit($serviceID)
    {
        $this->authorize('edit', Service::class);
        $service = $this->serviceRepo->findById($serviceID);
        $services = $this->serviceRepo->all_Parent();

        return view('Service::Admin.edit_service', compact('service', 'services'));
    }

    public function update(service_update_request $request, $id)
    {
        $this->authorize('edit', Service::class);
        if ($request->img) {
            $idMedia = (MediaFileService::publicUpload($request->img)->id);
        }
        $this->serviceRepo->update($request, $id, $idMedia ?? null);
        return redirect()->route('service.index')->with('status', 'updated');
    }

    public function update_parent(service_update_parent_request $request, $id)
    {
        $this->authorize('edit', Service::class);
        if ($request->img_parent) {
            $idMedia = (MediaFileService::publicUpload($request->img_parent)->id);
        }
        $this->serviceRepo->update_parent($request, $id, $idMedia ?? null);
        return redirect()->route('service.index')->with('status', 'updated');
    }

    public function status($id)
    {
        $this->authorize('status', Service::class);
        return $this->serviceRepo->status($id);
    }

    public function destroy($id)
    {
        $this->authorize('status', Service::class);
//       return  $this->serviceRepo->delete($id);
    }

    public function regions(CityRepo $cityRepo, $service)
    {
        $this->authorize('region', Service::class);
        $cities = $cityRepo->all_with_regions();
        $service = $this->serviceRepo->find_with_region($service);
        return view('Service::Admin.region_service', compact('cities', 'service'));
    }

    public function regions_sync(service_sync_region_request $request, RegionRepo $regionRepo, $service)
    {
        $this->authorize('region', Service::class);
        $service = $this->serviceRepo->findById($service);
        if ($request->regions) {
//                $regions = $regionRepo->find_with_where_in($request->regions);
            $service->regions()->sync($request->regions);
        } else {
            $service->regions()->sync([]);
        }
        return redirect()->back()->with('status', 'synced');

    }

    public function description($service)
    {
        $this->authorize('description', Service::class);
        $service = $this->serviceRepo->findById($service);
        return view('Service::Admin.description_service', compact('service'));
    }

    public function description_store(service_description_store_request $request, $service)
    {
        $this->authorize('description', Service::class);
        $this->serviceRepo->description_store($request, $service);
        return redirect()->back()->with('status', 'created');
    }

    public function description_delete($service, $desc)
    {
        $this->authorize('description', Service::class);
        $this->serviceRepo->description_delete($service, $desc);
    }

    public function question($service, QuestionRepo $questionRepo)
    {
        $this->authorize('question', Service::class);
        $service = $this->serviceRepo->find_with_question($service);
        $service_parent_questions = $service->questions->where("parent_id",null)->where("parent_question",0);
        return view('Service::Admin.question_service', compact('service','service_parent_questions'));
    }

    public function question_store(service_question_store_request $request, QuestionRepo $questionRepo, $service)
    {
        $this->authorize('question', Service::class);
        try {
            $question = $this->serviceRepo->question_store($request, $service);
            $questionRepo->save_options_for_question($request->options, $question->id);
            return redirect()->back()->with('status', 'created');
        } catch (\Exception $exception) {
            return redirect()->back()->with('status', 'error');
        }
    }

    public function question_price(set_price_on_option_question_request $request, QuestionRepo $questionRepo, $optionID)
    {
        $this->authorize('question', Service::class);
        $question = $questionRepo->set_price_options($request, $optionID);
         updated($question);

    }

    public function question_priority(set_priority_on_question_request $request, QuestionRepo $questionRepo, $optionID)
    {
        $this->authorize('question', Service::class);
        $question = $questionRepo->set_priority_options($request, $optionID);
        updated($question);

    }
    public function question_set_parent_question(Request $request, QuestionRepo $questionRepo, $optionID)
    {
        $this->authorize('question', Service::class);
        $question = $questionRepo->set_parent_question_options($request, $optionID);
        updated($question);

    }

    public function question_delete(QuestionRepo $questionRepo, $service, $question)
    {
        $this->authorize('question', Service::class);
        $questionRepo->question_delete($service, $question);
    }

    public function price(ServiceMetaRepo $serviceMetaRepo, $service)
    {
        $this->authorize('price', Service::class);
        $prices = $serviceMetaRepo->find_prices_service($service);
        $service = $this->serviceRepo->findById($service);
        return view('Service::Admin.price_service', compact('prices','service'));
    }

    public function price_store(service_price_store_request $request,ServiceMetaRepo $serviceMetaRepo, $service)
    {
        $this->authorize('price', Service::class);
        $serviceMetaRepo->price_for_service_store($request,$service);
        return redirect()->back()->with('status', 'created');
    }

    public function price_delete(ServiceMetaRepo $serviceMetaRepo,$price)
    {
        $this->authorize('price', Service::class);
        $serviceMetaRepo->price_delete($price);
    }

    public function edit_price(ServiceMetaRepo $serviceMetaRepo,$priceId,$serviceId)
    {
        $this->authorize('price', Service::class);
        $price = $serviceMetaRepo->find_price_service($priceId);
        return view('Service::Admin.price_service_edit', compact('price','serviceId'));

    }

    public function update_price(update_price_on_option_question_request $request,ServiceMetaRepo $serviceMetaRepo,$priceId,$serviceId)
    {
        $this->authorize('price', Service::class);
        $newPrice = $serviceMetaRepo->update_price($request,$priceId);
        return  updated($newPrice,"service.price",[$serviceId]);

    }

    public function delete_service($service){
        $this->authorize('manage', Service::class);

        $parent = $this->serviceRepo->findById($service);
        $parent->children()->delete();
        $parent->delete();
        return redirect()->back()->with('status', 'success');

    }

}
