<?php

namespace App\Http\Controllers\Api\Admin;

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

class SettingApiController extends Controller
{
    // Withdrawal settings api
    public function withdrawalSettings(Request $request)
    {

        $request->validate([
            'min_withdrawal' => 'required|numeric',
            'max_withdrawal' => 'required|numeric',
            'withdrawal_fee' => 'required|numeric',
            'auto_withdraw' => 'required|numeric',
            'dis_with' => 'nullable'
        ]);
        
        $settings = Setting::all();
        
        // update min max fee
        updateSite([
            'dis_with' => $request->dis_with,
            'min_withdrawal' => $request->min_withdrawal,
            'max_withdrawal' => $request->max_withdrawal,
            'withdrawal_fee' => $request->withdrawal_fee,
            'auto_withdraw' => $request->auto_withdraw,
        ]);
        
        
        return response()->json([
            'status' => 'success',
            'message' => 'Withdrawal setting updated successfully',
            'data' => $settings
        ]);
    }
    
    
    
    public function index()
    {
        try {
            $settings = Setting::all()->pluck('value', 'key');

            return response()->json([
                'status' => 'success',
                'data' => $settings
            ]);
        } catch (\Exception $e) {
            return response()->json([
                'status' => 'error',
                'message' => 'Failed to fetch settings',
                'error' => $e->getMessage()
            ], 500);
        }
    }

    public function updateSettings(Request $request)
    {
        try {
            $settings = $request->all();
            
            foreach ($settings as $key => $value) {
                Setting::updateOrCreate(
                    ['key' => $key],
                    ['value' => $value]
                );
            }

            // Clear settings cache
            Cache::forget('site_settings');

            return response()->json([
                'status' => 'success',
                'message' => 'Settings updated successfully'
            ]);
        } catch (\Exception $e) {
            return response()->json([
                'status' => 'error',
                'message' => 'Failed to update settings',
                'error' => $e->getMessage()
            ], 500);
        }
    }

    public function getSetting($key)
    {
        try {
            $setting = Setting::where('key', $key)->first();
            
            if (!$setting) {
                return response()->json([
                    'status' => 'error',
                    'message' => 'Setting not found'
                ], 404);
            }

            return response()->json([
                'status' => 'success',
                'data' => $setting
            ]);
        } catch (\Exception $e) {
            return response()->json([
                'status' => 'error',
                'message' => 'Failed to fetch setting',
                'error' => $e->getMessage()
            ], 500);
        }
    }

    public function updateSetting(Request $request, $key)
    {
        try {
            $validator = Validator::make($request->all(), [
                'value' => 'required'
            ]);

            if ($validator->fails()) {
                return response()->json([
                    'status' => 'error',
                    'message' => 'Validation failed',
                    'errors' => $validator->errors()
                ], 422);
            }

            $setting = Setting::updateOrCreate(
                ['key' => $key],
                ['value' => $request->value]
            );

            // Clear settings cache
            Cache::forget('site_settings');

            return response()->json([
                'status' => 'success',
                'message' => 'Setting updated successfully',
                'data' => $setting
            ]);
        } catch (\Exception $e) {
            return response()->json([
                'status' => 'error',
                'message' => 'Failed to update setting',
                'error' => $e->getMessage()
            ], 500);
        }
    }
}