<?php

namespace Modules\Subscription\Repositories;

use Illuminate\Http\Request;
use Mcamara\LaravelLocalization\Facades\LaravelLocalization;
use Modules\Subscription\Interfaces\SubscriptionRepositoryInterface;
use Modules\Subscription\Models\Subscription;
use Modules\Subscription\Models\SubscriptionDescription;

class SubscriptionRepository implements SubscriptionRepositoryInterface
{
    public function get(array $request)
    {
        $builder = Subscription::query();
        return $builder;
    }

    public function getItem(int $id)
    {
        return Subscription::find($id);
    }

    public function checkItem(int $id)
    {
        return Subscription::find($id);
    }

    public function getItemTrashed(int $id)
    {
        $subscription = Subscription::onlyTrashed()->find($id);
        return $subscription ;
    }

    public function create(array $request): Subscription
    {
        $subscription = Subscription::create($request);
        return $subscription;
    }

    public function update(array $request, ?Subscription $subscription): Subscription
    {
        $subscription->update($request);

        return $subscription;
    }

    public function saveData(array $request, int $subscription_id ) :void
    {
        SubscriptionDescription::where('subscription_id', $subscription_id)->delete();
        foreach (languages() as $lang) {
            $data=[
                'subscription_id'=> $subscription_id,
                'language_id'=> $lang->id,
                'title'=> $request['title_'.$lang->local],
            ];
            SubscriptionDescription::create($data);
        }
    }
    /**
     * Remove the specified subscription from storage.
     * @param Subscription $subscription
     * @return void
     */
    public function destroy(Subscription $subscription) :void
    {
        $subscription->delete();
    }

    /**
     * Remove several subscriptions by IDs.
     *
     * @param array $request
     * @return void
     */
    public function destroyAll(array $ids) :void
    {
         Subscription::whereIn('id', $ids)->delete();
    }

    /**
     * Restore the specified subscription from storage.
     *
     * @param Subscription $subscription
     * @return void
     */
    public function restore(Subscription $subscription) :void
    {
        $subscription->restore();
    }

    /**
     * Restore several subscriptions by IDs.
     *
     * @param array $request
     * @return void
     */
    public function restoreAll(array $ids) :void
    {
        Subscription::whereIn('id', $ids)->onlyTrashed()->restore();
    }
}
