<?php

namespace FleekDash\Controllers\Notifications;

use FleekDash\Controllers\BaseController;
use FleekDash\Models\Notifications;
use WP_REST_Request;
use WP_REST_Response;

defined('ABSPATH') || exit;

class Actions extends BaseController
{
    public function get(WP_REST_Request $request)
    {
        // Get visible notifications with their notes
        $notifications = Notifications::getVisible();

        // Load notes for each notification
        foreach ($notifications as &$notification) {
            $notification['notes'] = Notifications::getNotes($notification['id']);
        }

        return new WP_REST_Response($notifications);
    }

    public function mark_as_read(WP_REST_Request $request)
    {
        $id = $request['id'];
        $result = Notifications::markAsRead($id);

        if ($result !== false) {
            $notification = Notifications::find($id);
            return new WP_REST_Response([
                'status' => 'success',
                'notification' => $notification
            ]);
        }

        return new WP_REST_Response([
            'status' => 'error',
            'message' => 'Notification not found or already read.'
        ], 404);
    }

    public function resolve(WP_REST_Request $request)
    {
        $id = $request['id'];
        $result = Notifications::resolve($id);

        if ($result !== false) {
            $notification = Notifications::find($id);
            return new WP_REST_Response([
                'status' => 'success',
                'notification' => $notification
            ]);
        }

        return new WP_REST_Response([
            'status' => 'error',
            'message' => 'Notification not found.'
        ], 404);
    }

    public function snooze(WP_REST_Request $request)
    {
        $id = $request['id'];
        $until = $request->get_param('until');

        try {
            $snooze_date = new \DateTime($until);
            $until_formatted = $snooze_date->format('Y-m-d H:i:s');
        } catch (\Exception $e) {
            return new WP_REST_Response([
                'status' => 'error',
                'message' => 'Invalid date format for snoozing.'
            ], 400);
        }

        $result = Notifications::snooze($id, $until_formatted);

        if ($result !== false) {
            $notification = Notifications::find($id);
            return new WP_REST_Response([
                'status' => 'success',
                'notification' => $notification
            ]);
        }

        return new WP_REST_Response([
            'status' => 'error',
            'message' => 'Notification not found.'
        ], 404);
    }

    public function add_note(WP_REST_Request $request)
    {
        $id = $request['id'];
        $note_text = $request->get_param('note');

        $notification = Notifications::find($id);

        if ($notification) {
            $note_id = Notifications::addNote($id, $note_text);

            if ($note_id) {
                return new WP_REST_Response([
                    'status' => 'success',
                    'note' => \FleekDash\Models\NotificationNote::find($note_id)
                ]);
            }
        }

        return new WP_REST_Response([
            'status' => 'error',
            'message' => 'Notification not found.'
        ], 404);
    }
}
