<?php
// Start output buffering to prevent headers issue
ob_start();

// Load existing .env content if it exists
$envPath = __DIR__ . '/../../../.env';
$envContent = file_exists($envPath) ? file_get_contents($envPath) : '';

// Define the environment variables and their default values
$envVariables = [
    // General
    'APP_INSTALLED' => '0',
    'APP_ENV' => 'local',
    'APP_MODE' => 'production',
    'APP_DEBUG' => 'false',
    'TELESCOPE_ENABLED' => 'false',
    'LOG_LEVEL' => 'error',
    'LOG_CHANNEL' => 'stack',

    // Information
    'APP_NAME' => $_POST['APP_NAME'] ?? '',
    'APP_NICKNAME' => $_POST['APP_NICKNAME'] ?? '',
    'APP_KEY' => $_POST['APP_KEY'] ?? '',
    'APP_URL' => $_POST['APP_URL'] ?? '',
    
    // DB
    'DB_CONNECTION' => $_POST['DB_CONNECTION'] ?? '',
    'DB_HOST' => $_POST['DB_HOST'] ?? '',
    'DB_PORT' => $_POST['DB_PORT'] ?? '',
    'DB_DATABASE' => '"' . $_POST['DB_DATABASE'] . '"' ?? 'lems',
    'DB_USERNAME' => '"' . $_POST['DB_USERNAME'] . '"' ?? 'root',
    'DB_PASSWORD' => '"' . $_POST['DB_PASSWORD'] . '"' ?? '',
    
    // Google
    'GOOGLE_CLIENT_ID' => $_POST['GOOGLE_CLIENT_ID'] ?? '',
    'GOOGLE_CLIENT_SECRET' => $_POST['GOOGLE_CLIENT_SECRET'] ?? '',
    'GOOGLE_CALL_URL' => '${APP_URL}/auth/google/callback',
    
    // Facebook
    'FACEBOOK_SKD_ON' => $_POST['FACEBOOK_SKD_ON'] ?? '0',
    'FACEBOOK_CLIENT_ID' => $_POST['FACEBOOK_CLIENT_ID'] ?? '',
    'FACEBOOK_CLIENT_SECRET' => $_POST['FACEBOOK_CLIENT_SECRET'] ?? '',
    'FACEBOOK_CALLBACK_URL' => '${APP_URL}/auth/facebook/callback',

    // Firebase API
    'FIREBASE_ON' => '0',
    'FIREBASAE_apiKey' => '',
    'FIREBASAE_authDomain' => '',
    'FIREBASAE_projectId' => '',
    'FIREBASAE_storageBucket' => '',
    'FIREBASAE_messagingSenderId' => '',
    'FIREBASAE_appId' => '',

    // AWS
    'AWS_ACCESS_KEY_ID' => '',
    'AWS_SECRET_ACCESS_KEY' => '',
    'AWS_DEFAULT_REGION' => 'us-east-1',
    'AWS_BUCKET' => '',

    // Pusher
    'PUSHER_APP_ID' => '',
    'PUSHER_APP_KEY' => '',
    'PUSHER_APP_SECRET' => '',
    'PUSHER_APP_CLUSTER' => 'mt1',
    'MIX_PUSHER_APP_KEY' => '${PUSHER_APP_KEY}',
    'MIX_PUSHER_APP_CLUSTER' => '${PUSHER_APP_CLUSTER}',

    // Cache and Session
    'BROADCAST_DRIVER' => 'log',
    'CACHE_DRIVER' => 'file',
    'QUEUE_CONNECTION' => 'sync',
    'SESSION_DRIVER' => 'file',
    'SESSION_LIFETIME' => '120',
    // Memcache
    'MEMCACHED_HOST' => '127.0.0.1',
    // Redis
    'REDIS_HOST' => '127.0.0.1',
    'REDIS_PASSWORD' => 'null',
    'REDIS_PORT' => '6379',
];

// Update or add each environment variable
foreach ($envVariables as $key => $value) {
    if (strpos($envContent, $key) !== false) {
        $envContent = preg_replace('/^' . $key . '=.*/m', $key . '=' . $value, $envContent);
    } else {
        $envContent .= "\n" . $key . '=' . $value;
    }
}




// File creation
if (file_put_contents($envPath, $envContent)) {

    // Fix permissions
    chmod($envPath, 0775);
    chown($envPath, 'www-data');
    chgrp($envPath, 'www-data');


    // DB Import
    header('Location: /install/import_db/' . $_POST['mode']);
    exit();
}

// End output buffering and flush output
ob_end_flush();

?>