<?php

/*
 * LaraClassifier - Classified Ads Web Application
 * Copyright (c) BeDigit. All Rights Reserved
 *
 * Website: https://laraclassifier.com
 * Author: Mayeul Akpovi (BeDigit - https://bedigit.com)
 *
 * LICENSE
 * -------
 * This software is provided under a license agreement and may only be used or copied
 * in accordance with its terms, including the inclusion of the above copyright notice.
 * As this software is sold exclusively on CodeCanyon,
 * please review the full license details here: https://codecanyon.net/licenses/standard
 */

namespace App\Http\Middleware;

use App\Http\Middleware\EnsureCachedAssetsExist\AddonAssets;
use Closure;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Artisan;

class EnsureCachedAssetsExist
{
	use AddonAssets;
	
	/**
	 * Handle an incoming request.
	 *
	 * @param \Illuminate\Http\Request $request
	 * @param \Closure $next
	 * @return mixed
	 */
	public function handle(Request $request, Closure $next)
	{
		// Skip during installation or update routes
		if (isAppInstallOrUpgradeRoute()) {
			return $next($request);
		}
		
		// Generate missing cache files on-demand (only check once per request)
		if (!app()->has('cached_assets_checked')) {
			$this->ensureCacheFilesExist();
			app()->instance('cached_assets_checked', true);
		}
		
		return $next($request);
	}
	
	/**
	 * Ensure all cache files exist, generate if missing
	 *
	 * @return void
	 */
	private function ensureCacheFilesExist(): void
	{
		try {
			$this->ensureFileinputLocalesExist();
			$this->ensureSkinsExist();
			$this->ensureStyleCssExists();
			$this->ensureHomepageCssExists();
			$this->ensureAddonAssetsExist();
		} catch (\Throwable $e) {
			// Silent fail - cache will be generated on the next request or regenerated by observers
		}
	}
	
	/**
	 * Ensure bootstrap-fileinput locale cache files exist
	 *
	 * @return void
	 */
	private function ensureFileinputLocalesExist(): void
	{
		$cacheDir = public_path('cache/plugins/bootstrap-fileinput/js/locales');
		
		if (!is_dir($cacheDir) || count(glob($cacheDir . '/*.js')) === 0) {
			Artisan::call('fileinput:cache-locales');
		}
	}
	
	/**
	 * Ensure skin cache files exist for all templates
	 *
	 * @return void
	 */
	private function ensureSkinsExist(): void
	{
		$frontSkinsDir = public_path('cache/css/skins/front');
		$authSkinsDir = public_path('cache/css/skins/auth');
		
		$frontSkinsExist = is_dir($frontSkinsDir) && count(glob($frontSkinsDir . '/*.css')) > 0;
		$authSkinsExist = is_dir($authSkinsDir) && count(glob($authSkinsDir . '/*.css')) > 0;
		
		if (!$frontSkinsExist || !$authSkinsExist) {
			Artisan::call('skins-css:cache');
		}
	}
	
	/**
	 * Ensure style CSS cache file exists
	 *
	 * @return void
	 */
	private function ensureStyleCssExists(): void
	{
		$cacheFile = public_path('cache/css/front-style.css');
		
		if (!file_exists($cacheFile)) {
			Artisan::call('style-css:cache');
		}
	}
	
	/**
	 * Ensure homepage CSS cache file exists
	 *
	 * @return void
	 */
	private function ensureHomepageCssExists(): void
	{
		$cacheFile = public_path('cache/css/front-homepage.css');
		
		if (!file_exists($cacheFile)) {
			Artisan::call('homepage-css:cache');
		}
	}
	
	/**
	 * Ensure add-on public assets are synced to the cache directory
	 *
	 * @return void
	 */
	private function ensureAddonAssetsExist(): void
	{
		// Sync assets from installed addons
		$this->syncAddonAssets();
		
		// Clean up assets from unavailable addons
		$this->cleanupUnavailableAddonAssets();
	}
}
