<?php
// ─────────────────────────────────────────────
// includes/config.php  —  come2canada.life
// Zentrale Konfiguration für alle Skripte
// ─────────────────────────────────────────────
require_once __DIR__ . '/secrets.php';
// Umgebung erkennen
define('C2C_ENV', getenv('C2C_ENV') ?: 'production');
define('C2C_DEBUG', C2C_ENV === 'development');

// Datenbank
define('DB_HOST', 'localhost');
define('DB_NAME', 'neubad_c2c');
define('DB_USER', 'neubad_c2c_chris');
//define('DB_PASS', getenv('C2C_DB_PASS') ?: '437100!Shpountz');  // via .env oder cPanel ENV

// Anthropic API
define('ANTHROPIC_MODEL',   'claude-sonnet-4-20250514');
define('ANTHROPIC_ENDPOINT','https://api.anthropic.com/v1/messages');

// Authentifizierung
//define('C2C_SECRET', 'xK9mP2qL8nR5wT3vY6uJ4hF7bD1cA0eG');
// URLs
define('C2C_PUBLIC_URL',  'https://come2canada.life/c2c/');
define('C2C_HQ_URL',      'https://christianwehrli.ca/c2c/hq/');
define('C2C_API_BASE',    'https://christianwehrli.ca/c2c/api/');

// E-Mail
define('MAIL_FROM',       'mail@come2canada.life');
define('MAIL_FROM_NAME',  'SwissChris — come2canada.life');
define('MAIL_NOTIFY',     'mail@christianwehrli.ca');

// PayPal
//define('PAYPAL_CLIENT_ID',      getenv('PAYPAL_CLIENT_ID') ?: '');
//define('PAYPAL_CLIENT_SECRET',  getenv('PAYPAL_SECRET') ?: '');
define('PAYPAL_MODE',           'sandbox');  // 'sandbox' oder 'live'
define('PAYPAL_API_BASE', PAYPAL_MODE === 'live'
    ? 'https://api-m.paypal.com'
    : 'https://api-m.sandbox.paypal.com');

// Preise in CHF
define('PRICE_STARTER',   299.00);
define('PRICE_PORTFOLIO', 599.00);
define('PRICE_FAMILY',    799.00);

// CORS für cross-domain Anfragen (come2canada.life → christianwehrli.ca)
define('CORS_ORIGIN', 'https://come2canada.life');

// Fehlerbehandlung
if (C2C_DEBUG) {
    ini_set('display_errors', 1);
    error_reporting(E_ALL);
} else {
    ini_set('display_errors', 0);
    error_reporting(0);
    ini_set('log_errors', 1);
    ini_set('error_log', __DIR__ . '/../logs/error.log');
}

// CORS-Header setzen (für API-Endpunkte)
function c2c_cors_headers(): void {
    header('Access-Control-Allow-Origin: ' . CORS_ORIGIN);
    header('Access-Control-Allow-Methods: GET, POST, OPTIONS');
    header('Access-Control-Allow-Headers: Content-Type, X-C2C-Token');
    header('Access-Control-Allow-Credentials: true');
    if ($_SERVER['REQUEST_METHOD'] === 'OPTIONS') {
        http_response_code(204);
        exit;
    }
}

// JSON-Antwort
function json_response(array $data, int $code = 200): void {
    http_response_code($code);
    header('Content-Type: application/json; charset=utf-8');
    echo json_encode($data, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT);
    exit;
}

// Auth-Check für HQ
function require_hq_auth(): void {
    session_start();
    if (empty($_SESSION['hq_logged_in'])) {
        header('Location: ' . C2C_HQ_URL . 'login.php');
        exit;
    }
}

// Auth-Check für API-Aufrufe
function require_api_token(): void {
    $token = $_SERVER['HTTP_X_C2C_TOKEN'] ?? ($_GET['token'] ?? '');
    if ($token !== C2C_SECRET || empty(C2C_SECRET)) {
        json_response(['error' => 'Unauthorized'], 401);
    }
}
