<?php

declare(strict_types=1);

namespace App\Http\Controllers\Admin;

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

/**
 * Admin Maintenance Mode (WHM Maintenance Mode muadili).
 * Panel genelinde bakım modu yönetimi — anında etkinleştirme, zamanlama ve izin verilen IP'ler.
 */
class MaintenanceModeController extends Controller
{
    public function index(Request $request): Response
    {
        $settings = DB::table('maintenance_mode')->first();
        $current_admin_ip = $request->ip();

        return Inertia::render('Admin/Maintenance/Index', compact('settings', 'current_admin_ip'));
    }

    public function update(Request $request): RedirectResponse
    {
        $request->validate([
            'is_active' => ['required', 'boolean'],
            'scheduled_start' => ['nullable', 'date'],
            'scheduled_end' => ['nullable', 'date'],
            'message_tr' => ['nullable', 'string', 'max:1000'],
            'message_en' => ['nullable', 'string', 'max:1000'],
            'allowed_ips' => ['nullable', 'array'],
            'allowed_ips.*' => ['ip'],
            'show_countdown' => ['required', 'boolean'],
            'retry_after_seconds' => ['required', 'integer', 'min:60'],
        ]);

        DB::table('maintenance_mode')->where('id', 1)->update([
            'is_active' => $request->boolean('is_active'),
            'scheduled_start' => $request->scheduled_start,
            'scheduled_end' => $request->scheduled_end,
            'message_tr' => $request->message_tr,
            'message_en' => $request->message_en,
            'allowed_ips' => json_encode($request->allowed_ips ?? []),
            'show_countdown' => $request->boolean('show_countdown'),
            'retry_after_seconds' => $request->retry_after_seconds,
            'enabled_by' => auth()->id(),
            'enabled_at' => $request->boolean('is_active') ? now() : null,
            'updated_at' => now(),
        ]);

        return back()->with('success', $request->boolean('is_active')
            ? 'Bakım modu aktifleştirildi.'
            : 'Bakım modu kapatıldı.'
        );
    }

    public function enableNow(Request $request): RedirectResponse
    {
        $allowedIps = [$request->ip()];

        DB::table('maintenance_mode')->where('id', 1)->update([
            'is_active' => true,
            'allowed_ips' => json_encode($allowedIps),
            'enabled_by' => auth()->id(),
            'enabled_at' => now(),
            'updated_at' => now(),
        ]);

        return back()->with('success', "Bakım modu derhal aktifleştirildi (sadece IP'niz erişebilir).");
    }

    public function disable(Request $request): RedirectResponse
    {
        DB::table('maintenance_mode')->where('id', 1)->update([
            'is_active' => false,
            'enabled_at' => null,
            'updated_at' => now(),
        ]);

        return back()->with('success', 'Bakım modu kapatıldı.');
    }
}
