<?php
if (!defined('ABSPATH')) exit; // Exit if accessed directly
use AC_SYNC\Includes\Classes\Api\Action_Wear_Api_Route;
use AC_SYNC\Includes\Classes\Api\Action_Wear_API_Response as ApiResponse;
use AC_SYNC\Includes\Classes\Address\Action_Wear_Address as Address;

$route = new Action_Wear_Api_Route("/delete_address");
$route->setMethods("POST");
$route->setCallback(function () {
    $response = new ApiResponse();

    $data = json_decode(file_get_contents("php://input"), true);

    if (empty($data['id'])) {
        $response->fail("Address ID is required");
        return $response->getResponse();
    }

    $id = intval($data['id']);

    $existing = Address::getById($id);
    if (!$existing) {
        $response->fail("Address not found");
        return $response->getResponse();
    }

    $result = Address::delete($id);

    if (!$result) {
        $response->fail("Failed to delete address");
        return $response->getResponse();
    }

    $response->success([], "Address deleted successfully");

    return $response->getResponse();
});
$route->registerRoute();
