<?php
require_once __DIR__ . '/../../config/session.php';
require_once __DIR__ . '/../../config/db.php';
require_once __DIR__ . '/../../config/response.php';

if (!is_logged_in()) {
    send_response(401, false, 'লগইন প্রয়োজন', null);
}
if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
    send_response(405, false, 'শুধু POST অনুরোধ গ্রহণযোগ্য', null);
}

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

$id                 = (int)($data['id'] ?? 0);
$name               = trim($data['name'] ?? '');
$phone              = trim($data['phone'] ?? '');
$address            = trim($data['address'] ?? '');
$note               = trim($data['note'] ?? '');
$opening_receivable = (float)($data['opening_receivable'] ?? 0);
$opening_payable    = (float)($data['opening_payable'] ?? 0);
$balance            = (float)($data['balance'] ?? 0);
$gold_balance       = (float)($data['gold_balance'] ?? 0);
$is_customer        = (int)($data['is_customer'] ?? 0);
$is_supplier        = (int)($data['is_supplier'] ?? 0);
$is_pledge          = (int)($data['is_pledge'] ?? 0);
$is_locker          = (int)($data['is_locker'] ?? 0);
$is_karigar         = (int)($data['is_karigar'] ?? 0);

if ($id <= 0) {
    send_response(400, false, 'পার্টি ID দিন', null);
}
if ($name === '') {
    send_response(400, false, 'নাম দিন', null);
}
if ($opening_receivable < 0 || $opening_payable < 0 || $balance < 0 || $gold_balance < 0) {
    send_response(400, false, 'ঋণাত্মক মান দেওয়া যাবে না', null);
}
if (!$is_customer && !$is_supplier && !$is_pledge && !$is_locker && !$is_karigar) {
    send_response(400, false, 'কমপক্ষে একটি পার্টির ধরন নির্বাচন করুন', null);
}

try {
    $check = $pdo->prepare("SELECT id FROM parties WHERE id=? AND deleted_at IS NULL LIMIT 1");
    $check->execute([$id]);
    if (!$check->fetch()) {
        send_response(404, false, 'পার্টি পাওয়া যায়নি', null);
    }

    /* Duplicate phone check — শুধু phone দেওয়া থাকলে */
    if ($phone !== '') {
        $phoneCheck = $pdo->prepare("SELECT id FROM parties WHERE phone=? AND id!=? AND deleted_at IS NULL LIMIT 1");
        $phoneCheck->execute([$phone, $id]);
        if ($phoneCheck->fetch()) {
            send_response(400, false, 'এই ফোন নম্বরে অন্য পার্টি আছে', null);
        }
    }

    $stmt = $pdo->prepare("
        UPDATE parties SET
            name=?, phone=?, address=?, note=?,
            opening_receivable=?, opening_payable=?,
            balance=?, gold_balance=?,
            is_customer=?, is_supplier=?, is_pledge=?,
            is_locker=?, is_karigar=?
        WHERE id=? LIMIT 1
    ");
    $stmt->execute([
        $name, $phone, $address, $note,
        $opening_receivable, $opening_payable,
        $balance, $gold_balance,
        $is_customer, $is_supplier, $is_pledge,
        $is_locker, $is_karigar,
        $id
    ]);

    send_response(200, true, 'পার্টি সফলভাবে আপডেট হয়েছে', [
        'party_id' => $id, 'name' => $name
    ]);

} catch (Exception $e) {
    send_response(500, false, 'সার্ভার সমস্যা', ['error' => $e->getMessage()]);
}
