<?php

namespace Ignite\Core;

use Exception;
use Illuminate\Foundation\Application;
use Illuminate\Support\Str;

class Program
{
    public const TYPE_PAYOUT = 'payout';
    public const TYPE_REDEMPTION = 'redemption';

    /**
     * @var Application
     */
    private $application;

    /**
     * @var string
     */
    private $version;

    /**
     * Program constructor.
     *
     * @param Application $application
     */
    public function __construct(Application $application)
    {
        $this->application = $application;
    }

    /**
     * The type of program.
     *
     * @return string
     */
    public function getType()
    {
        return $this->application['config']->get('core.program_type', static::TYPE_REDEMPTION);
    }

    /**
     * Determine if this is a payout program.
     *
     * @return bool
     */
    public function isPayout()
    {
        return $this->getType() == self::TYPE_PAYOUT;
    }

    /**
     * Determine if this is a redemption program.
     *
     * @return bool
     */
    public function isRedemption()
    {
        return $this->getType() == self::TYPE_REDEMPTION;
    }

    /**
     * The version of the Ignite platform.
     *
     * @return string
     * @throws Exception
     */
    public function version()
    {
        if (! $this->version) {
            $this->version = cache()->rememberForever('version', function () {
                return Version::instance()->get();
            });
        }

        return $this->version;
    }

    /**
     * Is the user viewing the backend?
     *
     * @return bool
     */
    public function isViewingBackend(): bool
    {
        return Str::contains(url()->current(), 'admin');
    }
}
