<?php

namespace App\Rules;

use Illuminate\Contracts\Validation\InvokableRule;
use Illuminate\Support\Carbon;

class GameReleaseDate implements InvokableRule
{
    /**
     * Run the validation rule.
     *
     * @param  string  $attribute
     * @param  mixed  $value
     * @param  \Closure(string): \Illuminate\Translation\PotentiallyTranslatedString  $fail
     * @return void
     */
    public function __invoke($attribute, $value, $fail)
    {
        if ($value === 'TBD' || $value === 'EA') {
            return true;
        }

        try {
            Carbon::createFromFormat('F j, Y', $value);
        } catch (\Exception $e) {
            $fail('The release date must be in the format of Month Day, Year (ex. March 10, 2000), TBD or EA.');
        }

        return true;
    }
}
