<?php

namespace App\Http\Controllers;

use App\Models\Recipients;
use App\Models\Prize;
use App\Models\CheckIn;
use App\Models\User;
use App\Models\Department;
use App\Http\Requests\StoreRecipientsRequest;
use App\Http\Requests\UpdateRecipientsRequest;
use Illuminate\Http\Request;

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

    public function index_by_activity(UpdateRecipientsRequest $request)
    {
        return Recipients::join('prizes', 'recipients.prize_id', '=', 'prizes.id')
            ->select('user_id', 'prizes.activity_id', 'prize_id', 'recipients.created_at')
            ->where('prizes.activity_id', $request->activityId)
            ->orderBy('recipients.created_at', 'DESC')
            ->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\StoreRecipientsRequest  $request
     * @return \Illuminate\Http\Response
     */
    public function store(StoreRecipientsRequest $request)
    {
        Recipients::insert([
            'prize_id' => $request->prizeId,
            'user_id' => $request->userId,
            'is_claimed' => false,
            'created_at' => now(),
            'updated_at' => now(),
        ]);
        return 'success';
    }

    public function store_patch(StoreRecipientsRequest $request)
    {
        $data = [];
        foreach ($request->userIdList as $userId) {
            array_push($data, [
                'prize_id' => $request->prizeId,
                'user_id' => $userId,
                'is_claimed' => false,
                'created_at' => now(),
                'updated_at' => now(),
            ]);
        }
        Recipients::insert($data);
        return 'success';
    }

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

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

    /**
     * Update the specified resource in storage.
     *
     * @param  \App\Http\Requests\UpdateRecipientsRequest  $request
     * @param  \App\Models\Recipients  $recipients
     * @return \Illuminate\Http\Response
     */
    public function update(UpdateRecipientsRequest $request, Recipients $recipients)
    {
        Recipients::where('user_id', $request->userId)->update(['prize_id' => $request->prizeId]);
        return 'success';
    }

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

    public function claim(Request $request)
    {
        $prize = '';
        $prize_id = 0;
        $isClaimed = false;
        $user_id = $request->user_id;
        $activity_id = $request->activity_id;
        $user = CheckIn::where('activity_id', '=', $activity_id)
            ->where('user_id', '=', $user_id)->get()[0];
        $department_id = CheckIn::where('user_id', '=', $user_id)->get()[0]->department_id;
        if($department_id != '空'){
            $department_name = Department::where('department_id', '=', $department_id)->get()[0]->department_name;
        }else{
            $department_name = '空';
        }

        if (Recipients::where('user_id', '=', $user_id)->exists()) {
            $recipients = Recipients::where('user_id', '=', $user_id)->get()[0];
            $prize_id = $recipients->prize_id;
            //因為重新匯入後prize_id在prize下面有改動，但是在recipients下面沒有改動，因此會有問題產生
            //檢查prize_id是不是在activity_id底下
            $aid = Prize::where('id', '=', $prize_id)->get()[0]->activity_id;
            if ($aid == $activity_id) {
                $isRecipients = true;
                $isClaimed = $recipients->is_claimed;
                $prize = Prize::where('id', '=', $prize_id)->get()[0]->name;
            } else {
                $isRecipients = false;
            }
        } else {
            $isRecipients = false;
        }

        $response = [
            'user' => $user,
            'isRecipients' => $isRecipients,
            'isClaimed' => $isClaimed,
            'prize' => $prize,
            'prize_id' => $prize_id,
            'department_name' => $department_name,
        ];

        return response($response, 201);
    }

    public function isClaimed(UpdateRecipientsRequest $request)
    {
        $recipient = Recipients::where('prize_id', '=', $request->prize_id)
            ->where('user_id', '=', $request->user_id)->get()[0];
        $recipient->is_claimed = true;
        $recipient->save();
        return $recipient;
    }
}
