<?php

namespace App\Http\Admin;

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

class GameController extends Controller
{
    /**
     * IEU NU PENTING: Fungsi index kudu aya meh teu error mang!
     */
public function index()
{
    // Urang ambil data tina tabel games hungkul, teu kudu join ka products heula
    // Ieu pasti jalan sabab moal neangan kolom 'category_id'
    $categories = DB::table('games')
        ->select('id', 'name', 'slug', 'image', 'is_active')
        ->orderBy('id', 'desc')
        ->get();

    return view('account.games', compact('categories'));
}

    /**
     * Simpen Game Anyar
     */
    public function store(Request $request)
    {
        $request->validate([
            'name' => 'required|string|max:255',
            'image' => 'required|image|mimes:jpg,png,jpeg|max:2048'
        ]);

        $data = [
            'name'        => $request->name,
            'slug'        => Str::slug($request->name),
            'icon'        => $request->icon ?? 'fas fa-gamepad',
            'description' => $request->description,
            'sort_order'  => 99,
            'is_active'   => true,
            'created_at'  => now(),
            'updated_at'  => now()
        ];

        if ($request->hasFile('image')) {
            $imageName = time() . '.' . $request->image->extension();
            $request->image->move(public_path('images/games'), $imageName);
            $data['image'] = 'images/games/' . $imageName;
        }

        DB::table('categories')->insert($data);

        return redirect()->route('account.games.index')->with('success', 'Game anyar geus ditambahkeun!');
    }

    /**
     * Update Data Game
     */
public function update(Request $request, $id)
{
    try {
        // Ambil data tina request
        $data = [
            'name'          => $request->name,
            'brand_code'    => $request->brand_code,
            'category_type' => $request->category_type,
            'input_type'    => $request->input_type,
            'description'   => $request->description,
            'icon'          => $request->icon,
            'sort_order'    => $request->sort_order ?? 0,
            'is_popular'    => $request->is_popular, // Ieu geus angka 1/0 ti JS
            'updated_at'    => now(),
        ];

        // Urusan gambar
        if ($request->hasFile('image')) {
            $imageName = time() . '.' . $request->image->extension();
            $request->image->move(public_path('images/games'), $imageName);
            $data['image'] = 'images/games/' . $imageName;
        }

        // Update ka tabel categories
        DB::table('categories')->where('id', $id)->update($data);

        // IEU KONCI NA: Balikkeun JSON meh JS abang teu "Server Error"
        return response()->json([
            'success' => true,
            'message' => 'Kategori ' . $request->name . ' geus diupdate mang!'
        ]);

    } catch (\Exception $e) {
        return response()->json([
            'success' => false,
            'message' => 'Aya nu salah: ' . $e->getMessage()
        ], 500);
    }
}

    /**
     * Hapus Game
     */
    public function destroy($id)
    {
        DB::table('categories')->where('id', $id)->delete();
        return redirect()->route('account.games.index')->with('success', 'Game dihapus!');
    }
}