<?php

namespace App\Classes\Modules\Milestones\Services;

use App\Classes\General\Eloquent\AbstractUpdateRelationshipRecord;
use App\Models\Milestone;
use App\Models\MilestoneProgress;

class CreatesMilestoneProgress extends AbstractUpdateRelationshipRecord
{
    /** @var CheckIfMilestoneProgressExists */
    private $isMilestoneProgressExists;

    /**
     * CreatesMilestoneProgress constructor.
     * @param CheckIfMilestoneProgressExists $isMilestoneProgressExists
     */
    public function __construct(CheckIfMilestoneProgressExists $isMilestoneProgressExists)
    {
        $this->isMilestoneProgressExists = $isMilestoneProgressExists;
    }


    /**
     * @param Milestone $milestone
     * @param string $userId
     * @return \Illuminate\Database\Eloquent\Model|null
     */
    public function execute(Milestone $milestone, string $userId) {
        if(!$this->isMilestoneProgressExists->execute($userId, $milestone->id))
        {
            $model = new MilestoneProgress();
            $model->user_id = $userId;

            return $this->handler($milestone->progress(), $model);
        }

        return null;
    }
}
