<?php
session_start();
if(!isset($_SESSION['user_id'])){
    header("Location: /beauty_pos/login.php");
    exit();
}
include_once "../config/database.php";

header('Content-Type: application/json');
header('Content-Disposition: attachment; filename="beauty_pos_backup.json"');

$tables = [
    'users','categories','brands','products','inventory','stock_history',
    'customers','suppliers','purchase_orders','purchase_items',
    'sales','sale_items','expenses','employees','attendance','appointments'
];

$data = [];

foreach($tables as $table){
    $query = mysqli_query($conn, "SELECT * FROM $table");
    $rows = [];

    while($row = mysqli_fetch_assoc($query)){
        $rows[] = $row;
    }

    $data[$table] = $rows;
}

echo json_encode($data, JSON_PRETTY_PRINT);
exit();