<?php
namespace local_academic_ticket_system\external;

use core_external\external_api;
use core_external\external_function_parameters;
use core_external\external_single_structure;
use core_external\external_value;
use core_external\external_description;
use context_system;

defined('MOODLE_INTERNAL') || die();

class get_ticket_counts extends external_api {

    public static function execute_parameters(): external_function_parameters {
        return new external_function_parameters([]);
    }

    public static function execute() {
        global $DB;

        self::validate_context(context_system::instance());

        $total = $DB->count_records('local_acadtkt_tickets');
        $open = $DB->count_records('local_acadtkt_tickets', ['status' => 'open']);
        $closed = $DB->count_records('local_acadtkt_tickets', ['status' => 'closed']);
        $urgent = $DB->count_records('local_acadtkt_tickets', ['priority' => 'high', 'status' => 'open']);

        return [
            'total' => (int)$total,
            'open' => (int)$open,
            'closed' => (int)$closed,
            'urgent' => (int)$urgent
        ];
    }

    public static function execute_returns(): external_description {
        return new external_single_structure([
            'total' => new external_value(PARAM_INT, 'Total tickets'),
            'open' => new external_value(PARAM_INT, 'Open tickets'),
            'closed' => new external_value(PARAM_INT, 'Closed tickets'),
            'urgent' => new external_value(PARAM_INT, 'High priority open tickets'),
        ]);
    }
}