<?php

namespace App\Installer\Services;

use Illuminate\Support\Facades\Cache;

/**
 * License Storage Service
 * Handles secure storage of license information
 */
class LicenseStorage
{
    /**
     * Store license information securely
     */
    public function store(array $licenseData): bool
    {
        try {
            // Store in cache
            Cache::forever('license_info', $licenseData);

            // Store in file
            $licenseFile = storage_path('app/.license');
            file_put_contents($licenseFile, json_encode($licenseData));

            return true;
        } catch (\Exception $e) {
            return false;
        }
    }

    /**
     * Get stored license information
     */
    public function get(): ?array
    {
        try {
            // Try cache first
            $licenseData = Cache::get('license_info');

            if ($licenseData) {
                return $licenseData;
            }

            // Fallback to file
            $licenseFile = storage_path('app/.license');
            if (file_exists($licenseFile)) {
                $data = json_decode(file_get_contents($licenseFile), true);
                if ($data) {
                    Cache::forever('license_info', $data);

                    return $data;
                }
            }

            return null;
        } catch (\Exception $e) {
            return null;
        }
    }

    /**
     * Check if license is stored
     */
    public function exists(): bool
    {
        return $this->get() !== null;
    }
}
