<?php

namespace Modules\Store\Repositories;

use Illuminate\Http\Request;
use Modules\Store\Interfaces\BranchRepositoryInterface;
use Modules\Store\Models\Branch;

class BranchRepository implements BranchRepositoryInterface
{

    public function get(Request $request)
    {
        $query = Branch::orderBy('id', 'DESC');

        if ($request->name)
        {
            $query->where('name','LIKE', '%'.$request->name.'%');
        }
        if ($request->store)
        {
            $query->whereHas('store',function ($q) use ($request){
                $q->where('name', $request->store);
            });
        }
        if ($request->store_id)
        {
            $query->where('store_id',$request->store_id);
        }
        if ($request->area_id)
        {
            $query->where('area_id',$request->area_id);
        }
        if ($request->city_id)
        {
            $query->where('city_id',$request->city_id);
        }

        if ($request->date_from) {
            $query->whereDate('created_at', '>=', $request->date_from);
        }

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


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

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

        return $branches;
    }

    public function getItem(Branch $branch)
    {
        return $branch ;
    }

    //
    public function save(Request $request, ?Branch $branch): Branch
    {
        $data= $request->all();
        unset($data['id']);// for security reasons
        if (!$branch) {
            $branch = Branch::create($data);
        } else {
            $branch->update($data);
        }
        return $branch;
    }

    /**
     * Remove the specified client from storage.
     * @param Request $request
     * @param int $id
     * @return array
     * @throws Exception
     */
    public function destroy(Request $request, int $id) :array
    {
        $branch= Branch::withTrashed()->find($id);
        if ($branch) {
            if ($branch->deleted_at) {
                $branch->forceDelete();
            } else {
                $branch->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= Branch::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
    {
        $branch= Branch::withTrashed()->find($id);
        if ($branch) {
            $branch->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;
        Branch::whereIn('id', $ids)->onlyTrashed()->restore();
        return [['message'=> __('dashboard.saveDone')]];
    }

}
