<?php
namespace Elementor_Pay_Addons\API;

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

use Elementor_Pay_Addons\Database\Transaction_Query;
use WP_REST_Controller;
use WP_REST_Request;
use WP_REST_Response;
use Elementor_Pay_Addons\Shared\Security_Utils;

class Rest_API_Transaction_Controller extends WP_REST_Controller {
    public function __construct() {
        $this->namespace = trim(EPA_ADDONS_REST_API, '/');
        $this->rest_base = 'transaction';
    }

    public function register_routes() {
        register_rest_route($this->namespace, $this->rest_base . '/update-status', [
            [
                'methods' => 'POST',
                'callback' => [$this, 'update_status'],
                'permission_callback' => [$this, 'permission_check'],
            ],
        ]);
        // Delete transaction endpoint
        register_rest_route($this->namespace,  $this->rest_base . '/delete', [
            [
                'methods' => 'POST',
                'callback' => [$this, 'delete_transaction'],
                'permission_callback' => [$this, 'permission_check'],
            ],
        ]);
        // Get transaction details endpoint
        register_rest_route($this->namespace, $this->rest_base . '/details', [
            [
                'methods' => 'POST',
                'callback' => [$this, 'get_transaction_details'],
                'permission_callback' => [$this, 'permission_check'],
            ],
        ]);
    }

    public function permission_check(WP_REST_Request $request) {
      return Security_Utils::admin_check($request);
    }

    public function update_status(WP_REST_Request $request) {
        $transaction_id = sanitize_text_field($request->get_param('transaction_id'));
        $status = sanitize_text_field($request->get_param('status'));
        $comment = sanitize_text_field($request->get_param('message'));
        if (!$transaction_id || !$status) {
            return new WP_REST_Response(['error' => 'Missing parameters'], 400);
        }
        $txn_query = new Transaction_Query();
        $result = $txn_query->update_by_transaction_id($transaction_id, [
          'payment_status' => $status,
          'comment' => $comment
        ]);
        if ($result === false) {
            return new WP_REST_Response(['error' => 'Update failed'], 500);
        }
        return new WP_REST_Response(['success' => true], 200);
    }

    public function delete_transaction(WP_REST_Request $request) {
        $id = intval($request->get_param('id'));
        if (!$id) {
            return new WP_REST_Response(['error' => 'Missing transaction ID'], 400);
        }
        $txn_query = new Transaction_Query();
        $deleted = $txn_query->delete_by_id($id);
        if ($deleted) {
            return new WP_REST_Response(['success' => true], 200);
        } else {
            return new WP_REST_Response(['error' => 'Delete failed'], 500);
        }
    }

    public function get_transaction_details(WP_REST_Request $request) {
        $id = intval($request->get_param('id'));
        if (!$id) {
            return new WP_REST_Response(['error' => 'Missing transaction ID'], 400);
        }
        $txn_query = new Transaction_Query();
        $txn = $txn_query->get_transaction($id);
        if ($txn) {
            return new WP_REST_Response(['success' => true, 'data' => $txn], 200);
        } else {
            return new WP_REST_Response(['error' => 'Transaction not found'], 404);
        }
    }
}

