<?php

namespace App\Http\Controllers;

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

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(Request $request)
    {
        return Prize::where('activity_id', $request->activity_id)->get();
    }

    /**
     * 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)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  \App\Models\Prize  $prize
     * @return \Illuminate\Http\Response
     */
    public function destroy(Prize $prize)
    {
        //
    }
    
    public function count_by_prize(Request $request,Prize $prize){
        $prize =  Prize::where('id',$request->prize_id)->first();
        $prize_count = $prize->count;
        $recipient_count = Recipients::where("prize_id",$request->prize_id)->count();
        $response = [
            'drawnCount' => $recipient_count,
            'notDrawnCount' => $prize_count
        ];

        return response($response, 201);
    }

}
