<?php

namespace App\Core;

class Response
{
    public function __construct(
        private array $body,
        private int $status = 200,
        private array $headers = []
    ) {
    }

    public static function json(array $body, int $status = 200, array $headers = []): self
    {
        return new self($body, $status, $headers);
    }

    public function send(): void
    {
        http_response_code($this->status);
        header('Content-Type: application/json');

        foreach ($this->headers as $name => $value) {
            header($name . ': ' . $value);
        }

        echo json_encode($this->body, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES);
    }
}
