<?php
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

require_once __DIR__ . '/class/db-functions.php';
$dbHandler = DBHandlerSelect::getInstance();
$pdo = $dbHandler->getConnection();

// Ricezione dati dal form
$user_id = isset($_POST['user_id']) ? (int)$_POST['user_id'] : 0;
$first_name = isset($_POST['first_name']) ? htmlspecialchars($_POST['first_name']) : null;
$last_name = isset($_POST['last_name']) ? htmlspecialchars($_POST['last_name']) : null;
$company_name = isset($_POST['company_name']) ? htmlspecialchars($_POST['company_name']) : null;
$tax_code = isset($_POST['tax_code']) ? htmlspecialchars($_POST['tax_code']) : null;
$email = isset($_POST['email']) ? htmlspecialchars($_POST['email']) : null;
$phone = isset($_POST['phone']) ? htmlspecialchars($_POST['phone']) : null;
$address = isset($_POST['address']) ? htmlspecialchars($_POST['address']) : null;
$postal_code = isset($_POST['postal_code']) ? htmlspecialchars($_POST['postal_code']) : null;
$city = isset($_POST['city']) ? htmlspecialchars($_POST['city']) : null;
$province = isset($_POST['province']) ? htmlspecialchars($_POST['province']) : null;
$country = isset($_POST['country']) ? (int)$_POST['country'] : null;
$owner_type = isset($_POST['owner_type']) ? htmlspecialchars($_POST['owner_type']) : null;
$role = isset($_POST['role']) ? htmlspecialchars($_POST['role']) : null;
$notes = isset($_POST['notes']) ? htmlspecialchars($_POST['notes']) : null;

// Validazione dei campi obbligatori
if (!$user_id || !$tax_code || !$email || !$owner_type) {
    die(json_encode(["success" => false, "message" => "Campi obbligatori mancanti."]));
}

// Se è una persona fisica, company_name deve essere NULL
if ($owner_type === "individual") {
    $company_name = null;
}

// Controlla se il proprietario esiste già (evita duplicati)
$stmt = $pdo->prepare("SELECT owner_id FROM property_owners WHERE tax_code = ?");
$stmt->execute([$tax_code]);
if ($stmt->fetch()) {
    die(json_encode(["success" => false, "message" => "Esiste già un proprietario con questo Codice Fiscale/Partita IVA."]));
}

// Inserisci nuovo proprietario
$stmt = $pdo->prepare("
    INSERT INTO property_owners (user_id, first_name, last_name, company_name, tax_code, email, phone, address, postal_code, city, province, country, owner_type, role, notes) 
    VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
");
try {
    $stmt->execute([
        $user_id,
        $first_name,
        $last_name,
        $company_name,
        $tax_code,
        $email,
        $phone,
        $address,
        $postal_code,
        $city,
        $province,
        $country,
        $owner_type,
        $role,
        $notes
    ]);
    $owner_id = $pdo->lastInsertId();
    $owner_name = $owner_type === "individual" ? "$first_name $last_name" : $company_name;
    echo json_encode([
        "success" => true,
        "message" => "Proprietario aggiunto con successo!",
        "owner_id" => $owner_id,
        "owner_name" => $owner_name,
        "tax_code" => $tax_code
    ]);
} catch (PDOException $e) {
    die(json_encode(["success" => false, "message" => "Errore nell'inserimento: " . $e->getMessage()]));
}
