<?php
require_once "../config/db.php";

$id = (int) ($_GET['id'] ?? 0);

$data = $conn->query("
SELECT services.*, customers.name, customers.phone
FROM services
JOIN customers ON services.customer_id = customers.id
WHERE services.id = $id
")->fetch_assoc();

if(!$data){
    die("Not found");
}

// Format phone
$phone = preg_replace('/[^0-9]/', '', $data['phone']);

if(strlen($phone) == 10){
    $phone = "91" . $phone;
}

// Message
$message = "Hello {$data['name']},\n";
$message .= "Reminder: Your {$data['type']} service is due.\n";
$message .= "Amount: Rs. {$data['amount']}\n";
$message .= "Renewal Date: {$data['renewal_date']}\n";

// Optional CTA
$message .= "Please renew soon.";

// Encode
$message = urlencode($message);

// Redirect
header("Location: https://wa.me/$phone?text=$message");
exit;
