<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\Size;
use App\Repositories\ResponseRepository;
use Illuminate\Support\Facades\Log;

class SizeController extends Controller
{
    public function __construct(ResponseRepository $response) {
        $this->response = $response;
        $this->storeRules = ['size_name' => 'required'];
    }

    public function index() {
        return $this->response->jsonResponse(false, "Size Details Fetched Successfully", Size::orderByRaw("CAST(size_name as UNSIGNED) ASC")->get(), 400);
    }

    public function store(Request $request) {
        $checkSize = Size::where('size_name', $request->size_name)->exists();
        if(!$checkSize){
            $validate = $this->response->validate($request->all(), $this->storeRules);
            if($validate === true) {
                return $this->response->jsonResponse(false, "Size Created Successfully", Size::create($request->all()), 401);
            } else {
                return $validate;
            }
        }
        return $this->response->jsonResponse(true, "This ".$request->size_name. " Size Already Created", [], 402);
    }

    public function show($size_id) {
        return $this->response->jsonResponse(false, $this->response->message('Size Details', 'show'), $this->findSize($size_id), 403);
    }

    // public function update(Request $request, $size_id) {
    //     $checkSize = Size::where('size_name', $request->size_name)->exists();
    //     if(!$checkSize){
    //         $validate = $this->response->validate($request->all(), ['size_name' => ['required' ]]);
    //         if($validate === true) {
    //             return $this->response->jsonResponse(false, $this->response->message('Size Details', 'update'), $this->findSize($size_id)->update($request->all()), 404);
    //         } else {
    //             return $validate;
    //         }
    //     }
    //     return $this->response->jsonResponse(true, "This ".$request->size_name. " Size Already Created", [], 405);
    // }


    public function updateSize(Request $request) {
        $validate = $this->response->validate($request->all(), [
            'size_name' => ['required'],
        ]);
        if($validate === true) {
            return $this->response->jsonResponse(false, $this->response->message('Size Details', 'update'), $this->findSize($request->size_id)->update($request->all()), 303);
        } else {
            return $validate;
        }
    }

    public function destroy($size_id) {
        $size = $this->findSize($size_id);
        if($size) {
            return $this->response->jsonResponse(false, $this->response->message('Size Details', 'destroy'), $size->delete(), 406);
        }
        return $this->response->jsonResponse(true, 'Size Details Not Exists', [], 407);
    }

    public function findSize($size_id) {
        return Size::find($size_id);
    }

    public function getActiveSize() {
        return $this->response->jsonResponse(false, "Size Details Fetched Successfully", Size::where('active_status', 1)->orderByRaw("CAST(size_name as UNSIGNED) ASC")->get(), 408);
    }

    public function searchSize($search){
        if($search === "null") {
            return $this->response->jsonResponse(false, "Size Not Found", [], 409);
        }
        return $this->response->jsonResponse(false, $this->response->message('Size', 'search'), Size::where('size_name', 'LIKE', '%'.$search.'%')->get(), 410);
    }

    public function sizeSwitch($size_id) {
        $size = $this->findSize($size_id);
        if($size) {
            $value = $size->active_status === 1 ? 0 : 1;
            $msg = $value === 0 ? 'Deactivated': 'Activated';
            return $this->response->jsonResponse(false, 'Size Details '.$msg.' SuccessFully', $size->update(['active_status' => $value]), 411);
        }
        return $this->response->jsonResponse(true, 'Size Details Not Exists', [], 412);
    }
}

