<?php

namespace Lucent\Util\Form;


use Exception;
use Illuminate\Contracts\Foundation\Application;
use Illuminate\Contracts\Routing\ResponseFactory;
use Illuminate\Http\Response;
use Illuminate\Validation\Validator;
use Lucent\LucentException;

class ResponseFormError
{
    /**
     * @param $errors list<string>
     */
    public static function render(array $errors): ResponseFactory|Application|Response
    {
        return response(view("lucent::forms.errors", ["errors" => $errors])->render(), 400);
    }

    /**
     * @param Validator $validator
     * @return ResponseFactory|Application|Response
     */
    public static function fromValidator(Validator $validator): ResponseFactory|Application|Response
    {

        return self::render($validator->errors()->all());
    }

    /**
     * @param array $errors
     * @return ResponseFactory|Application|Response
     */
    public static function fromArray(array $errors): ResponseFactory|Application|Response
    {
        return self::render($errors);
    }

    /**
     * @param string $errorString
     * @return ResponseFactory|Application|Response
     */
    public static function fromMessage(string $errorString): ResponseFactory|Application|Response
    {
        return self::render([$errorString]);
    }

    /**
     * @param LucentException|FormException $exception
     * @return ResponseFactory|Application|Response
     */
    public static function fromException(LucentException|FormException $exception): ResponseFactory|Application|Response
    {
        return self::render([$exception->getMessage()]);
    }
}
