<?php

namespace Modules\Subscribe\Repositories;

use Illuminate\Http\Request;
use Modules\Subscribe\Interfaces\SubscribeRepositoryInterface;
use Modules\Subscribe\Models\Subscribe;

class SubscribeRepository implements SubscribeRepositoryInterface
{


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

        if ($request->package)
        {
            $query->whereHas('package.currentDescription',function ($q) use ($request){
                $q->where('title', 'LIKE', '%'.$request->package.'%');
            });
        }
        if ($request->package)
        {
            $query->whereHas('store',function ($q) use ($request){
                $q->where('name', 'LIKE', '%'.$request->store.'%');
            });
        }

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

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

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

        return $subscribes;
    }

    public function getItem(Subscribe $subscribe)
    {
        return $subscribe ;
    }

    //
    public function create(Request $request): Subscribe
    {
        $data= $request->all();
        unset($data['id']);// for security reasons
        $subscribe = Subscribe::create($data);
        return $subscribe;
    }
    public function update(Request $request,Subscribe $subscribe): Subscribe
    {
        $subscribe->update($request->all());
        return $subscribe;
    }

    private function saveData(Request $request, int $subscribe_id ) :void
    {
        $account = $this->accountService->getAccount();
        $request->merge(['status'=>$account['status'],'user_id'=>$account['user']->id]);
        $carts = $this->cartService->get($request);
        foreach ($carts as $row) {
            $data=[
                'count'=> $row->count,
                'product_id'=> $row->product_id,
                'product_cost'=>$row->product->price,
                'total'=> $row->product->price * $row->count,
                'main_attribute_id'=>$row->main_attribute_id ?? null,
                'sub_attribute_id'=>$row->sub_attribute_id ?? null,
                'subscribe_id'=> $subscribe_id,
            ];
            SubscribeProduct::create($data);
        }
    }



    public function getItemByToken(string $token)
    {
        $subscribe = Subscribe::onlyTrashed()->where('token',$token)->first();
        return $subscribe;
    }

    public function updateSubscribe(array $content,Subscribe $subscribe)
    {
        $subscribe->update($content);
    }
}
