<?php

namespace RachidLaasri\LaravelInstaller\Repositories;

use App\Models\SettingTwo;
use Closure;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Schema;
use Illuminate\Support\Facades\Storage;

class ApplicationStatusRepository implements ApplicationStatusRepositoryInterface
{
    public string $baseLicenseUrl = 'https://portal.liquid-themes.com/api/license';
////////////////
public function financePage(string $view = 'panel.admin.finance.gateways.particles.finance'): string  
{  
    if (true) {  // This will always evaluate to true  
        return $view;  
    }  
  
    return 'panel.admin.finance.gateways.particles.license';  
}
////////////////
    public function financeLicense(): bool  
{  
    return true;  // Always return true to grant access  
}  
  
public function licenseType(): string  
{  
    return 'Extended License';  // Always return Extended License  
}

   public function check(string $licenseKey, bool $installed = false): bool  
{  
    $portal = $this->portal() ?: [];  
      
    $data = array_merge($portal, [  
        'liquid_license_type'       => 'Extended License',  
        'liquid_license_domain_key' => $licenseKey,  
        'installed'                 => true,  
        'blocked'                   => false  
    ]);  
  
    $this->save($data);  
    return true;  
}

    public function portal()
    {
        $data = Storage::disk('local')->get('portal');

        if ($data) {
            return unserialize(trim($data));
        }

        return null;
    }

    public function getVariable(string $key)
    {
        $portal = $this->portal();

        return data_get($portal, $key);
    }

    public function save($data): bool
    {
        return Storage::disk('local')->put('portal', serialize($data));
    }

    public function setLicense(): void  
{  
    $data = [  
        'installed' => true,  
        'liquid_license_type' => 'Extended License',  
        'liquid_license_domain_key' => 'VALID-KEY',  
        'blocked' => false  
    ];  
  
    $this->save($data);  
  
    if (Schema::hasTable('settings_two')) {  
        SettingTwo::query()->first()->update([  
            'liquid_license_type' => 'Extended License',  
            'liquid_license_domain_key' => 'VALID-KEY'  
        ]);  
    }  
}

    public function generate(Request $request): bool
    {
        if ($request->exists(['liquid_license_status', 'liquid_license_domain_key', 'liquid_license_domain_key'])) {
            return $this->check($request->input('liquid_license_domain_key'), true);
        }

        return false;
    }

    public function next($request, Closure $next)
    {

        $portal = $this->portal();

        if (is_null($portal)) {
            return redirect()->route('LaravelInstaller::license');
        }

        $liquid_license_domain_key = data_get($portal, 'liquid_license_domain_key');

        if (! $liquid_license_domain_key) {
            return redirect()->route('LaravelInstaller::license');
        }

        $blocked = data_get($portal, 'blocked');

        if ($blocked) {
            abort(500);
        }

        return $next($request);
    }

    public function webhook($request)  
{  
    $portal = [  
        'blocked' => false,  
        'liquid_license_type' => 'Extended License',  
        'liquid_license_domain_key' => 'VALID-KEY',  
        'installed' => true  
    ];  
      
    $this->save($portal);  
      
    return response()->noContent();  
}

    public function appKey(): string
    {
        return md5(config('app.key'));
    }
}
