<?php

namespace App\Http\Controllers\Api;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class AppSettingController extends Controller
{
    public function getStatus() {
        // Menggunakan value default false jika belum ada di DB
        $setting = DB::table('app_settings')->where('key', 'is_upload_open')->first();
        return response()->json(['is_open' => $setting ? (bool)$setting->value : false]);
    }

    public function toggleStatus() {
        $current = DB::table('app_settings')->where('key', 'is_upload_open')->first();
        
        // Jika belum ada, buat baru dengan status true (1), jika ada balikkan nilainya
        $newValue = $current ? !$current->value : true;

        DB::table('app_settings')->updateOrInsert(
            ['key' => 'is_upload_open'],
            ['value' => $newValue, 'updated_at' => now()]
        );

        return response()->json([
            'success' => true,
            'is_open' => (bool)$newValue
        ]);
    }
}