<?php

namespace App\Http\Controllers;

use App\Events\SetPrizeEvent;
use App\Events\StoreDrawEvent;
use Illuminate\Http\Request;

use App\Models\CheckIn;
use App\Models\Prize;
use App\Models\Recipients;
use App\Models\Activity;

class DrawController extends Controller
{
    public function draw_user_by_region(Request $request)
    {
        return CheckIn::where('is_checked_in', 'true')
            ->whereTime('check_ins.updated_at', '<=', $request->deadline)
            ->whereIn('region', $request->region)
            ->get()
            ->random($request->number);
    }

    public function draw(Request $request)
    {
        $prize = Prize::where('id', $request->prize_id)->first();
        if ($prize->count > 0) {
            $activity = Activity::where('id', $request->activity_id)->first();
            $users = CheckIn::where('activity_id', $request->activity_id)
                ->where('is_checked_in', 'true')
                ->where('is_awarded', 'false')
                // ->whereTime('check_ins.updated_at', '<=', $activity->deadline)
                ->whereIn('region', $request->region)
                ->join('departments', 'check_ins.department_id', '=', 'departments.department_id')
                ->inRandomOrder()
                ->get();
            $draw_count = $request->draw_one ? 1 : $prize->count;

            if ($draw_count <= count($users)) {
                $output = [];
                $draw_user = $users->random($draw_count);
                foreach ($draw_user as $user) {
                    array_push($output, [
                        'user_id' => $user->user_id,
                        'user_name' => $user->name,
                        'department_name' => $user->alias,
                    ]);
                }
                return $output;
            } else {
                return "user not enough";
            }
        }
        return "prize not enough";
    }

    public function store(Request $request)
    {
        $prize = Prize::where('id', $request->prize_id)->first();
        if ($prize->count >= count($request->users)) {
            $prize->update([
                'count' => $prize->count - count($request->users),
                'updated_at' => now(),
            ]);

            CheckIn::whereIn('user_id', $request->users)->update(['is_awarded' => true]);
            $recipientsData = [];
            foreach ($request->users as $user_id) {
                array_push($recipientsData, [
                    'prize_id' => $request->prize_id,
                    'user_id' => $user_id,
                    'is_claimed' => false,
                    'created_at' => now(),
                    'updated_at' => now(),
                ]);
            }
            Recipients::insert($recipientsData);
            event(new StoreDrawEvent("store"));
            return true;
        }
        return false;
    }

    public function donate(Request $request)
    {
        CheckIn::whereIn('user_id', $request->users)->update(['is_awarded' => true]);
        return 'success';
    }

    public function send(Request $request)
    {
        event(new SetPrizeEvent($request->message));
        //$foo = new TemplateEvent;
        return $request->message;
    }



    public function test(Request $request)
    {
        return 'success';
    }

    public function search_user_status(Request $request)
    {
        return CheckIn::where('check_ins.activity_id', $request->activity_id)
            ->leftjoin('recipients', 'check_ins.user_id', '=', 'recipients.user_id')
            ->leftjoin('prizes', 'recipients.prize_id', '=', 'prizes.id')
            ->select(
                'check_ins.user_id',
                'check_ins.name as user_name',
                'check_ins.department_id',
                'prizes.name as prize_name',
                'check_ins.is_checked_in',
                'check_ins.is_awarded',
                'recipients.is_claimed'
            )
            ->where('check_ins.user_id', $request->user_id)
            ->first();
    }

    public function draw_test(Request $request)
    {

        $output = [];
        $draw_count = $request->number;
        //去掉判斷只保留邏輯
        for ($i = 0; $i < $draw_count; $i++) {
            $users = CheckIn::where('activity_id', $request->activity_id)
                // ->where('is_checked_in', 'true')
                // ->where('is_awarded', 'false')
                ->whereIn('region', $request->region)
                ->join('departments', 'check_ins.department_id', '=', 'departments.department_id')
                ->inRandomOrder()
                ->get();

            $draw_user = $users->random(1);
            array_push($output, $draw_user[0]->user_id);
        }
        return $output;
    }
}
