<?php

namespace App\Http\Controllers\Api;

use App\Http\Controllers\Controller;
use App\Models\AuditLog;
use App\Models\Setting;
use Illuminate\Http\Request;

class SettingsController extends Controller
{
    // GET /api/settings/pricing  (public — all users can see prices)
    public function getPricing()
    {
        $setting = Setting::where('key', 'key_prices')->first();
        $prices  = $setting ? $setting->value : [];
        return response()->json(['success' => true, 'data' => $prices]);
    }

    // POST /api/settings/pricing  (owner/admin only)
    public function setPricing(Request $request)
    {
        $user = auth()->user();
        if (!$user->isOwner()) {
            return response()->json(['success' => false, 'message' => 'Owner access required'], 403);
        }

        $request->validate([
            'prices'               => 'required|array',
            'prices.*.duration'    => 'required|integer|min:1',
            'prices.*.label'       => 'required|string',
            'prices.*.price'       => 'required|numeric|min:0',
        ]);

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

        AuditLog::record('UPDATE_PRICING', 'Pricing table updated');
        return response()->json(['success' => true, 'message' => 'Pricing updated successfully']);
    }

    public function getAnnouncement()
    {
        $setting = Setting::where('key', 'announcement')->first();
        return response()->json(['success' => true, 'data' => $setting ? $setting->value : null]);
    }

    public function setAnnouncement(Request $request)
    {
        $user = auth()->user();
        if (!$user->isOwner()) {
            return response()->json(['success' => false, 'message' => 'Owner access required'], 403);
        }

        $request->validate([
            'message' => 'nullable|string'
        ]);

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

        AuditLog::record('UPDATE_ANNOUNCEMENT', 'Announcement updated: ' . ($request->message ?: 'Cleared'));
        return response()->json(['success' => true, 'message' => 'Announcement saved successfully']);
    }

    public function getModes()
    {
        $setting = Setting::where('key', 'app_modes')->first();
        $modes = $setting ? $setting->value : [
            'imgui_mode_enabled' => true,
            'auto_mode_enabled' => true,
        ];
        return response()->json(['success' => true, 'data' => $modes]);
    }

    public function setModes(Request $request)
    {
        $user = auth()->user();
        if (!$user->isOwner()) {
            return response()->json(['success' => false, 'message' => 'Owner access required'], 403);
        }

        $request->validate([
            'imgui_mode_enabled' => 'required|boolean',
            'auto_mode_enabled'  => 'required|boolean',
        ]);

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

        AuditLog::record('UPDATE_APP_MODES', 'Application mode toggles updated');
        return response()->json(['success' => true, 'message' => 'App modes updated successfully']);
    }
}
