<?php
namespace App\Providers;

use Illuminate\Support\Facades\View;
use Illuminate\Support\ServiceProvider;
use Illuminate\Support\Facades\Validator;

class Base64ImageValidationServiceProvider extends ServiceProvider
{
    /**
     * Bootstrap the application services.
     *
     * @return void
     */
    public function boot()
    {
        Validator::extend('image64', 'App\Validators\Base64Validator@validateBase64');

        Validator::replacer('image64', function ($message, $attribute, $rule, $parameters) {
            $settings = View::shared('getSettings');
            $maxKb = is_array($settings) ? ($settings['max_allowed_image_size'] ?? '') : '';
            return str_replace(':max_kb', (string) $maxKb, $message);
        });

        // HH:MM or HH:MM:SS — do not use regex:... in rule strings (| splits Laravel rules).
        Validator::extend('time24', function ($attribute, $value, $parameters, $validator) {
            if ($value === null || $value === '') {
                return true;
            }
            return preg_match('/^([01]?[0-9]|2[0-3]):[0-5][0-9](:[0-5][0-9])?$/', (string) $value) === 1;
        });
    }

    /**
     * Register services.
     *
     * @return void
     */
    public function register()
    {
        //
    }
}
