<?php
// api/auto_close_services.php
// Auto-close all services with past dates

require_once '../config/database.php';
require_once '../includes/functions.php';

// Check if user is logged in and has appropriate role
session_start();
if (!isset($_SESSION['user_id'])) {
    header('Location: ../index.php?page=login');
    exit;
}

// Auto-close past services
$sql = "UPDATE services SET status = 'Closed' WHERE service_date < CURRENT_DATE AND status = 'Open'";
$stmt = $pdo->prepare($sql);
$stmt->execute();
$affected = $stmt->rowCount();

// Redirect back with success message
header("Location: ../index.php?page=services&msg=Auto-closed $affected past service(s)");
exit;
