<?php

declare(strict_types=1);

namespace App\Support;

use Illuminate\Http\JsonResponse;
use Illuminate\Http\Resources\Json\ResourceCollection;

class ApiResponse
{
    private static function trans(string $key): string
    {
        return trans($key, [], 'fa');
    }

    public static function response(
        mixed $data = null,
        ?string $next = null,
        string|array|null $message = null,
        int $status = 200,
        array|string|null $errors = null,
        array $meta = []
    ): JsonResponse {
        $response = [
            'success' => $status < 400,
            'message' => $message,
            'data' => $data,
            'errors' => $errors,
            'meta' => $meta,
            'next' => $next,
        ];

        return response()->json(
            array_filter(
                $response,
                fn ($value) => $value !== null
            ),
            $status
        );
    }

    public static function success(
        mixed $data = null,
        ?string $message = null
    ): JsonResponse {
        $meta = [];
        $links = [];

        if ($data instanceof ResourceCollection) {
            $responseData = $data->response()->getData(true);

            $meta = $responseData['meta'] ?? [];
            $links = $responseData['links'] ?? [];
            $data = $responseData['data'] ?? [];
        }

        return self::response(
            data: $data,
            message: $message ?? self::trans('api.success'),
            meta: [
                ...$meta,
                'links' => $links,
            ]
        );
    }

    public static function created(
        mixed $data = null,
        ?string $message = null
    ): JsonResponse {
        return self::response(
            data: $data,
            message: $message ?? self::trans('api.created'),
            status: 201
        );
    }

    public static function badRequest(
        string|array|null $errors = null
    ): JsonResponse {
        return self::response(
            status: 400,
            errors: $errors ?? self::trans('api.bad_request')
        );
    }

    public static function unauthorized(
        string|array|null $errors = null
    ): JsonResponse {
        return self::response(
            status: 401,
            errors: $errors ?? self::trans('api.unauthorized')
        );
    }

    public static function forbidden(
        string|array|null $errors = null
    ): JsonResponse {
        return self::response(
            status: 403,
            errors: $errors ?? self::trans('api.forbidden')
        );
    }

    public static function notFound(
        string|array|null $errors = null
    ): JsonResponse {
        return self::response(
            status: 404,
            errors: $errors ?? self::trans('api.not_found')
        );
    }

    public static function noContent(): JsonResponse
    {
        return response()->json(null, 204);
    }

    public static function serverError(
        string|array|null $errors = null
    ): JsonResponse {
        return self::response(
            status: 500,
            errors: $errors ?? self::trans('api.server_error')
        );
    }
}
