<?php
include '../session_check.php';
require '../envLoader.php';  // Load envLoader from php/
loadEnv(__DIR__ . '/../../.env');  // Go up three levels to root for .env

$clientId = $_ENV['ETSY_KEYSTRING'];
$redirectUri = $_ENV['ETSY_REDIRECT_URI'];
$scope = 'transactions_r';
$state = bin2hex(random_bytes(16));

session_start();
$_SESSION['oauth_state'] = $state;

// Authorization URL
$url = "https://www.etsy.com/oauth/connect?" . http_build_query([
    'response_type' => 'code',
    'client_id' => $clientId,
    'redirect_uri' => $redirectUri,
    'scope' => $scope,
    'state' => $state
]);

header("Location: $url");
exit;
?>
