<?php

namespace App\Classes\Modules\Milestones\Standards\Validators;


use App\Classes\General\Abstracts\AbstractValidation;
use App\Classes\Modules\Milestones\DataTransferObjects\MilestoneObject;
use App\Models\Milestone;

class MilestoneValidation extends AbstractValidation
{
    /**
     * @param MilestoneObject $object
     * @return array
     */
    protected function data($object): array {

        $data = [
            'name'=> $object->getName(),
            'description' => $object->getDescription(),
        ];

        return $data;
    }

    /**
     * @param null|string $type
     * @return array
     */
    protected function rules(?string $type = 'POST'): array {
        if ($type == 'POST') { //crete
            return [
                'name' => [
                    'required',
                    function ($attribute, $value, $fail) {
                        // Check if milestone name already exists in the database
                        $existingMilestone = Milestone::where('name', $value)->first();
                        if ($existingMilestone) {
                            $fail("The {$attribute} milestone name already exists in the database.");
                        }
                    },
                ],
                'description' => [
                    'required',
                ]
            ];

        }
        elseif($type == 'PUT') { //update
            return [
                'name' => [
                    'required',
                ],
                'description' => [
                    'required',
                ]
            ];
        }
    }

    /**
     * @return array
     */
    protected function messages(): array {
        return [];
    }

}
