<?php

namespace App\Http\Controllers\Response;

use App\Http\Controllers\Error\ErrorInterface;
use Illuminate\Http\Response;

class ApiResponse
{
    public static function success(
        array $content,
        int $status = Response::HTTP_OK,
        array $headers = []
    ): Response
    {
        return static::determineResponse($content, $status, false, $headers);
    }

    private static function determineResponse(
        array $content,
        int $status,
        bool $error,
        array $headers = []
    ): Response
    {
        return response(
            [
                'error' => $error,
                'status' => [
                    'code' => $status,
                    'text' => Response::$statusTexts[$status],
                ],
                'content' => $content,
            ],
            $status,
            $headers,
        );
    }

    public static function error(
        ErrorInterface $error,
        array $headers = []
    ): Response
    {
        return static::determineResponse(
            [
                'code' => $error->getErrorCode(),
                'message' => $error->getErrorMessage(),
            ],
            $error->getStatusCode(),
            true,
            $headers,
        );
    }
}
