<?php

namespace App\Http\Controllers;

use App\Models\FormData;
use App\Models\Prize;
use App\Models\PrizeWeek;
use App\Models\Setting;
use App\Models\VerifiedData;
use App\Models\Winner;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;
use DB;

class SettingsController extends Controller
{

    #Index
    public function index(Request $request)
    {
        $weeks = PrizeWeek::get();
        $prizes = Prize::with('category')->get();
        return view('raffle_draw.settings')->with(['weeks' => $weeks, 'prizes' => $prizes]);
    }


    /**
     * Update Settings
     */
    public function update(Request $request)
    {
        try {
            $settings_id = $request->settings_id;
            $validator = Validator::make($request->all(), [
                'settings_id' => 'required|exists:settings,id',
                'speed' => 'required|numeric|max:1000',
                'spin_time' => 'required|numeric|max:1000',
                'prize_week_id' => 'required'
            ]);

            if ($validator->fails()) {
                $errors = $validator->errors();
                return $this->returnErrorResponse($errors->first());
            }
            DB::beginTransaction();
            $setting = Setting::find($settings_id);
            if ($setting) {

                if ($setting->prize_week_id != $request->prize_week_id) {

                    // Check all prizes are drawn or not
                    $prize_count = Prize::whereRaw("total_count > drawn_count")->count();
                    $user_count = VerifiedData::where('status', 1)->count();
                    if ($prize_count > 0 && $user_count > 0) {
                        DB::rollback();
                        return $this->returnErrorResponse('Cannot change week. Prizes are pending.');
                    }
                    // Delete all user data
                    $update = FormData::query()->truncate();
                    $update = Winner::where('prize_week_id', $setting->prize_week_id)->update(['verified_data_id' => null]);
                    DB::statement('SET FOREIGN_KEY_CHECKS=0;');
                    $update = VerifiedData::query()->truncate();
                    DB::statement('SET FOREIGN_KEY_CHECKS=1;');
                    $setting->prize_week_id = $request->prize_week_id;
                }

                $setting->speed = $request->speed;
                $setting->spin_time = $request->spin_time;
                $setting->save();
                DB::commit();
                return $this->returnResponse('Settings updated successfully');
            }
            DB::rollback();

            return $this->returnErrorResponse();
        } catch (\Exception $e) {
            DB::rollback();
            // return $this->returnErrorResponse($e->getMessage());
            return $this->returnErrorResponse();
        }
    }

    // Show settings
    public function show(Request $request)
    {
        try {
            // $validator = Validator::make($request->all(), [
            //     'settings_id'  => 'required',
            // ]);

            // if ($validator->fails()){
            //     $errors = $validator->errors();
            //     return $this->returnErrorResponse($errors->first());
            // }

            // $setting=Setting::find($request->settings_id);

            $setting = Setting::first();

            if ($setting) {
                return $this->returnResponse('Settings', $setting);
            }

            return $this->returnErrorResponse();
        } catch (\Exception $e) {
            // return $this->returnErrorResponse($e->getMessage());
            return $this->returnErrorResponse();
        }
    }
}
