<?php

namespace App\Http\Controllers\Api\Admin;

use App\Http\Controllers\Controller;
use App\Models\Setting;
use Illuminate\Http\Request;
use Illuminate\Http\JsonResponse;
use Illuminate\Support\Facades\Cache;

class PermalinkSettingsController extends Controller
{
    public function index(): JsonResponse
    {
        $structure = Setting::where('group', 'permalinks')->where('key', 'post_structure')->value('value') ?? 'default';
        return response()->json(['post_structure' => $structure]);
    }

    public function update(Request $request): JsonResponse
    {
        $request->validate(['post_structure' => 'required|in:default,slug_only,post_id']);
        
        Setting::updateOrCreate(['group' => 'permalinks', 'key' => 'post_structure'],['value' => $request->post_structure]
        );
        Cache::forget('permalink_structure');
        
        return response()->json(['message' => 'Permalink settings updated successfully.']);
    }
}