<?php

namespace GriffinForms\Includes\Onboarding;

use GriffinForms\Traits\Singleton;

class FeatureFlags extends \GriffinForms\Format
{
    use Singleton;

    public const OPTION_NAME = 'onboarding_feature_flags';
    public const OPTION_GROUP = 'onboarding';
    public const EXPERIENCE_VERSION = 'v1';
    public const PAGE_SLUG = 'griffinforms-onboarding';
    public const FORMS_PAGE_SLUG = 'griffinforms-forms';

    public function getExperienceVersion(): string
    {
        return self::EXPERIENCE_VERSION;
    }

    public function getOptionName(): string
    {
        return self::OPTION_NAME;
    }

    public function getOptionGroup(): string
    {
        return self::OPTION_GROUP;
    }

    public function getPageSlug(): string
    {
        return self::PAGE_SLUG;
    }

    public function getFormsPageSlug(): string
    {
        return self::FORMS_PAGE_SLUG;
    }

    public function getFormsPageUrl(array $query_args = []): string
    {
        return add_query_arg($query_args, admin_url('admin.php?page=' . self::FORMS_PAGE_SLUG));
    }

    public function getOnboardingPageUrl(array $query_args = []): string
    {
        return add_query_arg($query_args, admin_url('admin.php?page=' . self::PAGE_SLUG));
    }

    public function getDefaultFlags(): array
    {
        return [
            'enabled' => 1,
            'activation_redirect' => 1,
            'page_access' => 1,
            'forms_empty_state_cta' => 1,
            'import_entry' => 1,
            'welcome_step' => 1,
            'resume' => 1,
            'graphics' => 1,
            'goal_step' => 1,
            'starter_preview_step' => 1,
            'publish_step' => 1,
        ];
    }

    public function getFlags(): array
    {
        $stored_flags = $this->settings->getOption(self::OPTION_NAME, []);
        if (!is_array($stored_flags)) {
            $stored_flags = [];
        }

        $flags = array_merge($this->getDefaultFlags(), $stored_flags);
        foreach ($flags as $key => $value) {
            $flags[$key] = (int) (!empty($value));
        }

        return $flags;
    }

    public function isEnabled(string $flag_name): bool
    {
        $flags = $this->getFlags();
        return !empty($flags[$flag_name]);
    }

    public function getEnabledSteps(): array
    {
        $steps = [
            'welcome' => 'welcome_step',
            'goal' => 'goal_step',
            'starter' => 'starter_preview_step',
            'publish' => 'publish_step',
        ];

        $enabled_steps = [];
        foreach ($steps as $step_key => $flag_key) {
            if ($this->isEnabled($flag_key)) {
                $enabled_steps[] = $step_key;
            }
        }

        return $enabled_steps;
    }

    public function getFirstEnabledStep(): ?string
    {
        $enabled_steps = $this->getEnabledSteps();
        return empty($enabled_steps) ? null : $enabled_steps[0];
    }

    public function isStepEnabled(string $step_key): bool
    {
        $step_flags = [
            'welcome' => 'welcome_step',
            'goal' => 'goal_step',
            'starter' => 'starter_preview_step',
            'publish' => 'publish_step',
            'success' => 'publish_step',
        ];

        if (!isset($step_flags[$step_key])) {
            return false;
        }

        return $this->isEnabled($step_flags[$step_key]);
    }
}
