<?php
	include('API/includes/config.php');
	include('ThumbnailGenerator.php');
	
	// --- CONFIGURATION ---
	$secret_key = "govet__secret_16"; 
	$method = "AES-128-ECB";
	
	// --- DECRYPTION FUNCTION ---
	function decrypt_data($encrypted_string, $key, $method) {
		$input = trim($encrypted_string, '/');
		$base64 = str_replace(['-','_'], ['+','/'], $input);
		$len = strlen($base64);
		if ($len % 4 != 0) {
			$base64 .= str_repeat('=', 4 - ($len % 4));
		}
		$decoded = openssl_decrypt($base64, $method, $key);
		return json_decode($decoded, true);
	}
	
	// --- LOGIC START ---
	$screen = '';
	$id = 0;
	
	// 1. Try to get data from Encrypted Path
	$pathInfo = isset($_SERVER['PATH_INFO']) ? $_SERVER['PATH_INFO'] : '';
	
	if (!empty($pathInfo)) {
		$data = decrypt_data($pathInfo, $secret_key, $method);
		if ($data) {
			$screen = isset($data['screen']) ? $data['screen'] : '';
			$id = isset($data['id']) ? intval($data['id']) : 0;
		}
	}
	
	// 2. Fallback: Only check $_GET if we didn't find data in the path
	// FIX: Added "!$screen" check here to prevent overwriting
	if (empty($screen)) {
		$screen = isset($_GET['screen']) ? $con->real_escape_string($_GET['screen']) : '';
		$id = isset($_GET['id']) ? intval($_GET['id']) : 0;
	}
	
	$thumbGen = new ThumbnailGenerator();
	// Default Meta
	$ogTitle = "Aauraa";
	$ogDesc = "Find ur vibe. Find ur club.";
	$ogImage = "https://govet.ae/mec/default/logo.jpg"; 
	$redirectUrl = "https://govet.ae"; 
	
	// 3. Fetch Data from DB
	// Note: ensured $id is safe using intval() above
	if ($screen === 'SHOP' && $id > 0) {
		$query = "SELECT * FROM business WHERE b_id='$id'";
		$result = mysqli_query($con, $query);
		if ($result && mysqli_num_rows($result) > 0) {
			$content = mysqli_fetch_assoc($result);
			$ogTitle = $content['b_title'];
			$ogDesc = mb_substr(strip_tags($content['b_about']), 0, 150) . '...';
			$rawImageUrl = "https://govet.ae/mec/" . $content['s_cover'];
            $ogImage = $thumbGen->getThumbnail($rawImageUrl, 630, 1200);
			$redirectUrl = "https://govet.ae/store-view/" . $id; // Ensure this route exists in Flutter
		}
		} else if ($screen === 'PRODUCT' && $id > 0) {
		$query = "SELECT * FROM products WHERE p_id='$id'";
		$result = mysqli_query($con, $query);
		if ($result && mysqli_num_rows($result) > 0) {
			$content = mysqli_fetch_assoc($result);
			$ogTitle = $content['p_title'];
			$ogDesc = mb_substr(strip_tags($content['p_detail']), 0, 150) . '...';
			$rawImageUrl = "https://govet.ae/mec/" . $content['p_cover'];
            $ogImage = $thumbGen->getThumbnail($rawImageUrl, 630, 1200);
			$redirectUrl = "https://govet.ae/user/" . $id; // Ensure this route exists in Flutter
		}
	}
	
	// Close connection (removed $con->close() if using procedural mysqli)
	// If $con is an object use $con->close(); if resource use mysqli_close($con);
	// mysqli_close($con); 
	
	//Run this command on your Ubuntu server:
	//mkdir -p /var/www/html/cache/og
	//sudo chown -R www-data:www-data /var/www/html/cache
	//sudo chmod -R 755 /var/www/html/cache
?>

<!DOCTYPE html>
<html lang="en">
	<head>
		<meta charset="UTF-8">
		<title><?php echo $ogTitle; ?></title>
		
		<meta property="og:title" content="<?php echo $ogTitle; ?>" />
		<meta property="og:description" content="<?php echo $ogDesc; ?>" />
		<meta property="og:image" content="<?php echo $ogImage; ?>" />
		<meta property="og:type" content="website" />
		<meta property="og:url" content="<?php echo "https://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]"; ?>" />
		
		<meta name="twitter:card" content="summary_large_image">
		<meta name="twitter:title" content="<?php echo $ogTitle; ?>">
		<meta name="twitter:description" content="<?php echo $ogDesc; ?>">
		<meta name="twitter:image" content="<?php echo $ogImage; ?>">
		
		    <link rel="apple-touch-icon" href="/assets/icons/Icon-192.png">
    <!-- Favicon -->
    <link rel="icon" type="image/png" href="/assets/icons/favicon-96x96.png" sizes="96x96" />
    <link rel="icon" type="image/svg+xml" href="/assets/icons/favicon.svg" />
    <link rel="shortcut icon" href="/assets/icons/favicon.ico" />
    <link rel="apple-touch-icon" sizes="180x180" href="/assets/icons/apple-touch-icon.png" />


		
    <style>
        /* Prevent text selection during dragging */
        * {
          -webkit-user-select: none;
          -moz-user-select: none;
          -ms-user-select: none;
          user-select: none;
        }

        /* Enable text selection for input fields */
        input, textarea {
          -webkit-user-select: text;
          -moz-user-select: text;
          -ms-user-select: text;
          user-select: text;
        }

        /* Improve touch responsiveness */
        body {
          touch-action: manipulation;
          -webkit-touch-callout: none;
          -webkit-tap-highlight-color: transparent;
        }

        /* Loading Animation Styles */
        .loading {
          display: flex;
          flex-direction: column;
          justify-content: center;
          align-items: center;
          margin: 0;
          position: absolute;
          top: 50%;
          left: 50%;
          -ms-transform: translate(-50%, -50%);
          transform: translate(-50%, -50%);
          text-align: center;
          z-index: 999;
          background: #D3ECF4;
          width: 100%;
          height: 100%;
        }
        .loading .logo-container {
          width: auto;
          height: 250px;
          margin-bottom: 20px;
          display: flex;
          justify-content: center;
          align-items: center;
        }
        .loading .logo {
          width: 100%;
          height: 100%;
          object-fit: contain;
        }

        .loading.hide {
          display: none;
        }
    </style>
	</head>
	<body>
	<div class="loading">
    <div class="logo-container">
        <img src="/assets/icons/logo.gif" alt="Logo" class="logo">
    </div>
</div>
<script type="text/javascript">
        // The Magic Redirect
        window.location.href = "<?php echo $redirectUrl; ?>";
    </script>
		
	</body>
</html>