<?php

namespace GriffinForms\Includes\Packages;

use GriffinForms\Traits\Singleton;

class PackageStateStore extends \GriffinForms\Format
{
    use Singleton;

    public const SNAPSHOT_OPTION = 'package_state_snapshot';
    public const LIFECYCLE_OPTION = 'package_lifecycle_state';
    public const ENTITLEMENT_OPTION = 'package_entitlement_state';

    public function getSnapshot(): array
    {
        $snapshot = $this->settings->getOption(self::SNAPSHOT_OPTION, []);
        return is_array($snapshot) ? $snapshot : [];
    }

    public function saveSnapshot(array $snapshot): bool
    {
        return (bool) $this->settings->updateOption(self::SNAPSHOT_OPTION, $snapshot, 'packages');
    }

    public function getLifecycleState(): array
    {
        $state = $this->settings->getOption(self::LIFECYCLE_OPTION, []);
        return is_array($state) ? $state : [];
    }

    public function saveLifecycleState(array $state): bool
    {
        return (bool) $this->settings->updateOption(self::LIFECYCLE_OPTION, $state, 'packages');
    }

    public function getEntitlementState(): array
    {
        $state = $this->settings->getOption(self::ENTITLEMENT_OPTION, []);
        return is_array($state) ? $state : [];
    }

    public function saveEntitlementState(array $state): bool
    {
        return (bool) $this->settings->updateOption(self::ENTITLEMENT_OPTION, $state, 'packages');
    }

    public function getPackageEntitlementState(string $package_id): array
    {
        $state = $this->getEntitlementState();
        return is_array($state[$package_id] ?? null) ? $state[$package_id] : [];
    }

    public function savePackageEntitlementState(string $package_id, array $package_state): bool
    {
        $state = $this->getEntitlementState();
        $state[$package_id] = $package_state;

        return $this->saveEntitlementState($state);
    }

    public function markFirstInstalled(string $package_id, int $user_id = 0): void
    {
        $state = $this->getLifecycleState();
        if (!isset($state[$package_id])) {
            $state[$package_id] = [];
        }

        if (!empty($state[$package_id]['first_installed_at'])) {
            return;
        }

        $state[$package_id]['first_installed_at'] = current_time('mysql', true);
        $state[$package_id]['first_installed_user_id'] = max(0, $user_id);
        $this->saveLifecycleState($state);
    }

    public function markActivated(string $package_id, int $user_id = 0): void
    {
        $state = $this->getLifecycleState();
        if (!isset($state[$package_id])) {
            $state[$package_id] = [];
        }

        if (empty($state[$package_id]['first_installed_at'])) {
            $state[$package_id]['first_installed_at'] = current_time('mysql', true);
            $state[$package_id]['first_installed_user_id'] = max(0, $user_id);
        }

        $state[$package_id]['last_activated_at'] = current_time('mysql', true);
        $state[$package_id]['last_activated_user_id'] = max(0, $user_id);
        $this->saveLifecycleState($state);
    }

    public function markDeactivated(string $package_id, int $user_id = 0): void
    {
        $state = $this->getLifecycleState();
        if (!isset($state[$package_id])) {
            $state[$package_id] = [];
        }

        $state[$package_id]['last_deactivated_at'] = current_time('mysql', true);
        $state[$package_id]['last_deactivated_user_id'] = max(0, $user_id);
        $this->saveLifecycleState($state);
    }

    public function markDeleted(string $package_id, int $user_id = 0): void
    {
        $state = $this->getLifecycleState();
        if (!isset($state[$package_id])) {
            $state[$package_id] = [];
        }

        $state[$package_id]['last_deleted_at'] = current_time('mysql', true);
        $state[$package_id]['last_deleted_user_id'] = max(0, $user_id);
        $this->saveLifecycleState($state);
    }
}
