<?php

namespace Modules\Service\Repositories;

use Mcamara\LaravelLocalization\Facades\LaravelLocalization;
use Modules\Service\Models\Service;
use Modules\Service\Models\ServiceDescription;
use Modules\Service\Interfaces\ServiceRepositoryInterface;

class ServiceRepository implements ServiceRepositoryInterface
{
    public function get(array $request)
    {
        $builder = Service::query()
            ->join('service_descriptions AS sd', 'services.id', 'sd.service_id')
            ->join('languages', 'languages.id', 'sd.language_id')
            ->where('local', LaravelLocalization::getCurrentLocale())
            ->select(['sd.title','sd.description', 'services.*']);
        return $builder;
    }

    public function getItem(int $id)
    {
        return ServiceDescription::where('service_id', $id)
            ->join('languages', 'languages.id', 'service_descriptions.language_id')
            ->join('services', 'services.id', 'service_descriptions.service_id')
            ->select(['service_descriptions.*', 'languages.local','services.*'])->get();
    }

    public function checkItem(int $id)
    {
        return Service::find($id);
    }

    public function getItemTrashed(int $id)
    {
        $service = Service::onlyTrashed()->find($id);
        return $service ;
    }

    public function create(array $request): Service
    {
        $service = Service::create($request);
        return $service;
    }

    public function update(array $request, ?Service $service): Service
    {
        $service->update($request);

        return $service;
    }

    public function saveData(array $request, int $service_id ) :void
    {
        ServiceDescription::where('service_id', $service_id)->delete();
        // dd($request);
        foreach (languages() as $lang) {
            $data=[
                'service_id'=> $service_id,
                'language_id'=> $lang->id,
                'title'=> $request['title_'.$lang->local],
                'description'=> $request['description_'.$lang->local],
            ];
            ServiceDescription::create($data);
        }
    }
    /**
     * Remove the specified service from storage.
     * @param Service $service
     * @return void
     */
    public function destroy(Service $service) :void
    {
        $service->delete();
    }

    /**
     * Remove several services by IDs.
     *
     * @param array $request
     * @return void
     */
    public function destroyAll(array $ids) :void
    {
        Service::whereIn('id', $ids)->delete();
    }

    /**
     * Restore the specified service from storage.
     *
     * @param Service $service
     * @return void
     */
    public function restore(Service $service) :void
    {
        $service->restore();
    }

    /**
     * Restore several services by IDs.
     *
     * @param array $request
     * @return void
     */
    public function restoreAll(array $ids) :void
    {
        Service::whereIn('id', $ids)->onlyTrashed()->restore();
    }

    /**
     * Permanently delete the specified service from storage.
     *
     * @param Service $service
     * @return void
     */
    public function forceDelete(Service $service) :void
    {
        $service->forceDelete();
    }

    /**
     * Permanently delete several services by IDs.
     *
     * @param array $ids
     * @return void
     */
    public function forceDeleteAll(array $ids) :void
    {
        Service::whereIn('id', $ids)->onlyTrashed()->forceDelete();
    }
}
