#!/usr/bin/env php
<?php

require_once __DIR__ . '/../vendor/autoload.php';

use PostHog\PostHog;

if (in_array('--help', $argv, true)) {
    print(usage());
    exit;
}

date_default_timezone_set('UTC');

$options = getopt(
    '',
    array(
        'apiKey:',
        'type:',
        'host:',
        'distinctId:',
        'event:',
        'properties:',
        'alias:',
        'groupType:',
        'groupKey:',
        'no-ssl',
        'debug'
    )
);

if (empty($options['apiKey'])) {
    error('apiKey flag required');
}

PostHog::init(
    $options['apiKey'],
    array(
        'host' => $options['host'],
        'debug' => array_key_exists('debug', $options),
        'ssl' => !array_key_exists('no-ssl', $options)
    )
);

switch ($options['type']) {
    case 'capture':
        PostHog::capture(
            array(
                'distinctId' => $options['distinctId'],
                'event' => $options['event'],
                'properties' => parse_json($options['properties'])
            )
        );
        break;

    case 'identify':
        PostHog::identify(
            array(
                'distinctId' => $options['distinctId'],
                'properties' => parse_json($options['properties'])
            )
        );
        break;

    case 'alias':
        PostHog::alias(
            array(
                'distinctId' => $options['distinctId'],
                'alias' => $options['alias']
            )
        );
        break;

    case 'groupIdentify':
        PostHog::groupIdentify(
            array(
                'groupType' => $options['groupType'],
                'groupKey' => $options['groupKey'],
                'properties' => parse_json($options['properties'])
            )
        );
        break;

    default:
        error(usage());
        break;
}

PostHog::flush();

function usage(): string
{
    return "\n  Common keys:" .
        "\n    --apiKey KEY" .
        "\n    --host HOST" .
        "\n    --debug" .
        "\n    --no-ssl" .
        "\n" .
        "\n  Usage:" .
        "\n    posthog --type capture --distinctId \"id\" --event \"event name\" --properties '{ \"json\": \"object\" }'" .
        "\n    posthog --type identify --distinctId \"id\" --properties '{ \"json\": \"object\" }'" .
        "\n    posthog --type alias --distinctId \"id\" --alias \"alias\"" .
        "\n    posthog --type groupIdentify --groupType \"organization\" --groupKey \"id:5\" --properties '{ \"json\": \"object\" }'" .
        "\n\n\n";
}

function error($message)
{
    print("$message\n\n");
    exit(1);
}

function parse_json($input)
{
    if (empty($input)) {
        return null;
    }

    return json_decode($input, true);
}

function parse_timestamp($input)
{
    if (empty($input)) {
        return null;
    }

    return strtotime($input);
}
