<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Artisan;
use Illuminate\Support\Facades\File;

class AdminCacheController extends Controller
{
    public function index()
    {
        return view('admin.cache.index');
    }

    public function clearAll(Request $request)
    {
        try {
            // Clear application cache
            Artisan::call('cache:clear');
            
            // Clear config cache
            Artisan::call('config:clear');
            
            // Clear route cache
            Artisan::call('route:clear');
            
            // Clear view cache
            Artisan::call('view:clear');
            
            // Clear compiled classes
            Artisan::call('clear-compiled');
            
            return back()->with('success', 'All caches cleared successfully!');
        } catch (\Exception $e) {
            return back()->with('error', 'Error clearing caches: ' . $e->getMessage());
        }
    }

    public function clearApplication(Request $request)
    {
        try {
            Artisan::call('cache:clear');
            return back()->with('success', 'Application cache cleared successfully!');
        } catch (\Exception $e) {
            return back()->with('error', 'Error clearing application cache: ' . $e->getMessage());
        }
    }

    public function clearConfig(Request $request)
    {
        try {
            Artisan::call('config:clear');
            return back()->with('success', 'Config cache cleared successfully!');
        } catch (\Exception $e) {
            return back()->with('error', 'Error clearing config cache: ' . $e->getMessage());
        }
    }

    public function clearRoute(Request $request)
    {
        try {
            Artisan::call('route:clear');
            return back()->with('success', 'Route cache cleared successfully!');
        } catch (\Exception $e) {
            return back()->with('error', 'Error clearing route cache: ' . $e->getMessage());
        }
    }

    public function clearView(Request $request)
    {
        try {
            Artisan::call('view:clear');
            return back()->with('success', 'View cache cleared successfully!');
        } catch (\Exception $e) {
            return back()->with('error', 'Error clearing view cache: ' . $e->getMessage());
        }
    }

    public function cacheConfig(Request $request)
    {
        try {
            Artisan::call('config:cache');
            return back()->with('success', 'Config cached successfully!');
        } catch (\Exception $e) {
            return back()->with('error', 'Error caching config: ' . $e->getMessage());
        }
    }

    public function cacheRoute(Request $request)
    {
        try {
            Artisan::call('route:cache');
            return back()->with('success', 'Routes cached successfully!');
        } catch (\Exception $e) {
            return back()->with('error', 'Error caching routes: ' . $e->getMessage());
        }
    }

    public function cacheView(Request $request)
    {
        try {
            Artisan::call('view:cache');
            return back()->with('success', 'Views cached successfully!');
        } catch (\Exception $e) {
            return back()->with('error', 'Error caching views: ' . $e->getMessage());
        }
    }

    public function optimize(Request $request)
    {
        try {
            Artisan::call('optimize');
            return back()->with('success', 'Application optimized successfully!');
        } catch (\Exception $e) {
            return back()->with('error', 'Error optimizing application: ' . $e->getMessage());
        }
    }

    public function getCacheStatus()
    {
        $status = [
            'bootstrap_cache' => $this->getDirectorySize(base_path('bootstrap/cache')),
            'storage_framework' => $this->getDirectorySize(storage_path('framework/cache')),
            'storage_views' => $this->getDirectorySize(storage_path('framework/views')),
            'config_cached' => file_exists(base_path('bootstrap/cache/config.php')),
            'routes_cached' => file_exists(base_path('bootstrap/cache/routes-v7.php')),
        ];

        return response()->json($status);
    }

    private function getDirectorySize($path)
    {
        if (!File::exists($path)) {
            return 0;
        }

        $size = 0;
        $files = File::allFiles($path);
        
        foreach ($files as $file) {
            $size += $file->getSize();
        }

        return $this->formatBytes($size);
    }

    private function formatBytes($bytes, $precision = 2)
    {
        $units = ['B', 'KB', 'MB', 'GB'];
        
        for ($i = 0; $bytes > 1024 && $i < count($units) - 1; $i++) {
            $bytes /= 1024;
        }
        
        return round($bytes, $precision) . ' ' . $units[$i];
    }
}
