<?php

namespace App\Http\Requests\Admin\Manage;

use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Support\Facades\Auth;

class AttendanceRequest extends FormRequest
{
    public function authorize(): bool
    {
        return Auth::check();
    }

    public function rules(): array
    {
        return [
            'image' => ['required', function ($attribute, $value, $fail) {
                if (! preg_match('/^data:image\/(\w+);base64,/', $value)) {
                    $fail('The '.$attribute.' is not a valid image.');
                } else {
                    $imageData = explode(',', $value)[1];
                    $image = base64_decode($imageData);

                    $imageInfo = @getimagesizefromstring($image);
                    if ($imageInfo === false) {
                        $fail('The '.$attribute.' is not a valid image.');
                    }
                }
            }],
        ];
    }
}
