<?php
// util/update_pricing_desc.php
require_once __DIR__ . '/../config.php';

if (!$conn) {
    die("Database connection failed.\n");
}

echo "Updating pricing descriptions...\n";

// 1. Update Free/Community tier
$free_desc = "Entry-level package. Best for CBOs and small projects.";
$sql_free = "UPDATE subscription_packages SET description = ? WHERE price = 0";
$stmt = $conn->prepare($sql_free);
$stmt->bind_param("s", $free_desc);
$stmt->execute();
echo "Updated Free tier description. Rows: " . $stmt->affected_rows . "\n";
$stmt->close();

// 2. Remove specific phrases and placeholders from others
$sql_others = "SELECT id, description FROM subscription_packages WHERE price > 0";
$res = $conn->query($sql_others);
while ($row = $res->fetch_assoc()) {
    $desc = $row['description'];
    $strip_phrases = [
        '/Ideal for County-level programs with high volume\.?/i',
        '/For National Partners & International NGOs\.?/i',
        '/Best for [^.]+\.?/i'
    ];
    $new_desc = preg_replace($strip_phrases, '', $desc);
    $new_desc = trim($new_desc, " .");

    // Fallback descriptions if they become empty or too short
    if (strlen($new_desc) < 5) {
        if (strpos(strtolower($row['name']), 'basic') !== false)
            $new_desc = "Essential features for growing teams.";
        if (strpos(strtolower($row['name']), 'standard') !== false)
            $new_desc = "Advanced sharing and collaboration tools.";
        if (strpos(strtolower($row['name']), 'premium') !== false)
            $new_desc = "Maximum visibility and priority support.";
        if (strpos(strtolower($row['name']), 'enterprise') !== false)
            $new_desc = "Custom solutions for large-scale operations.";
    }

    $update = $conn->prepare("UPDATE subscription_packages SET description = ? WHERE id = ?");
    $update->bind_param("si", $new_desc, $row['id']);
    $update->execute();
    echo "Updated package ID {$row['id']} description.\n";
}

echo "Done.\n";
?>