<?php

/*
|--------------------------------------------------------------------------
| OwnStore - Build your own online store
| Author: TakfeDev (MKJUEL)
| All Rights Reserved
|--------------------------------------------------------------------------
*/

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Models\Language;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\File;
use Illuminate\Support\Facades\Cache; 
use Inertia\Inertia;

class LanguageController extends Controller
{
    public function index()
    {
        return Inertia::render('cp/Settings/LanguageManager');
    }

    public function getLanguages()
    {
        return response()->json(Language::orderBy('is_default', 'desc')->get());
    }

    public function store(Request $request)
    {
        $request->validate([
            'name' => 'required|string|max:255',
            'code' => 'required|string|max:10|unique:languages,code',
            'is_rtl' => 'boolean',
        ]);

        $lang = Language::create($request->only('name', 'code', 'is_rtl'));

        $path = base_path("lang/{$lang->code}.json");
        if (!File::exists(base_path('lang'))) {
            File::makeDirectory(base_path('lang'));
        }
        if (!File::exists($path)) {
            File::put($path, '{}');
        }

        Cache::forget('active_languages'); // <-- CLEAR CACHE

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

    public function setDefault(Language $language)
    {
        Language::query()->update(['is_default' => false]);
        $language->update(['is_default' => true]);
        Cache::forget('active_languages'); // <-- CLEAR CACHE
        return response()->json(['success' => true]);
    }

    public function destroy(Language $language)
    {
        if ($language->is_default) {
            return response()->json(['message' => 'Cannot delete the default language.'], 400);
        }
        $language->delete();
        Cache::forget('active_languages'); // <-- CLEAR CACHE
        return response()->json(['success' => true]);
    }

    public function getTranslations($code)
    {
        $path = base_path("lang/{$code}.json");
        $content = File::exists($path) ? File::get($path) : '{}';
        return response()->json(['content' => $content]);
    }

    public function updateTranslations(Request $request, $code)
    {
        $request->validate(['content' => 'required|string']);
        
        json_decode($request->content);
        if (json_last_error() !== JSON_ERROR_NONE) {
            return response()->json(['message' => 'Invalid JSON format'], 422);
        }

        $path = base_path("lang/{$code}.json");
        File::put($path, $request->content);

        return response()->json(['success' => true]);
    }

    // --- UPDATED: Prevents 404 error if code doesn't exist ---
    public function switchLanguage($code)
    {
        $lang = Language::where('code', $code)->first();
        
        if ($lang) {
            session(['locale' => $lang->code, 'is_rtl' => $lang->is_rtl]);
        }
        
        return back();
    }
}