<?php

namespace Modules\Store\Repositories;

use Illuminate\Http\Request;
use Modules\Store\Interfaces\StoreRepositoryInterface;
use Modules\Store\Models\Store;
use Modules\Store\Models\StoreWork;

class StoreRepository implements StoreRepositoryInterface
{

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

        if ($request->name)
        {
            $query->where('name','LIKE', '%'.$request->name.'%');
        }

        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) {
            $stores= $query->paginate(50);
        }else{
            $stores=$query->cursor();
        }

        return $stores;
    }

    public function getItem(Store $store)
    {
        return $store ;
    }

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

    private function saveData(Request $request, int $store_id ) :void
    {
        $requestData= $request->all();

        if (isset($requestData['days']))
        {
            foreach ($request->days as $key=>$row)
            {
                StoreWork::create([
                    'start_time' => $row['start_time'],
                    'end_time' => $row['end_time'],
                    'store_id' => $store_id,
                    'day' => $key,
                ]);
            }
        }
    }
    public function products(Store $store)
    {
        $products = StoreProduct::where('store_id',$store->id)->cursor();
        return $products;
    }

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

}
