<?php

namespace Lucent\Response;

use Illuminate\Http\Response;
use Illuminate\Support\Facades\Log;
use Lucent\LucentException;
use Throwable;

function fail(Throwable|string|array $error, int $code = 400): Response
{
    $message = "Something went wrong";

    if (is_string($error) || is_array($error)) {
        $message = $error;
    } elseif ($error instanceof LucentException) {
        $message = $error->getMessage();
    } elseif ($error instanceof Throwable) {
        Log::error($error->getMessage());
    }

    return response([
        "error" => $message
    ], $code);
}


function ok(array $data = [], int $code = 200): Response
{
    return response($data, $code);
}
