<?php

namespace Modules\Store\Repositories;

use Illuminate\Http\Request;
use Modules\Store\Enums\StoreStatusActiveEnum;
use Modules\Store\Interfaces\EditStoreRepositoryInterface;
use Modules\Store\Models\EditStore;
use Modules\Store\Models\Store;
use Modules\Store\Models\StoreLog;

class EditStoreRepository implements EditStoreRepositoryInterface
{

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

        if ($request->store)
        {
            $query->whereHas('store',function ($q) use ($request){
                $q->where('name', $request->store);
            });
        }

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

        return $editStores;
    }

    public function getItem(EditStore $editStore)
    {
        return $editStore ;
    }

    //
    public function save(array $data, ?EditStore $editStore): EditStore
    {
        unset($data['id']);// for security reasons
        if (!$editStore) {
            $editStore = EditStore::create($data);
        } else {
            if ($data['status'] == StoreStatusActiveEnum::INACTIVE->value)
            {
                StoreLog::create([
                    'reason_id'=>$data['reason_id'],
                    'message'=>$data['message'],
                    'store_id'=>$editStore->store_id
                ]);
            }
            $store = Store::find($editStore->store_id);
            $store->update($data);
        }
        return $editStore;
    }

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

}
