<?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_multiple_structure;
use core_external\external_value;
use core_external\external_description;

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

class get_ticket_details extends external_api {

    public static function execute_parameters(): external_function_parameters {
        return new external_function_parameters([
            'ticketid' => new external_value(PARAM_INT, 'Ticket ID')
        ]);
    }

    public static function execute($ticketid) {
        global $DB;
        $params = self::validate_parameters(self::execute_parameters(), ['ticketid' => $ticketid]);
        self::validate_context(\context_system::instance());

        $ticket = $DB->get_record('local_acadtkt_tickets', ['id' => $params['ticketid']], '*', MUST_EXIST);
        $replies = $DB->get_records('local_acadtkt_replies', ['ticket_id' => $params['ticketid']], 'created_at ASC');
        $logs = $DB->get_records('local_acadtkt_logs', ['ticketid' => $params['ticketid']], 'timemodified DESC');
        $feedback = $DB->get_record('local_acadtkt_feedback', ['ticketid' => $params['ticketid']]);

        $formatted_replies = [];
        foreach ($replies as $reply) {
            $formatted_replies[] = [
                'userid' => $reply->userid,
                'message' => $reply->message,
                'created_at' => $reply->created_at
            ];
        }

        $formatted_logs = [];
        foreach ($logs as $log) {
            $formatted_logs[] = [
                'action' => $log->actionname,
                'user' => $log->userid,
                'time' => $log->timemodified
            ];
        }

        return [
            'id' => $ticket->id,
            'status' => $ticket->status,
            'replies' => $formatted_replies,
            'logs' => $formatted_logs,
            'feedback_rating' => $feedback ? $feedback->rating : 0
        ];
    }

    public static function execute_returns(): external_description {
        return new external_single_structure([
            'id' => new external_value(PARAM_INT, 'ID'),
            'status' => new external_value(PARAM_TEXT, 'Status'),
            'replies' => new external_multiple_structure(
                new external_single_structure([
                    'userid' => new external_value(PARAM_INT, 'User ID'),
                    'message' => new external_value(PARAM_RAW, 'Message content'),
                    'created_at' => new external_value(PARAM_INT, 'Timestamp')
                ])
            ),
            'logs' => new external_multiple_structure(
                new external_single_structure([
                    'action' => new external_value(PARAM_TEXT, 'Action name'),
                    'user' => new external_value(PARAM_INT, 'User ID'),
                    'time' => new external_value(PARAM_INT, 'Timestamp')
                ])
            ),
            'feedback_rating' => new external_value(PARAM_INT, 'Feedback rating')
        ]);
    }
}