<?php
namespace App\Core;

class Response {
    
    protected $statusCode = 200;
    protected $headers = [];
    
    /**
     * Set HTTP status code
     */
    public function setStatusCode($code) {
        $this->statusCode = $code;
        http_response_code($code);
        return $this;
    }
    
    /**
     * Set header
     */
    public function setHeader($name, $value) {
        $this->headers[$name] = $value;
        return $this;
    }
    
    /**
     * Send headers
     */
    protected function sendHeaders() {
        foreach ($this->headers as $name => $value) {
            header("$name: $value");
        }
    }
    
    /**
     * JSON response
     */
    public function json($data, $statusCode = 200) {
        $this->setStatusCode($statusCode);
        $this->setHeader('Content-Type', 'application/json');
        $this->setHeader('X-Content-Type-Options', 'nosniff');
        $this->sendHeaders();
        
        echo json_encode($data, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES);
        exit;
    }
    
    /**
     * Redirect response
     */
    public function redirect($url, $statusCode = 302) {
        $this->setStatusCode($statusCode);
        $this->setHeader('Location', APP_URL . '/' . ltrim($url, '/'));
        $this->sendHeaders();
        exit;
    }
    
    /**
     * Download response
     */
    public function download($filePath, $filename = null) {
        if (!file_exists($filePath)) {
            throw new \Exception("File not found");
        }
        
        $filename = $filename ?: basename($filePath);
        
        $this->setHeader('Content-Type', mime_content_type($filePath));
        $this->setHeader('Content-Disposition', 'attachment; filename="' . $filename . '"');
        $this->setHeader('Content-Length', filesize($filePath));
        $this->setHeader('Cache-Control', 'no-cache, must-revalidate');
        $this->sendHeaders();
        
        readfile($filePath);
        exit;
    }
    
    /**
     * HTML response
     */
    public function html($content, $statusCode = 200) {
        $this->setStatusCode($statusCode);
        $this->setHeader('Content-Type', 'text/html; charset=UTF-8');
        $this->sendHeaders();
        
        echo $content;
        exit;
    }
    
    /**
     * Plain text response
     */
    public function text($content, $statusCode = 200) {
        $this->setStatusCode($statusCode);
        $this->setHeader('Content-Type', 'text/plain; charset=UTF-8');
        $this->sendHeaders();
        
        echo $content;
        exit;
    }
    
    /**
     * Return error response
     */
    public function error($message, $statusCode = 400) {
        return $this->json([
            'success' => false,
            'error' => $message
        ], $statusCode);
    }
    
    /**
     * Return success response
     */
    public function success($data = null, $message = 'Success') {
        return $this->json([
            'success' => true,
            'message' => $message,
            'data' => $data
        ], 200);
    }
    
    /**
     * Return not found response
     */
    public function notFound($message = 'Resource not found') {
        return $this->error($message, 404);
    }
    
    /**
     * Return unauthorized response
     */
    public function unauthorized($message = 'Unauthorized') {
        return $this->error($message, 401);
    }
    
    /**
     * Return forbidden response
     */
    public function forbidden($message = 'Forbidden') {
        return $this->error($message, 403);
    }
}