<?php
/**
 * @package     MefisAssetBridge
 * @subpackage  MefisAssetBridgeCore_Api_Response
 *
 * @copyright   Copyright (C) 2025 Goran Usljebrka. All rights reserved.
 * @license     Proprietary — No resale or incorporation into other projects without explicit written permission. see LICENSE.txt
 */

namespace MefisAssetBridgeCore\Api\Response;

/**
 * API Response class
 */
class ApiResponse
{
    /** @var bool Success indicator */
    private bool $success;

    /** @var mixed Response data */
    private $data;

    /** @var string Message */
    private string $message;

    /** @var int HTTP status code */
    private int $statusCode;

    /**
     * Create a new ApiResponse instance
     *
     * @param bool $success Success indicator
     * @param mixed $data Response data
     * @param string $message Message
     * @param int $statusCode HTTP status code
     */
    public function __construct(bool $success, $data = null, string $message = '', int $statusCode = 200)
    {
        $this->success = $success;
        $this->data = $data;
        $this->message = $message;
        $this->statusCode = $statusCode;
    }

    /**
     * Check if response is successful
     *
     * @return bool
     */
    public function isSuccess(): bool
    {
        return $this->success;
    }

    /**
     * Get response data
     *
     * @return mixed
     */
    public function getData()
    {
        return $this->data;
    }

    /**
     * Set response data
     *
     * @param mixed $data Response data
     */
    public function setData($data): void
    {
        $this->data = $data;
    }

    /**
     * Get response message
     *
     * @return string
     */
    public function getMessage(): string
    {
        return $this->message;
    }

    /**
     * Get HTTP status code
     *
     * @return int
     */
    public function getStatusCode(): int
    {
        return $this->statusCode;
    }

    /**
     * Convert to array
     *
     * @return array
     */
    public function toArray(): array
    {
        return [
            'success' => $this->success,
            'data' => $this->data,
            'message' => $this->message,
            'status_code' => $this->statusCode
        ];
    }

    /**
     * Create success response
     *
     * @param mixed $data Response data
     * @param string $message Success message
     * @param int $statusCode HTTP status code
     * @return self
     */
    public static function success($data = null, string $message = 'Success', int $statusCode = 200): self
    {
        return new self(true, $data, $message, $statusCode);
    }

    /**
     * Create error response
     *
     * @param string $message Error message
     * @param mixed $data Additional error data
     * @param int $statusCode HTTP status code
     * @return self
     */
    public static function error(string $message = 'Error', $data = null, int $statusCode = 400): self
    {
        return new self(false, $data, $message, $statusCode);
    }
}