<?php

namespace Modules\Notification\Repositories;

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

class ManagementNotificationRepository implements ManagementNotificationRepositoryInterface
{
    public function get(array $request)
    {
        $builder = ManagementNotification::orderBy('id', 'DESC')
            ->join('management_notification_descriptions AS sd', 'management_notifications.id', 'sd.management_notification_id')
            ->join('languages', 'languages.id', 'sd.language_id')
            ->where('local', LaravelLocalization::getCurrentLocale())
            ->select(['sd.title', 'management_notifications.*']);
        return $builder;
    }

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

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

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

    public function getItem(int $id)
    {
        return ManagementNotificationDescription::where('management_notification_id', $id)
            ->join('languages', 'languages.id', 'management_notification_descriptions.language_id')
            ->join('management_notifications', 'management_notifications.id', 'management_notification_descriptions.management_notification_id')
            ->select(['management_notification_descriptions.*', 'languages.local','management_notifications.*'])->get();
    }
}
