<?php
if (!defined('ABSPATH')) exit;

class SIMPINBY_Ajax_Actions {

  public function __construct() {
    add_action("wp_ajax_simpinby_toggle_status", [$this, "toggle_status"]);
    add_action("wp_ajax_simpinby_toggle_archive", [$this, "toggle_archive"]);
  }

  public function toggle_status() {
    check_ajax_referer( 'simpinby_ajax_nonce', 'nonce' );

    if ( ! current_user_can("manage_options") ) {
        wp_send_json_error(["message" => "Permission denied"]);
    }


    global $wpdb;
    $invoice_table = $wpdb->prefix . "simpinby_invoices";

    $invoice_id = isset($_POST["invoice_id"])
    ? absint( wp_unslash($_POST["invoice_id"]) )
    : 0;

    if ( ! $invoice_id ) {
        wp_send_json_error(["message" => "Invalid invoice id"]);
    }


    $inv = $wpdb->get_row($wpdb->prepare(
      "SELECT id, status FROM {$invoice_table} WHERE id = %d",
    $invoice_id), ARRAY_A);
    
    if (!$inv) {
      wp_send_json_error(["message" => "Invoice not found"]);
    }

    $new_status = ($inv["status"] === "paid") ? "unpaid" : "paid";

    $updated = $wpdb->update(
        $invoice_table,
        ["status" => $new_status],
        ["id" => $invoice_id],
        ["%s"],
        ["%d"]
    );

    if ( false === $updated ) {
        wp_send_json_error( [ 'message' => 'Database update failed' ] );
    }


    $total_all = (int) $wpdb->get_var(
        $wpdb->prepare(
            "SELECT COUNT(*) FROM {$invoice_table} WHERE is_archived = %d",
            0
        )
    );

    $total_paid = (int) $wpdb->get_var(
        $wpdb->prepare(
            "SELECT COUNT(*) FROM {$invoice_table} WHERE is_archived = %d AND status = %s",
            0,
            'paid'
        )
    );

    $total_unpaid = (int) $wpdb->get_var(
        $wpdb->prepare(
            "SELECT COUNT(*) FROM {$invoice_table} WHERE is_archived = %d AND status = %s",
            0,
            'unpaid'
        )
    );


    wp_send_json_success([
      "invoice_id" => $invoice_id,
      "new_status" => $new_status,
      "counts" => [
            "total_all" => $total_all,
            "total_paid" => $total_paid,
            "total_unpaid" => $total_unpaid,
      ]
    ]);
  }

  public function toggle_archive() {
    check_ajax_referer( 'simpinby_ajax_nonce', 'nonce' );

    if ( ! current_user_can("manage_options") ) {
        wp_send_json_error(["message" => "Permission denied"]);
    }


    global $wpdb;
    $invoice_table = $wpdb->prefix . "simpinby_invoices";

    $invoice_id = isset($_POST["invoice_id"])
    ? absint( wp_unslash($_POST["invoice_id"]) )
    : 0;

    if ( ! $invoice_id ) {
        wp_send_json_error(["message" => "Invalid invoice id"]);
    }


    $inv = $wpdb->get_row($wpdb->prepare("SELECT id, is_archived FROM $invoice_table WHERE id=%d", $invoice_id), ARRAY_A);
    if (!$inv) {
      wp_send_json_error(["message" => "Invoice not found"]);
    }

    $new_archived = ($inv["is_archived"] == 1) ? 0 : 1;

    $updated = $wpdb->update(
        $invoice_table,
        ["is_archived" => $new_archived],
        ["id" => $invoice_id],
        ["%d"],
        ["%d"]
    );

    if ( false === $updated ) {
        wp_send_json_error( [ 'message' => 'Database update failed' ] );
    }


    $total_all = (int) $wpdb->get_var(
        $wpdb->prepare(
            "SELECT COUNT(*) FROM {$invoice_table} WHERE is_archived = %d",
            0
        )
    );

    $total_paid = (int) $wpdb->get_var(
        $wpdb->prepare(
            "SELECT COUNT(*) FROM {$invoice_table} WHERE is_archived = %d AND status = %s",
            0,
            'paid'
        )
    );

    $total_unpaid = (int) $wpdb->get_var(
        $wpdb->prepare(
            "SELECT COUNT(*) FROM {$invoice_table} WHERE is_archived = %d AND status = %s",
            0,
            'unpaid'
        )
    );


    wp_send_json_success([
      "invoice_id" => $invoice_id,
      "is_archived" => $new_archived,
      "counts" => [
            "total_all" => $total_all,
            "total_paid" => $total_paid,
            "total_unpaid" => $total_unpaid,
       ]
    ]);
  }
}
