<?php
session_start();

include "include.php";
include "session.php";

$result = array();
$result['error'] = 1;
$result['message'] = "Something went wrong. Please try again.";

if ($updte == 1 || isset($_POST['user_id'])) {

    $user_id = intval($_POST['user_id']);
    $auto_refill_id = intval($_POST['auto_refill_id'] ?? 0);
    $edit_mode = intval($_POST['edit_mode'] ?? 0);
    $is_enabled = isset($_POST['is_enabled']) ? 1 : 0;
    $min_balance = floatval($_POST['min_balance']);
    $refill_amount = floatval($_POST['refill_amount']);
    $max_refill_per_day = intval($_POST['max_refill_per_day']);
    $source_user_id = intval($_POST['source_user_id']);

    // Validation
    if ($user_id <= 0) {
        $result['message'] = "Please select a valid user.";
        echo json_encode($result);
        exit;
    }

    if ($min_balance < 0) {
        $result['message'] = "Minimum balance cannot be negative.";
        echo json_encode($result);
        exit;
    }

    if ($refill_amount < 100) {
        $result['message'] = "Refill amount must be at least ₹100.";
        echo json_encode($result);
        exit;
    }

    if ($source_user_id <= 0) {
        $result['message'] = "Please select a valid fund source.";
        echo json_encode($result);
        exit;
    }

    // Get user details
    $user = $factory->get_object($user_id, "users", "user_id");
    if ($user->user_id <= 0) {
        $result['message'] = "User not found.";
        echo json_encode($result);
        exit;
    }

    // Get source user details
    $source_user = $factory->get_object($source_user_id, "users", "user_id");
    if ($source_user->user_id <= 0) {
        $result['message'] = "Source admin not found.";
        echo json_encode($result);
        exit;
    }

    // Check if already exists for this user (when adding new)
    if ($edit_mode == 0) {
        $sql_check = "SELECT auto_refill_id FROM auto_refill WHERE user_id = $user_id AND is_active = 1";
        $res_check = getXbyY($sql_check);
        if (count($res_check) > 0) {
            $result['message'] = "Auto refill is already configured for this user. Please edit existing settings.";
            echo json_encode($result);
            exit;
        }
    }

    $ar = new auto_refill();
    $ar->user_id = $user_id;
    $ar->user_name = $user->user_name . " - " . $user->name;
    $ar->is_enabled = $is_enabled;
    $ar->min_balance = $min_balance;
    $ar->refill_amount = $refill_amount;
    $ar->max_refill_per_day = $max_refill_per_day;
    $ar->source_user_id = $source_user_id;
    $ar->source_user_name = $source_user->user_name . " - " . $source_user->name;
    $ar->is_active = 1;

    if ($edit_mode == 1 && $auto_refill_id > 0) {
        // Update existing
        $ar->auto_refill_id = $auto_refill_id;
        $ar->updated_at = todaysDate();

        $ar->auto_refill_id = $updater->update_object($ar, "auto_refill");

        $result['error'] = 0;
        $result['message'] = "Auto refill settings updated successfully!";
    } else {
        // Insert new
        $ar->created_at = todaysDate();
        $ar->updated_at = todaysDate();
        $ar->refill_count_today = 0;
        $ar->total_refilled = 0;

        $ar->auto_refill_id = $insertor->insert_object($ar, "auto_refill");

        if ($ar->auto_refill_id > 0) {
            $result['error'] = 0;
            $result['message'] = "Auto refill configured successfully for " . $user->name . "!";
        }
    }
}

echo json_encode($result);
?>
