<?php

namespace Modules\DealStatus\Repositories;

use Mcamara\LaravelLocalization\Facades\LaravelLocalization;
use Modules\DealStatus\Interfaces\DealStatusRepositoryInterface;
use Modules\DealStatus\Models\DealStatus;
use Modules\DealStatus\Models\DealStatusDescription;

class DealStatusRepository implements DealStatusRepositoryInterface
{
    public function get(array $request)
    {
        $builder = DealStatus::orderBy('id', 'DESC')
            ->join('deal_status_descriptions AS sd', 'deal_statuses.id', 'sd.deal_status_id')
            ->join('languages', 'languages.id', 'sd.language_id')
            ->where('local', LaravelLocalization::getCurrentLocale())
            ->select(['sd.title', 'deal_statuses.*']);
        return $builder;
    }

    public function getItem(int $id)
    {
        $dealStatus = DealStatus::find($id);

        $query = DealStatusDescription::where('deal_status_id', $dealStatus->id)
            ->join('deal_statuses', 'deal_status_descriptions.deal_status_id','deal_statuses.id')
            ->join('languages', 'languages.id', 'deal_status_descriptions.language_id')
            ->select(['deal_statuses.*','deal_status_descriptions.title', 'languages.local']);

        $dealStatusDescription= $query->get();

        foreach ($dealStatusDescription as $key=>$row)
        {
            $dealStatus[$row->local] = $row;
        }
        return $dealStatus;
    }

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

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

    public function create(array $request): DealStatus
    {
        $deal = DealStatus::create($request);
        return $deal;
    }

    public function update(array $request, ?DealStatus $dealStatus): DealStatus
    {
        $dealStatus->update($request);

        return $dealStatus;
    }

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

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

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

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