<?php
/**
 * announcement_actions.php — the Announcements POST actions, extracted so they
 * can be driven both by the page (non-JS full-reload fallback) and by
 * api/announcement-ajax.php (async JSON via whp_ajax_endpoint). Logic is
 * unchanged from the original page handler; it reads $_POST + AUTH_USER and
 * returns ['message' => string, 'type' => 'success'|'danger'].
 *
 * Pattern mirror of libs/database_actions.php.
 */
function whp_announcement_handle_action(announcement_manager $AnnouncementMgr): array {
    $message = '';
    $message_type = '';

    if (($_SERVER['REQUEST_METHOD'] ?? '') !== 'POST' || !isset($_POST['action'])) {
        return ['message' => $message, 'type' => $message_type];
    }

    switch ($_POST['action']) {
        case 'create_announcement':
            if (!empty($_POST['title']) && !empty($_POST['content'])) {
                $title = trim($_POST['title']);
                $content = trim($_POST['content']);
                $priority = intval($_POST['priority'] ?? 0);
                $active = isset($_POST['active']) ? true : false;
                $pinned = isset($_POST['pinned']) ? true : false;

                $result = $AnnouncementMgr->create_announcement($title, $content, AUTH_USER, $priority, $active, $pinned);
                if ($result['status'] === 'success') {
                    $message = 'Announcement created successfully';
                    $message_type = 'success';
                } else {
                    $message = 'Error creating announcement: ' . $result['message'];
                    $message_type = 'danger';
                }
            } else {
                $message = 'Title and content are required';
                $message_type = 'danger';
            }
            break;

        case 'update_announcement':
            if (!empty($_POST['id']) && !empty($_POST['title']) && !empty($_POST['content'])) {
                $id = intval($_POST['id']);
                $title = trim($_POST['title']);
                $content = trim($_POST['content']);
                $priority = intval($_POST['priority'] ?? 0);
                $active = isset($_POST['active']) ? true : false;
                $pinned = isset($_POST['pinned']) ? true : false;

                $result = $AnnouncementMgr->update_announcement($id, $title, $content, $priority, $active, $pinned);
                if ($result['status'] === 'success') {
                    $message = 'Announcement updated successfully';
                    $message_type = 'success';
                } else {
                    $message = 'Error updating announcement: ' . $result['message'];
                    $message_type = 'danger';
                }
            } else {
                $message = 'ID, title and content are required';
                $message_type = 'danger';
            }
            break;

        case 'delete_announcement':
            if (!empty($_POST['id'])) {
                $id = intval($_POST['id']);
                $result = $AnnouncementMgr->delete_announcement($id);
                if ($result['status'] === 'success') {
                    $message = 'Announcement deleted successfully';
                    $message_type = 'success';
                } else {
                    $message = 'Error deleting announcement: ' . $result['message'];
                    $message_type = 'danger';
                }
            } else {
                $message = 'Announcement ID is required';
                $message_type = 'danger';
            }
            break;

        case 'toggle_status':
            if (!empty($_POST['id'])) {
                $id = intval($_POST['id']);
                $result = $AnnouncementMgr->toggle_announcement_status($id);
                if ($result['status'] === 'success') {
                    $message = 'Announcement status updated successfully';
                    $message_type = 'success';
                } else {
                    $message = 'Error updating announcement status: ' . $result['message'];
                    $message_type = 'danger';
                }
            } else {
                $message = 'Announcement ID is required';
                $message_type = 'danger';
            }
            break;

        case 'toggle_pinned':
            if (!empty($_POST['id'])) {
                $id = intval($_POST['id']);
                $result = $AnnouncementMgr->toggle_announcement_pinned($id);
                if ($result['status'] === 'success') {
                    $message = 'Announcement pin status updated successfully';
                    $message_type = 'success';
                } else {
                    $message = 'Error updating announcement pin status: ' . $result['message'];
                    $message_type = 'danger';
                }
            } else {
                $message = 'Announcement ID is required';
                $message_type = 'danger';
            }
            break;
    }

    return ['message' => $message, 'type' => $message_type];
}
