<?php

namespace App\Http\Controllers;

use App\Models\Prize;
use Illuminate\Http\Request;
use App\Http\Requests\StorePrizeRequest;
use App\Http\Requests\UpdatePrizeRequest;

class PrizeController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        return Prize::all();
    }

    public function index_by_activity(UpdatePrizeRequest $request)
    {
        return Prize::where('activity_id', $request->activityId)->get();
    }

    public function prize_is_drawable(Request $request)
    {
        $prize = Prize::where('id', $request->prizeId)->first();
        if ($prize->count >= $request->number) {
            return true;
        }
        return false;
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \App\Http\Requests\StorePrizeRequest  $request
     * @return \Illuminate\Http\Response
     */
    public function store(StorePrizeRequest $request)
    {
        Prize::insert([
            'activity_id' => $request->activity_id,
            'name' => $request->name,
            'provider' => $request->provider,
            'count' => $request->count,
            'created_at' => now(),
            'updated_at' => now(),
        ]);
        return true;
    }

    /**
     * Display the specified resource.
     *
     * @param  \App\Models\Prize  $prize
     * @return \Illuminate\Http\Response
     */
    public function show(Prize $prize)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  \App\Models\Prize  $prize
     * @return \Illuminate\Http\Response
     */
    public function edit(Prize $prize)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \App\Http\Requests\UpdatePrizeRequest  $request
     * @param  \App\Models\Prize  $prize
     * @return \Illuminate\Http\Response
     */
    public function update(UpdatePrizeRequest $request, Prize $prize)
    {
        //
    }

    public function draw(UpdatePrizeRequest $request, Prize $prize)
    {
        $prize = Prize::where('id', $request->prizeId)->first();
        if (!$prize) {
            // return 'prize not found';
            return false;
        }
        if ($prize->count > 0) {
            $prize->update([
                'count' => $prize->count - 1,
                'updated_at' => now(),
            ]);
            return true;
        }
        // return 'prize is empty';
        return false;
    }

    public function draw_patch(UpdatePrizeRequest $request, Prize $prize)
    {
        $prize = Prize::where('id', $request->prizeId)->first();
        if (!$prize) {
            // return 'prize not found';
            return false;
        }
        if ($prize->count >= $request->number) {
            $prize->update([
                'count' => $prize->count - $request->number,
                'updated_at' => now(),
            ]);
            return true;
        }
        // return 'prize is empty';
        return false;
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  \App\Models\Prize  $prize
     * @return \Illuminate\Http\Response
     */
    public function destroy(Prize $prize)
    {
        //
    }
}
