<?php

namespace App\Classes\Modules\Announcements\Services;

use App\Classes\General\Eloquent\AbstractUpdateRecord;
use App\Classes\Modules\Announcements\DataTransferObjects\AnnouncementObject;
use App\Models\Announcement;
use DateTime;

class CreatesAnnouncement extends AbstractUpdateRecord
{
    /**
     * @param AnnouncementObject $object
     * @return \Illuminate\Database\Eloquent\Model
     * @throws \App\Classes\Exceptions\MalformedRequestException
     */
    public function execute(AnnouncementObject $object)
    {
        $model = new Announcement();
        $model->title = $object->getTitle();
        $model->description = $object->getDescription();
        // $model->starting_on = $object->getStartingOn();
        $model->starting_on = $this->convertDateFormat($object->getStartingOn());
        // $model->ending_on = $object->getEndingOn();
        $model->ending_on = $this->convertDateFormat($object->getEndingOn());

        return $this->handler($model);
    }

    public function convertDateFormat($value) {
        $value = DateTime::createFromFormat('d-m-Y', $value)->format('Y-m-d H:i:s');

        return $value;
    }
}
