<?php

namespace Modules\Notification\Repositories;

use Illuminate\Http\Request;
use Mcamara\LaravelLocalization\Facades\LaravelLocalization;
use Modules\Notification\Interfaces\NotificationRepositoryInterface;
use Modules\Notification\Models\Notification;
use Modules\Notification\Models\NotificationDescription;

class NotificationRepository implements NotificationRepositoryInterface
{
    public function get(array $request)
    {
        $builder = Notification::orderBy('id', 'DESC')
            ->join('notification_descriptions AS sd', 'notifications.id', 'sd.notification_id')
            ->join('languages', 'languages.id', 'sd.language_id')
            ->where('local', LaravelLocalization::getCurrentLocale())
            ->select(['sd.title', 'notifications.*']);
        return $builder;
    }

    public function create(array $request)
    {
        return Notification::create($request);
    }

    public function saveData(array $request, int $notification_id ) :void
    {
        NotificationDescription::where('notification_id', $notification_id)->delete();
        foreach (languages() as $lang)
        {
            $data =
                [
                    'notification_id'=> $notification_id,
                    'language_id'=> $lang->id,
                    'title'=> $request['title_'.$lang->local],
                    'description'=> $request['description_'.$lang->local]
                ];
            NotificationDescription::create($data);
        }
    }
}
