<?php

namespace Modules\User\Repositories;

use Illuminate\Http\Request;
use Modules\User\Interfaces\AddressRepositoryInterface;
use Modules\User\Models\Address;

class AddressRepository implements AddressRepositoryInterface
{
    public function get(Request $request)
    {
        $query = Address::orderBy('id', 'DESC');

        if ($request->user_id) {
            $query->where('user_id',$request->user_id);
        }
        if ($request->user_status) {
            $query->where('user_status',$request->user_status);
        }
        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) {
            $addresses= $query->paginate(50);
        }else{
            $addresses=$query->cursor();
        }

        return $addresses;
    }

    public function getItem(Address $address)
    {
        return $address ;
    }

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

        if ($request->modules){
            $address->modules()->sync($request->modules);
        }

        return $address;
    }

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


}
