<?php

namespace App\Controllers;

use App\Core\Controller;
use App\Core\Request;
use App\Core\Response;
use App\Core\Session;
use App\Core\Database;
use App\Models\User;
use App\Models\Page;

class SettingsController extends Controller
{
    public function index(Request $request): Response
    {
        $userModel = new User();
        $user = $userModel->find(Session::get('user_id'));
        $preferences = json_decode($user['preferences'] ?? '{}', true) ?: [];
        return $this->render('settings.index', compact('user', 'preferences'));
    }

    public function getPreferences(Request $request): Response
    {
        $userModel = new User();
        $user = $userModel->find(Session::get('user_id'));
        $prefs = json_decode($user['preferences'] ?? '{}', true) ?: [];
        return Response::json($prefs);
    }

    public function saveTheme(Request $request): Response
    {
        $body = json_decode(file_get_contents('php://input'), true) ?? [];
        $theme = $body['theme'] ?? $request->post('theme', 'light');
        $userId = Session::get('user_id');

        $userModel = new User();
        $user = $userModel->find($userId);
        $prefs = json_decode($user['preferences'] ?? '{}', true) ?: [];
        $prefs['theme'] = $theme;
        $userModel->update($userId, ['preferences' => json_encode($prefs)]);

        return Response::json(['ok' => true]);
    }

    public function saveNotificationPreferences(Request $request): Response
    {
        $body = json_decode(file_get_contents('php://input'), true) ?? [];
        $userId = Session::get('user_id');
        $userModel = new User();
        $user = $userModel->find($userId);
        $prefs = json_decode($user['preferences'] ?? '{}', true) ?: [];
        $prefs['notifications'] = $body;
        $userModel->update($userId, ['preferences' => json_encode($prefs)]);
        return Response::json(['ok' => true]);
    }

    public function saveKeyboardShortcuts(Request $request): Response
    {
        $body = json_decode(file_get_contents('php://input'), true) ?? [];
        $userId = Session::get('user_id');
        $userModel = new User();
        $user = $userModel->find($userId);
        $prefs = json_decode($user['preferences'] ?? '{}', true) ?: [];
        $prefs['shortcuts'] = $body;
        $userModel->update($userId, ['preferences' => json_encode($prefs)]);
        return Response::json(['ok' => true]);
    }

    public function saveAccessibility(Request $request): Response
    {
        $body = json_decode(file_get_contents('php://input'), true) ?? [];
        $userId = Session::get('user_id');
        $userModel = new User();
        $user = $userModel->find($userId);
        $prefs = json_decode($user['preferences'] ?? '{}', true) ?: [];
        $prefs['accessibility'] = $body;
        $userModel->update($userId, ['preferences' => json_encode($prefs)]);
        return Response::json(['ok' => true]);
    }

    public function privacy(Request $request): Response
    {
        $userModel = new User();
        $user = $userModel->find(Session::get('user_id'));
        $prefs = json_decode($user['preferences'] ?? '{}', true) ?: [];
        return $this->render('settings.privacy', compact('user', 'prefs'));
    }

    public function savePrivacy(Request $request): Response
    {
        $body = json_decode(file_get_contents('php://input'), true) ?? [];
        $userId = Session::get('user_id');
        $userModel = new User();
        $user = $userModel->find($userId);
        $prefs = json_decode($user['preferences'] ?? '{}', true) ?: [];
        $prefs['privacy'] = $body;
        $userModel->update($userId, ['preferences' => json_encode($prefs)]);
        return Response::json(['ok' => true]);
    }

    public function showPage(Request $request, string $slug): Response
    {
        $pageModel = new Page();
        $page = $pageModel->findBySlug($slug);
        if (!$page) return new Response('Page not found', 404);
        $title = htmlspecialchars($page['title']);
        return $this->render('page', compact('page', 'title'));
    }
}
