<?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("/create_address");
$route->setMethods("POST");
$route->setCallback(function () {
    $response = new ApiResponse();

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

    if (empty($data)) {
        $response->fail("Address data is required");
        return $response->getResponse();
    }

    $id = Address::create($data);

    if ($id === false) {
        $response->fail("Failed to create address. Check required fields: label, ragione_sociale, indirizzo, localita, cap, provincia");
        return $response->getResponse();
    }

    $address = Address::getById($id);
    $response->success([
        "address" => $address
    ], "Address created successfully");

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