<?php

namespace App\Http\Controllers;

use App\Models\Prize;
use App\Models\PrizeCategory;
use App\Models\PrizeWeek;
use App\Models\Setting;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;
use Str;

class PrizeController extends Controller
{
    //
    #Index
    public function index(Request $request)
    {
        $categories=PrizeCategory::get();
        $prizes=Prize::with('category')->get();
        return view('raffle_draw.prize_data')->with(['categories'=>$categories,'prizes'=>$prizes]);
    }

    /**
     * Add prize details
    */
    public function store(Request $request) 
    {
        try{

            $validator = Validator::make($request->all(), [
                'prize'  => 'required|unique:prizes,prize',
                'prize_category_id'=>'required|exists:prize_categories,id',
                'total_count'=>'required|numeric',
                'url_slug'=>'required|unique:prizes'
            ]);
    
            if ($validator->fails()){
                $errors = $validator->errors();
                return $this->returnErrorResponse($errors->first());
            }

            $prize=new Prize();
            $prize->total_count=$request->total_count;
            $prize->prize_category_id=$request->prize_category_id;
            $prize->prize=$request->prize;
            $prize->url_slug=Str::slug($request->url_slug);
            $prize->save();

            if($prize){
                return $this->returnResponse('Prize Added Successfully');
            }

            return $this->returnErrorResponse();

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

    /**
     * Update prize details
    */
    public function update(Request $request) 
    {
        try{
            $prize_id=$request->prize_id;
            $validator = Validator::make($request->all(), [
                'prize_id' => 'required|exists:prizes,id',
                'prize'  => 'required|unique:prizes,prize,'.$prize_id.',id',
                'prize_category_id'=>'required|exists:prize_categories,id',
                'total_count'=>'required|numeric',
                'url_slug'=>'required|unique:prizes,url_slug,'.$prize_id.',id'
            ]);
    
            if ($validator->fails()){
                $errors = $validator->errors();
                return $this->returnErrorResponse($errors->first());
            }

            $prize=Prize::where('status',1)->find($prize_id);
            if(!$prize)
                return $this->returnErrorResponse("Prize cannot update");

            $prize->total_count=$request->total_count;
            $prize->prize_category_id=$request->prize_category_id;
            $prize->prize=$request->prize;
            $prize->url_slug=Str::slug($request->url_slug);
            $prize->save();

            if($prize){
                return $this->returnResponse('Prize updated successfully');
            }

            return $this->returnErrorResponse();

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

    // View Prize details
    public function show(Request $request) 
    {
        try{
            $validator = Validator::make($request->all(), [
                'prize_id'  => 'required',
            ]);
    
            if ($validator->fails()){
                $errors = $validator->errors();
                return $this->returnErrorResponse($errors->first());
            }

            $prize=Prize::find($request->prize_id);

            if($prize){
                return $this->returnResponse('Prize details',$prize);
            }

            return $this->returnErrorResponse();

        }catch(\Exception $e){
            // return $this->returnErrorResponse($e->getMessage());
            return $this->returnErrorResponse();
        }
    }
    
    // Delete Prize details
    public function delete(Request $request) 
    {
        try{
            $validator = Validator::make($request->all(), [
                'prize_id'  => 'required',
            ]);
    
            if ($validator->fails()){
                $errors = $validator->errors();
                return $this->returnErrorResponse($errors->first());
            }

            $prize=Prize::where('status',1)->find($request->prize_id);
            if(!$prize)
                return $this->returnErrorResponse("Prize cannot delete");

            if($prize->delete()){
                return $this->returnResponse('Prize Deleted Successfully');
            }

            return $this->returnErrorResponse();

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


    /**
     * Add Category details
    */
    public function storeCategory(Request $request) 
    {
        try{

            $validator = Validator::make($request->all(), [
                'name'  => 'required|unique:prize_categories,name',
            ]);
    
            if ($validator->fails()){
                $errors = $validator->errors();
                return $this->returnErrorResponse($errors->first());
            }

            $prize=new PrizeCategory();
            $prize->name=$request->name;
            $prize->save();

            if($prize){
                return $this->returnResponse('Category Added Successfully');
            }

            return $this->returnErrorResponse();

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

    /**
     * Update Category details
    */
    public function updateCategory(Request $request) 
    {
        try{
            $category_id=$request->category_id;
            $validator = Validator::make($request->all(), [
                'category_id' => 'required|exists:prize_categories,id',
                'name'  => 'required|unique:prize_categories,name,'.$category_id.',id',
            ]);
    
            if ($validator->fails()){
                $errors = $validator->errors();
                return $this->returnErrorResponse($errors->first());
            }

            $prize=PrizeCategory::where('status',1)->find($category_id);
            if(!$prize)
                return $this->returnErrorResponse("Category cannot update");

            $prize->name=$request->name;
            $prize->save();

            if($prize){
                return $this->returnResponse('Category updated successfully');
            }

            return $this->returnErrorResponse();

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

    // View Category details
    public function showCategory(Request $request) 
    {
        try{
            $validator = Validator::make($request->all(), [
                'category_id'  => 'required',
            ]);
    
            if ($validator->fails()){
                $errors = $validator->errors();
                return $this->returnErrorResponse($errors->first());
            }

            $category=PrizeCategory::find($request->category_id);

            if($category){
                return $this->returnResponse('Category details',$category);
            }

            return $this->returnErrorResponse();

        }catch(\Exception $e){
            // return $this->returnErrorResponse($e->getMessage());
            return $this->returnErrorResponse();
        }
    }
    
    // Delete Category details
    public function deleteCategory(Request $request) 
    {
        try{
            $validator = Validator::make($request->all(), [
                'category_id'  => 'required',
            ]);
    
            if ($validator->fails()){
                $errors = $validator->errors();
                return $this->returnErrorResponse($errors->first());
            }

            $category=PrizeCategory::where('status',1)->find($request->category_id);
            if(!$category)
                return $this->returnErrorResponse("Category cannot delete");

            if($category->delete()){
                return $this->returnResponse('Category Deleted Successfully');
            }

            return $this->returnErrorResponse();

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


    /**
     * Add Week details
    */
    public function storeWeek(Request $request) 
    {
        try{

            $validator = Validator::make($request->all(), [
                'prize_week'  => 'required|unique:prize_weeks,prize_week',
                'prize_date'  => 'required|date',
            ]);
    
            if ($validator->fails()){
                $errors = $validator->errors();
                return $this->returnErrorResponse($errors->first());
            }

            $prize=new PrizeWeek();
            $prize->prize_week=$request->prize_week;
            $prize->prize_date=$request->prize_date;
            $prize->save();

            if($prize){
                return $this->returnResponse('Week Added Successfully');
            }

            return $this->returnErrorResponse();

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

    /**
     * Update Week details
    */
    public function updateWeek(Request $request) 
    {
        try{
            $week_id=$request->week_id;
            $validator = Validator::make($request->all(), [
                'week_id' => 'required|exists:prize_weeks,id',
                'prize_week'  => 'required|unique:prize_weeks,prize_week,'.$week_id.',id',
                'prize_date'  => 'required|date',
            ]);
    
            if ($validator->fails()){
                $errors = $validator->errors();
                return $this->returnErrorResponse($errors->first());
            }

            $prize=PrizeWeek::where('status',1)->find($week_id);
            if(!$prize)
                return $this->returnErrorResponse("Week cannot update");

            $prize->prize_week=$request->prize_week;
            $prize->prize_date=$request->prize_date;
            $prize->save();

            if($prize){
                return $this->returnResponse('Week updated successfully');
            }

            return $this->returnErrorResponse();

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

    // View Week details
    public function showWeek(Request $request) 
    {
        try{
            $validator = Validator::make($request->all(), [
                'week_id'  => 'required',
            ]);
    
            if ($validator->fails()){
                $errors = $validator->errors();
                return $this->returnErrorResponse($errors->first());
            }

            $category=PrizeWeek::find($request->week_id);

            if($category){
                return $this->returnResponse('Week details',$category);
            }

            return $this->returnErrorResponse();

        }catch(\Exception $e){
            // return $this->returnErrorResponse($e->getMessage());
            return $this->returnErrorResponse();
        }
    }
    
    // Delete Week details
    public function deleteWeek(Request $request) 
    {
        try{
            $validator = Validator::make($request->all(), [
                'week_id'  => 'required',
            ]);
    
            if ($validator->fails()){
                $errors = $validator->errors();
                return $this->returnErrorResponse($errors->first());
            }

            $prize_week=PrizeWeek::where('status',1)->find($request->week_id);
            if(!$prize_week && $prize_week->active_status==1)
                return $this->returnErrorResponse("Week cannot delete");

            if($prize_week->delete()){
                return $this->returnResponse('Week Deleted Successfully');
            }

            return $this->returnErrorResponse();

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

}
