<?php

namespace App\Http\Controllers\Dashboard;
use App\Http\Controllers\Controller;

use App\Models\Gift;
use Illuminate\Http\Request;

class GiftController extends Controller
{
    /**
     * Display a listing of the resource.
     */
    public function index()
    {
        $gifts = Gift::latest()->paginate(25);
        return view('dashboard.gifts.index', compact('gifts'));
    }

    /**
     * Show the form for creating a new resource.
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     */
    public function store(Request $request)
    {
        $request->validate([
            'limit' => 'required|numeric|min:1',
            'amount' => 'required|numeric|min:1',
        ]);

        Gift::create([
            'limit' => $request->limit,
            'amount' => $request->amount,
            'remaining' => $request->limit,
            'gift_code' => mt_rand(1000000000, 9999999999)  // uniqid()
        ]);
        
        return to_route('gifts.index')->with('success', 'Gift Created successfully');
    }

    public function toggleStatus($id){
        $gift = Gift::findOrFail($id);
        $gift->status = !$gift->status;
        $gift->save();
        return to_route('gifts.index')->with('success', 'Gift Status Updated successfully');
    }

    /**
     * Display the specified resource.
     */
    public function show(Gift $gift)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     */
    public function edit(Gift $gift)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     */
    public function update(Request $request, Gift $gift)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     */
    public function destroy($id)
    {
        $gift = Gift::findOrFail($id);
        $gift->delete();

        return redirect()->route('gifts.index')->with('success', 'Gift deleted successfully.');
    }
}
