<?php
// sales/config.php - Global configuration for team-mania.de payment backend

// Enable sessions
if (session_status() === PHP_SESSION_NONE) {
    session_start();
}

// Stripe Credentials (Live Keys provided by Morina Solutions UG)
define('STRIPE_SECRET_KEY', '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');
define('STRIPE_PUBLISHABLE_KEY', 'pk_live_51TxqC43x5ykpCKHinY6g7u4FqDoT0JprjxNom0OSklLdN3cbqoOg1eBvPRKVWjt3fWue4fVeQS1fBK8Q7Qd97Icy000YV4Wudd');

// Webhook Secret (Retrieve this from Stripe Dashboard -> Developers -> Webhooks -> whsec_...)
define('STRIPE_WEBHOOK_SECRET', 'whsec_CHANGE_ME_AFTER_CREATING_WEBHOOK');

// Database Connection for License Keys & Orders
$sales_db_file = __DIR__ . '/data/sales.sqlite';

try {
    if (!file_exists(__DIR__ . '/data')) {
        mkdir(__DIR__ . '/data', 0777, true);
    }
    
    $sales_pdo = new PDO("sqlite:" . $sales_db_file);
    $sales_pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    
    // Create license schema if not exists
    $sales_pdo->exec("CREATE TABLE IF NOT EXISTS licenses (
        id INTEGER PRIMARY KEY AUTOINCREMENT,
        license_key TEXT UNIQUE NOT NULL,
        club_name TEXT NOT NULL,
        email TEXT NOT NULL,
        max_members INTEGER NOT NULL,
        modules TEXT NOT NULL, -- JSON array (e.g. ['forum', 'playright'])
        status TEXT DEFAULT 'active', -- active, suspended
        expires_at DATETIME,
        created_at DATETIME DEFAULT CURRENT_TIMESTAMP
    )");

    $sales_pdo->exec("CREATE TABLE IF NOT EXISTS clothing_orders (
        id INTEGER PRIMARY KEY AUTOINCREMENT,
        stripe_session_id TEXT UNIQUE NOT NULL,
        email TEXT NOT NULL,
        item_details TEXT NOT NULL, -- JSON
        total_amount REAL NOT NULL,
        status TEXT DEFAULT 'pending', -- pending, sent
        created_at DATETIME DEFAULT CURRENT_TIMESTAMP
    )");
    
} catch (PDOException $e) {
    die("Zahlungsdatenbank konnte nicht initialisiert werden: " . $e->getMessage());
}
