<?php

declare(strict_types=1);

namespace Segment\Consumer;

class File extends Consumer
{
    protected string $type = 'File';

    /**
     * @var false|resource
     */
    private $file_handle;

    /**
     * The file consumer writes track and identify calls to a file.
     * @param string $secret
     * @param array $options
     *     string "filename" - where to log the analytics calls
     */
    public function __construct(string $secret, array $options = [])
    {
        if (!isset($options['filename'])) {
            $options['filename'] = sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'analytics.log';
        }

        parent::__construct($secret, $options);

        $this->file_handle = @fopen($options['filename'], 'ab');
        if ($this->file_handle === false) {
            $this->handleError(13, 'Failed to open analytics.log file');
            return;
        }
        if (isset($options['filepermissions'])) {
            chmod($options['filename'], $options['filepermissions']);
        } else {
            chmod($options['filename'], 0644);
        }
    }

    public function __destruct()
    {
        if (
            $this->file_handle &&
            get_resource_type($this->file_handle) !== 'Unknown'
        ) {
            fclose($this->file_handle);
        }
    }

    /**
     * Tracks a user action
     *
     * @param array $message
     * @return bool whether the track call succeeded
     */
    public function track(array $message): bool
    {
        return $this->write($message);
    }

    /**
     * Writes the API call to a file as line-delimited json
     * @param array $body post body content.
     * @return bool whether the request succeeded
     */
    private function write(array $body): bool
    {
        if (!$this->file_handle) {
            return false;
        }

        $content = json_encode($body);
        $content .= "\n";

        return fwrite($this->file_handle, $content) === strlen($content);
    }

    /**
     * Tags traits about the user.
     *
     * @param array $message
     * @return bool whether the identify call succeeded
     */
    public function identify(array $message): bool
    {
        return $this->write($message);
    }

    /**
     * Tags traits about the group.
     *
     * @param array $message
     * @return bool whether the group call succeeded
     */
    public function group(array $message): bool
    {
        return $this->write($message);
    }

    /**
     * Tracks a page view.
     *
     * @param array $message
     * @return bool whether the page call succeeded
     */
    public function page(array $message): bool
    {
        return $this->write($message);
    }

    /**
     * Tracks a screen view.
     *
     * @param array $message
     * @return bool whether the screen call succeeded
     */
    public function screen(array $message): bool
    {
        return $this->write($message);
    }

    /**
     * Aliases from one user id to another
     *
     * @param array $message
     * @return bool whether the alias call succeeded
     */
    public function alias(array $message): bool
    {
        return $this->write($message);
    }
}
