<?php
// Mobile API endpoint for certificate verification
header('Content-Type: application/json');
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: GET, POST, OPTIONS');
header('Access-Control-Allow-Headers: Content-Type');

// Handle preflight requests
if ($_SERVER['REQUEST_METHOD'] === 'OPTIONS') {
    http_response_code(200);
    exit();
}

include('includes/config.php');

function sendResponse($success, $data = null, $message = '', $code = 200) {
    http_response_code($code);
    echo json_encode([
        'success' => $success,
        'data' => $data,
        'message' => $message,
        'timestamp' => date('c')
    ]);
    exit();
}

// Get registration number from URL parameter or POST data
$registration_no = '';
if (isset($_GET['reg']) && !empty($_GET['reg'])) {
    $registration_no = mysqli_real_escape_string($conn, trim($_GET['reg']));
} elseif (isset($_POST['registration_no']) && !empty($_POST['registration_no'])) {
    $registration_no = mysqli_real_escape_string($conn, trim($_POST['registration_no']));
}

if (empty($registration_no)) {
    sendResponse(false, null, 'Registration number is required', 400);
}

// Search in both tables
$certificate_data = null;
$source_table = '';

// Try delhi_birth table first (CRS system)
$sql = "SELECT * FROM delhi_birth WHERE registration_no = '$registration_no'";
$result = mysqli_query($conn, $sql);

if ($result && mysqli_num_rows($result) > 0) {
    $certificate_data = mysqli_fetch_assoc($result);
    $source_table = 'delhi_birth';
} else {
    // Try birth_certificate table (HHHHH system)
    $sql = "SELECT * FROM birth_certificate WHERE registration_no = '$registration_no'";
    $result = mysqli_query($conn, $sql);
    
    if ($result && mysqli_num_rows($result) > 0) {
        $certificate_data = mysqli_fetch_assoc($result);
        $source_table = 'birth_certificate';
    }
}

if (!$certificate_data) {
    sendResponse(false, null, 'Certificate not found with this registration number', 404);
}

// Format the response data
$response_data = [
    'registration_no' => $certificate_data['registration_no'],
    'name' => $certificate_data['name'],
    'gender' => $certificate_data['gender'],
    'date_of_birth' => date('d F Y', strtotime($certificate_data['date_of_birth'])),
    'date_of_birth_raw' => $certificate_data['date_of_birth'],
    'father_name' => $certificate_data['father_name'],
    'mother_name' => $certificate_data['mother_name'],
    'place_of_birth' => $certificate_data['place_of_birth'],
    'source_system' => strtoupper(str_replace('_', ' ', $source_table)),
    'verification_timestamp' => date('c'),
    'authority' => 'Municipal Corporation of Delhi'
];

// Add optional fields if they exist
if (isset($certificate_data['date_of_registration'])) {
    $response_data['date_of_registration'] = date('d F Y', strtotime($certificate_data['date_of_registration']));
    $response_data['date_of_registration_raw'] = $certificate_data['date_of_registration'];
}

if (isset($certificate_data['date_of_issue'])) {
    $response_data['date_of_issue'] = date('d F Y', strtotime($certificate_data['date_of_issue']));
    $response_data['date_of_issue_raw'] = $certificate_data['date_of_issue'];
}

if (isset($certificate_data['present_address'])) {
    $response_data['present_address'] = $certificate_data['present_address'];
}

if (isset($certificate_data['permanent_address'])) {
    $response_data['permanent_address'] = $certificate_data['permanent_address'];
}

// Send successful response
sendResponse(true, $response_data, 'Certificate verified successfully');
?>