<?php

namespace Modules\Sector\Repositories;

use Illuminate\Http\Request;
use Mcamara\LaravelLocalization\Facades\LaravelLocalization;
use Modules\Sector\Interfaces\SectorRepositoryInterface;
use Modules\Sector\Models\Sector;
use Modules\Sector\Models\SectorDescription;

class SectorRepository implements SectorRepositoryInterface
{
    public function get(array $request)
    {
        $builder = Sector::query()
            ->join('sector_descriptions AS sd', 'sectors.id', 'sd.sector_id')
            ->join('languages', 'languages.id', 'sd.language_id')
            ->where('local', LaravelLocalization::getCurrentLocale())
            ->select(['sd.title','sd.description', 'sectors.*']);
        return $builder;
    }

    public function getItem(int $id)
    {
        $sector = Sector::find($id);

        $query = SectorDescription::where('sector_id', $sector->id)
            ->join('sectors', 'sector_descriptions.sector_id','sectors.id')
            ->join('languages', 'languages.id', 'sector_descriptions.language_id')
            ->select(['sectors.*','sector_descriptions.title','sector_descriptions.description', 'languages.local']);

        $sectorDescription= $query->get();
        // dd($sectorDescription->toArray());
        foreach ($sectorDescription as $key=>$row) {

            $sector[$row->local] = $row;
        }
        return $sector;
    }

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

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

    public function create(array $request): Sector
    {
        $sector = Sector::create($request);
        return $sector;
    }

    public function update(array $request, ?Sector $sector): Sector
    {
        $sector->update($request);

        return $sector;
    }

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

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

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

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