<?php

namespace App\Core\Exceptions;

use Exception;
use Illuminate\Http\JsonResponse;

class ServiceException extends Exception
{
    protected string $errorCode;
    protected int $statusCode;
    protected mixed $data;

    public function __construct(
        string $message = 'Service error',
        string $errorCode = 'SERVICE_ERROR',
        int $statusCode = 400,
        mixed $data = null
    ) {
        parent::__construct($message);
        $this->errorCode = $errorCode;
        $this->statusCode = $statusCode;
        $this->data = $data;
    }

    public function render(): JsonResponse
    {
        return response()->json([
            'success' => false,
            'message' => $this->getMessage(),
            'error_code' => $this->errorCode,
            'data' => $this->data,
        ], $this->statusCode);
    }

    public function getErrorCode(): string
    {
        return $this->errorCode;
    }
}
