<?php
require_once __DIR__ . '/functions.php';

$tenant = enrep_tenant_segment($_POST['tenant'] ?? $_GET['tenant'] ?? null);
if ($tenant !== '' && !defined('CONFIG_LOADED')) {
    require_once '../../' . $tenant . '/config.php';
}

assetmanager_require_user();

$con = $GLOBALS['con'] ?? null;
if (!$con instanceof mysqli) {
    assetmanager_json_response(['success' => false, 'message' => 'Tenant database connection unavailable.']);
}
$con->set_charset('utf8mb4');

$clientnumber = enrep_request_int('clientnumber', 0);
$ruleId = enrep_request_int('rule_id', 0);
$deleteScope = enrep_request_enum('delete_scope', ['entry', 'group'], 'entry');
if ($clientnumber <= 0 || $ruleId <= 0) {
    assetmanager_json_response(['success' => false, 'message' => 'Pricing entry is required.']);
}
assetmanager_json_require_client_access($con, $clientnumber);
assetmanager_json_require_module($con, $tenant, $clientnumber, 'hire', 'Hire');

if (!assetmanager_table_exists($con, 'assetmanager_hire_pricing')) {
    assetmanager_json_response(['success' => false, 'setup_required' => true, 'message' => 'Hire pricing table is not installed yet.']);
}

$stmt = $con->prepare("
    SELECT id, record_type, group_key, group_name, asset_id
    FROM assetmanager_hire_pricing
    WHERE id=? AND tenant_key=? AND clientnumber=? AND status_key='active'
    LIMIT 1
");
if (!$stmt) {
    assetmanager_json_response(['success' => false, 'message' => 'Pricing entry could not be loaded.']);
}
$stmt->bind_param('isi', $ruleId, $tenant, $clientnumber);
$stmt->execute();
$rule = $stmt->get_result()->fetch_assoc();
$stmt->close();
if (!$rule) {
    assetmanager_json_response(['success' => false, 'message' => 'Pricing entry was not found.']);
}

$now = date('Y-m-d H:i:s');
$userId = assetmanager_actor_user_id();
$groupKey = (string)($rule['group_key'] ?? '');
if ($deleteScope === 'group' && $groupKey !== '') {
    $stmt = $con->prepare("
        UPDATE assetmanager_hire_pricing
        SET status_key='deleted', updated_at=?
        WHERE tenant_key=? AND clientnumber=? AND (group_key=? OR id=?)
    ");
    if (!$stmt) {
        assetmanager_json_response(['success' => false, 'message' => 'Pricing group could not be removed.']);
    }
    $stmt->bind_param('ssisi', $now, $tenant, $clientnumber, $groupKey, $ruleId);
    $ok = $stmt->execute();
    $stmt->close();
    if ($ok) {
        assetmanager_audit($con, $tenant, $clientnumber, $userId, 'hire_pricing', $ruleId, 'delete_group', $rule, []);
    }
    assetmanager_json_response(['success' => $ok, 'message' => $ok ? 'Pricing group removed.' : 'Pricing group could not be removed.']);
}

$stmt = $con->prepare("
    UPDATE assetmanager_hire_pricing
    SET status_key='deleted', updated_at=?
    WHERE id=? AND tenant_key=? AND clientnumber=? AND status_key='active'
");
if (!$stmt) {
    assetmanager_json_response(['success' => false, 'message' => 'Pricing entry could not be removed.']);
}
$stmt->bind_param('sisi', $now, $ruleId, $tenant, $clientnumber);
$ok = $stmt->execute();
$stmt->close();
if ($ok) {
    assetmanager_audit($con, $tenant, $clientnumber, $userId, 'hire_pricing', $ruleId, 'delete_entry', $rule, []);
}

assetmanager_json_response(['success' => $ok, 'message' => $ok ? 'Pricing entry removed.' : 'Pricing entry could not be removed.']);
