<?php

namespace App\Services;

use Carbon\Carbon;
use App\Repositories\CourseNotification;

class CourseNotificationService
{
    private $coursenotificationRepository;

    public function __construct(CourseNotification $coursenotificationRepository)
    {
        $this->coursenotificationRepository = $coursenotificationRepository;
    }

    public function save(array $data)
    {
        $time = explode(':', $data['time']);
        $hours = $time[0];
        $minutes = $time[1];

        $data['date'] = Carbon::parse($data['date'])
            ->copy()
            ->addHours($hours)
            ->addMinutes($minutes);

        if (isset($data['id'])) {
            $coursenotification = $this->coursenotificationRepository->findOrFail($data['id']);
            $coursenotification->update($data);
        } else {
            $coursenotification = $this->coursenotificationRepository->create($data);
        }

        return $coursenotification;
    }

    public function delete($id)
    {
        $coursenotification = $this->coursenotificationRepository->findOrFail($id);
        $coursenotification->delete();
    }

    public function get($id)
    {
        return $this->coursenotificationRepository->findOrFail($id);
    }

    public function getAll()
    {
        return $this->coursenotificationRepository->all();
    }

    public function getNotExpired()
    {
        return $this->coursenotificationRepository->NotExpired()->get();
    }

    public function getExpiredToSendWithProducts()
    {
        return $this->coursenotificationRepository
            ->with(['learning_product'])
            ->Expired()
            ->NotSent()
            ->get();
    }

    public function getExpiringToSend()
    {
        return $this->coursenotificationRepository->ExpireInFiveMinutes()->NotSent()->get();
    }

    public function with(array $children)
    {
        return $this->coursenotificationRepository->with($children)->get();
    }

    public function getOneWith($id, array $children)
    {
        return $this->coursenotificationRepository->with($children)->findOrFail($id);
    }
}
