<?php
declare(strict_types=1);


namespace ProRank\SEO\Core\RestApi\Controllers;

use ProRank\SEO\Core\RestApi\BaseController;

if ( ! defined( 'ABSPATH' ) ) {
    exit;
}




class NotificationsController extends BaseController {
    
    public function register_routes() {
        register_rest_route($this->namespace, '/notifications', [
            [
                'methods' => \WP_REST_Server::READABLE,
                'callback' => [$this, 'get_notifications'],
                'permission_callback' => [$this, 'check_admin_permission'],
            ],
            [
                'methods' => \WP_REST_Server::EDITABLE,
                'callback' => [$this, 'update_notification'],
                'permission_callback' => [$this, 'check_admin_permission'],
            ],
            [
                'methods' => \WP_REST_Server::DELETABLE,
                'callback' => [$this, 'dismiss_notification'],
                'permission_callback' => [$this, 'check_admin_permission'],
            ],
        ]);
    }
    
    public function get_notifications($request) {
        try {
            $notifications_manager = $this->container->get('notifications');
            $notifications = $notifications_manager->get_all_notifications();
            
            return $this->success($notifications);
        } catch (\Exception $e) {
            return $this->error($e->getMessage(), 'notifications_error', 500);
        }
    }
    
    public function update_notification($request) {
        try {
            $notification_id = $request->get_param('id');
            $read = $request->get_param('read');
            
            if (empty($notification_id)) {
                return $this->error('Notification ID is required', 'missing_notification_id');
            }
            
            $notifications_manager = $this->container->get('notifications');
            $result = $notifications_manager->mark_as_read($notification_id, $read);
            
            if (is_wp_error($result)) {
                return $this->error($result->get_error_message(), $result->get_error_code());
            }
            
            return $this->success([
                'id' => $notification_id,
                'read' => $read,
            ]);
        } catch (\Exception $e) {
            return $this->error($e->getMessage(), 'notifications_error', 500);
        }
    }
    
    public function dismiss_notification($request) {
        try {
            $notification_id = $request->get_param('id');
            
            if (empty($notification_id)) {
                return $this->error('Notification ID is required', 'missing_notification_id');
            }
            
            $notifications_manager = $this->container->get('notifications');
            $result = $notifications_manager->dismiss($notification_id);
            
            if (is_wp_error($result)) {
                return $this->error($result->get_error_message(), $result->get_error_code());
            }
            
            return $this->success([
                'id' => $notification_id,
                'dismissed' => true,
            ]);
        } catch (\Exception $e) {
            return $this->error($e->getMessage(), 'notifications_error', 500);
        }
    }
}
