<?php
declare(strict_types=1);

require_once dirname(__DIR__) . '/includes/api_helpers.php';

$db = gpc_db();
$body = gpc_parse_body();
$isGpc = gpc_has_device_header()
    || (is_array($body) && (isset($body['app_type']) || isset($body['version']) || array_key_exists('is_paid', $body)));

$mac = null;
$key = null;

if (is_array($body) && !empty($body['app_device_id'])) {
    $mac = gpc_mac_from_device_id((string) $body['app_device_id']);
    if ($mac) {
        $key = gpc_device_key($mac);
    }
}

if (!$mac || !$key) {
    gpc_json_out([
        'is_google_paid' => false,
        'is_paid'        => false,
        'status'         => 'invalid',
        'mac_registered' => false,
        'mac_address'    => '',
        'expire_date'    => null,
        'device_key'     => '',
        'notification'   => '',
        'urls'           => [],
    ]);
    exit;
}

gpc_auto_register_mac($mac, $key);
gpc_json_out(gpc_build_guim($mac, $key));
