<?php

namespace App\Http\Controllers\Admin\Download;

use App\Http\Controllers\Controller;
use App\Models\DownloadItem;
use Illuminate\Http\Request;
use Inertia\Inertia;
use Inertia\Response;

class DownloadController extends Controller
{

    public function index(): Response
    {
        $downloadItems = DownloadItem::ordered()->get();

        return Inertia::render('admin/download/pages/DownloadListPage', [
            'downloadItems' => $downloadItems,
        ]);
    }

    public function create(): Response
    {
        return Inertia::render('admin/download/pages/DownloadCreatePage');
    }

    public function store(Request $request)
    {
        $validated = $request->validate([
            'name' => 'required|string|max:255',
            'description' => 'required|string',
            'size' => 'required|string|max:50',
            'url' => 'required|url',
            'icon_name' => 'string|max:50',
            'mirror_url' => 'nullable|url',
            'mirror_icon' => 'nullable|string|max:50',
            'sort_order' => 'integer|min:0',
            'is_active' => 'boolean',
        ]);

        DownloadItem::create($validated);

        // Return JSON for API requests
        if ($request->wantsJson() || $request->ajax()) {
            return response()->json([
                'success' => true,
                'message' => 'Download item created successfully.',
            ]);
        }

        return redirect()->route('admin.download.index')
            ->with('success', 'Download item created successfully.');
    }

    public function edit(DownloadItem $downloadItem): Response
    {
        return Inertia::render('admin/download/pages/DownloadEditPage', [
            'downloadItem' => $downloadItem,
        ]);
    }

    public function update(Request $request, DownloadItem $downloadItem)
    {
        $validated = $request->validate([
            'name' => 'required|string|max:255',
            'description' => 'required|string',
            'size' => 'required|string|max:50',
            'url' => 'required|url',
            'icon_name' => 'string|max:50',
            'mirror_url' => 'nullable|url',
            'mirror_icon' => 'nullable|string|max:50',
            'sort_order' => 'integer|min:0',
            'is_active' => 'boolean',
        ]);

        $downloadItem->update($validated);

        // Return JSON for API requests
        if ($request->wantsJson() || $request->ajax()) {
            return response()->json([
                'success' => true,
                'message' => 'Download item updated successfully.',
            ]);
        }

        return redirect()->route('admin.download.index')
            ->with('success', 'Download item updated successfully.');
    }

    public function destroy(Request $request, DownloadItem $downloadItem)
    {
        $downloadItem->delete();

        // Return JSON for API requests
        if ($request->wantsJson() || $request->ajax()) {
            return response()->json([
                'success' => true,
                'message' => 'Download item deleted successfully.',
            ]);
        }

        return redirect()->route('admin.download.index')
            ->with('success', 'Download item deleted successfully.');
    }

    public function toggleActive(Request $request, DownloadItem $downloadItem)
    {
        $downloadItem->update(['is_active' => !$downloadItem->is_active]);

        // Return JSON for API requests
        if ($request->wantsJson() || $request->ajax()) {
            return response()->json([
                'success' => true,
                'message' => 'Download item status updated successfully.',
            ]);
        }

        return back()->with('success', 'Download item status updated successfully.');
    }

    // Test method for debugging
    public function testMethod(): Response
    {
        $downloadItems = DownloadItem::ordered()->get();
        
        return Inertia::render('admin/download/pages/DownloadListPage', [
            'downloadItems' => $downloadItems,
        ]);
    }
}
