<?php

namespace Modules\Partner\Repositories;

use Illuminate\Http\Request;
use Mcamara\LaravelLocalization\Facades\LaravelLocalization;
use Modules\Partner\Interfaces\PartnerRepositoryInterface;
use Modules\Partner\Models\Partner;
use Modules\Partner\Models\PartnerDescription;

class PartnerRepository implements PartnerRepositoryInterface
{
    public function get(Request $request)
    {
        $query = Partner::orderBy('id', 'DESC')
            ->join('partner_descriptions AS sd', 'partners.id', 'sd.partner_id')
            ->join('languages', 'languages.id', 'sd.language_id')
            ->where('local', LaravelLocalization::getCurrentLocale())
            ->select(['sd.title', 'partners.*']);

        if ($request->title) {
            $query->where('sd.title', 'LIKE', '%'.$request->title.'%');
        }
        if ($request->date_from) {
            $query->whereDate('partners.created_at', '>=', $request->date_from);
        }

        if ($request->date_to) {
            $query->whereDate('partners.created_at', '<=', $request->date_to);
        }


        if ( !is_null($request->status) && $request->status == 0) {
            $query->onlyTrashed();
        }

        if ($request->paginate ?? null) {
            $partners= $query->paginate(50);
        }else{
            $partners=$query->cursor();
        }

        return $partners;
    }

    public function getItem(Partner $partner)
    {
        $query = PartnerDescription::where('partner_id', $partner->id)
            ->join('languages', 'languages.id', 'partner_descriptions.language_id')
            ->select(['partner_descriptions.*', 'languages.local']);

        $partnerDescription= $query->get();
        // dd($partnerDescription->toArray());
        foreach ($partnerDescription as $row) {
            $partner[$row->local]= $row;
        }
        return $partner ;
    }

    //
    public function save(Request $request, ?Partner $partner): Partner
    {
        $data= $request->all();
        unset($data['id']);// for security reasons
        if (!$partner) {
            $partner = Partner::create($data);
        } else {
            $partner->update($data);
            PartnerDescription::where('partner_id', $partner->id)->delete();
        }
        $this->saveData( $request, $partner->id );
        return $partner;
    }

    private function saveData(Request $request, int $partner_id ) :void
    {
        $requestData= $request->all();
        foreach (languages() as $lang) {
            $data=[
                'partner_id'=> $partner_id,
                'language_id'=> $lang->id,
                'title'=> $requestData['title_'.$lang->local],
            ];

            PartnerDescription::create($data);
        }
    }
    /**
     * Remove the specified client from storage.
     * @param Request $request
     * @param int $id
     * @return array
     * @throws Exception
     */
    public function destroy(Request $request, int $id) :array
    {
        $partner= Partner::withTrashed()->find($id);
        if ($partner) {
            if ($partner->deleted_at) {
                $partner->forceDelete();
            } else {
                $partner->delete();
            }
            return [ ['message'=> __('dashboard.deletedDone')]];
        } else{
            return [ ['message'=> __('dashboard.noResult')], 400];
        }
    }

    /**
     * Remove several clients by IDs.
     *
     * @param Request $request
     * @return array
     */
    public function destroyAll(Request $request) :array
    {
        $ids= $request->ids;
        $query= Partner::whereIn('id', $ids);
        if ($request->force) {
            $deleted= $query->onlyTrashed()->forceDelete();
        } else {
            $deleted= $query->delete();
        }
        if($deleted){
            return [['message'=> __('dashboard.deletedDone')]];
        }
        return [ ['message'=> __('dashboard.noResult')], 400];
    }

    /**
     * Restore the specified client from storage.
     *
     * @param Request $request
     * @param int $id
     * @return array
     */
    public function restore(Request $request, int $id) :array
    {
        $partner= Partner::withTrashed()->find($id);
        if ($partner) {
            $partner->restore();
            return [['message'=> __('dashboard.saveDone')]];
        }
        return [['message'=> __('dashboard.noResult')], 400];

    }

    /**
     * Restore several clients by IDs.
     *
     * @param Request $request
     * @return array
     */
    public function restoreAll(Request $request) :array
    {
        $ids= $request->ids;
        Partner::whereIn('id', $ids)->onlyTrashed()->restore();
        return [['message'=> __('dashboard.saveDone')]];
    }
}
