<?php

namespace App\Core;

class Controller {
    protected function json($data, $status = 200) {
        http_response_code($status);
        header('Content-Type: application/json');
        echo json_encode($data);
        exit;
    }

    protected function input() {
        return json_decode(file_get_contents('php://input'), true) ?? [];
    }

    protected function validate($data, $rules) {
        $errors = [];
        foreach ($rules as $field => $rule) {
            $required = strpos($rule, 'required') !== false;
            if ($required && empty($data[$field])) {
                $errors[$field] = 'Field is required';
            }
            // Add more validation rules as needed
        }
        
        if (!empty($errors)) {
            $this->json(['errors' => $errors], 422);
        }
    }
}
