<?php
require_once __DIR__ . '/includes/db.php';
require_once __DIR__ . '/includes/utils.php';
require_once __DIR__ . '/config/config.php';

function exportData($type) {
    ensureExportDirectory();
    $data = readJsonFile($type . '.json');
    $filename = generateExportFilename($type);
    $exportPath = EXPORT_DESTINATION . '/' . $filename;
    
    return file_put_contents($exportPath, json_encode($data, JSON_PRETTY_PRINT));
}

// Handle export request
if (isset($_GET['type'])) {
    $type = sanitizeInput($_GET['type']);
    if (in_array($type, [
        'chores', 'groceries', 'meals', 'meal_plans', 'expenses',
        'stores', 'grocery_lists', 'grocery_catalog', 'bank_transactions',
    ], true)) {
        if (exportData($type)) {
            echo json_encode(['success' => true, 'message' => 'Export successful']);
        } else {
            echo json_encode(['success' => false, 'message' => 'Export failed']);
        }
    } else {
        echo json_encode(['success' => false, 'message' => 'Invalid export type']);
    }
} else {
    echo json_encode(['success' => false, 'message' => 'No export type specified']);
} 