<?php
// api/settings_action.php
session_start();
require_once '../config/database.php';
require_once '../includes/functions.php';

requireRole('admin');

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    
    // Add Option
    if (isset($_POST['add_option'])) {
        $category = clean_input($_POST['category']);
        $value = clean_input($_POST['value']);
        $display_order = (int)$_POST['display_order'];
        
        $pdo->prepare("INSERT INTO settings_options (category, value, display_order) VALUES (?, ?, ?)")
            ->execute([$category, $value, $display_order]);
        
        header("Location: ../index.php?page=settings&msg=Option added successfully");
        exit;
    }
    
    // Delete Option
    if (isset($_POST['delete_option'])) {
        $id = (int)$_POST['id'];
        $pdo->prepare("DELETE FROM settings_options WHERE id = ?")->execute([$id]);
        
        header("Location: ../index.php?page=settings&msg=Option deleted successfully");
        exit;
    }
    
    // Toggle Active Status
    if (isset($_POST['toggle_active'])) {
        $id = (int)$_POST['id'];
        $pdo->prepare("UPDATE settings_options SET is_active = NOT is_active WHERE id = ?")->execute([$id]);
        
        header("Location: ../index.php?page=settings&msg=Option status updated");
        exit;
    }

    // Update Value by Category
    if (isset($_POST['is_update_value'])) {
        $category = clean_input($_POST['category']);
        $value = clean_input($_POST['value']);
        
        // Check if exists
        $stmt = $pdo->prepare("SELECT id FROM settings_options WHERE category = ?");
        $stmt->execute([$category]);
        if ($stmt->fetch()) {
            $pdo->prepare("UPDATE settings_options SET value = ? WHERE category = ?")->execute([$value, $category]);
        } else {
             $pdo->prepare("INSERT INTO settings_options (category, value, display_order) VALUES (?, ?, 1)")
                ->execute([$category, $value]);
        }
        
        header("Location: ../index.php?page=settings&msg=Settings updated");
        exit;
    }

    // Bulk Update SMTP
    if (isset($_POST['update_smtp'])) {
        $keys = ['smtp_host', 'smtp_user', 'smtp_pass', 'smtp_port', 'smtp_from_email', 'smtp_from_name'];
        foreach ($keys as $key) {
            if (isset($_POST[$key])) {
                $val = clean_input($_POST[$key]);
                // Check exist
                $stmt = $pdo->prepare("SELECT id FROM settings_options WHERE category = ?");
                $stmt->execute([$key]);
                if ($stmt->fetch()) {
                    $pdo->prepare("UPDATE settings_options SET value = ? WHERE category = ?")->execute([$val, $key]);
                } else {
                    $pdo->prepare("INSERT INTO settings_options (category, value, display_order) VALUES (?, ?, 1)")->execute([$val, $key]);
                }
            }
        }
        header("Location: ../index.php?page=settings&msg=SMTP Settings updated");
        exit;
    }
}

// Fallback
header("Location: ../index.php?page=settings");
exit;
