<?php

namespace App\Http\Controllers\Api;

use App\Http\Controllers\Controller;
use App\Models\Setting;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Validation\ValidationException;

class SettingController extends Controller
{
    public function index(Request $request): JsonResponse
    {
        try {
            $query = Setting::query();

            if ($request->filled('group')) {
                $query->where('group', $request->group);
            }

            $settings = $query->orderBy('group')->orderBy('key')->get();

            return response()->json([
                'success' => true,
                'data' => $settings,
                'message' => 'لیست تنظیمات',
            ]);
        } catch (\Exception $e) {
            return response()->json([
                'success' => false,
                'message' => 'خطا در دریافت تنظیمات',
            ], 500);
        }
    }

    public function store(Request $request): JsonResponse
    {
        try {
            $request->validate([
                'key' => 'required|string|max:255|unique:settings,key',
                'value' => 'present',
                'group' => 'nullable|string|max:255',
                'type' => 'nullable|string|max:50',
            ]);

            $setting = Setting::create([
                'key' => $request->input('key'),
                'value' => $request->input('value'),
                'group' => $request->input('group', 'general'),
                'type' => $request->input('type'),
            ]);

            return response()->json([
                'success' => true,
                'data' => $setting,
                'message' => 'تنظیم با موفقیت ایجاد شد',
            ], 201);
        } catch (ValidationException $e) {
            return response()->json([
                'success' => false,
                'message' => 'اطلاعات تنظیم معتبر نیست',
                'errors' => $e->errors(),
            ], 422);
        } catch (\Exception $e) {
            return response()->json([
                'success' => false,
                'message' => 'خطا در ایجاد تنظیم',
            ], 500);
        }
    }

    public function update(Request $request, Setting $setting): JsonResponse
    {
        try {
            $request->validate([
                'value' => 'present',
                'group' => 'nullable|string|max:255',
                'type' => 'nullable|string|max:50',
            ]);

            $setting->update([
                'value' => $request->input('value'),
                'group' => $request->input('group', $setting->group),
                'type' => $request->input('type', $setting->type),
            ]);

            return response()->json([
                'success' => true,
                'data' => $setting->fresh(),
                'message' => 'تنظیم به‌روزرسانی شد',
            ]);
        } catch (ValidationException $e) {
            return response()->json([
                'success' => false,
                'message' => 'اطلاعات تنظیم معتبر نیست',
                'errors' => $e->errors(),
            ], 422);
        } catch (\Exception $e) {
            return response()->json([
                'success' => false,
                'message' => 'خطا در به‌روزرسانی تنظیم',
            ], 500);
        }
    }
}
