<?php

require 'vendor/autoload.php';
use Shopify\Context;
use Shopify\Auth\FileSessionStorage;
use Shopify\Clients\Graphql;
use Shopify\Clients\Rest;

function DB_connect()
{
    $env = parse_ini_file('.env');
    $server_name = $env['DB_HOST'];
    $username = $env['DB_USERNAME'];
    $password = $env['DB_PASSWORD'];
    $database = $env['DB_DATABASE'];

    $con = new mysqli($server_name, $username, $password, $database) or die("unable to connect");
    if ($con) {
        return $con;
    } else {
        return mysqli_connect_error();
    }
}



function graphql()
{

    $env = parse_ini_file('.env');

    $cont = Context::initialize(

        apiKey: $env['SHOPIFY_API_KEY'],
        apiSecretKey: $env['SHOPIFY_API_SECRET'],
        scopes: $env['SHOPIFY_APP_SCOPES'],
        hostName: $env['SHOPIFY_APP_HOST_NAME'],
        sessionStorage: new FileSessionStorage('/'),
        apiVersion: '2024-04',
        isEmbeddedApp: true,
        isPrivateApp: false,

    );
    return $client = new Graphql($env['SHOPIFY_APP_HOST_NAME'], $env['SHOPIFY_ACCESS_TOKEN']);
}

function rest()
{

    $env = parse_ini_file('.env');

    $cont = Context::initialize(

        apiKey: $env['SHOPIFY_API_KEY'],
        apiSecretKey: $env['SHOPIFY_API_SECRET'],
        scopes: $env['SHOPIFY_APP_SCOPES'],
        hostName: $env['SHOPIFY_APP_HOST_NAME'],
        sessionStorage: new FileSessionStorage('/'),
        apiVersion: '2024-04',
        isEmbeddedApp: true,
        isPrivateApp: false,

    );
    return $client = new Rest($env['SHOPIFY_APP_HOST_NAME'], $env['SHOPIFY_ACCESS_TOKEN']);

}

if (!function_exists('dd')) {

    function dd(mixed $data): void
    {
        echo "<pre style='background:#1d1d1d; color: greenyellow; position:absolute; left: 0; top: 0; z-index: 9999; width: 100%; height: 100%; margin: 0; padding: 20px; overflow: auto;'>";
        print_r($data);
        echo '</pre>';
        die();
    }


}