<?php

namespace App\Http\Controllers;

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

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

    public function index() {
        return $this->response->jsonResponse(false, "Style Details Fetched Successfully", Style::orderBy('style_name', 'asc')->get(), 300);
    }

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

    public function show($style_id) {
        return $this->response->jsonResponse(false, $this->response->message('Style Details', 'show'), $this->findStyle($style_id), 303);
    }

    // public function update(Request $request, $style_id) {
    //     $checkStyle = Style::where('style_name', $request->style_name)->exists();
    //     if(!$checkStyle){
    //         $validate = $this->response->validate($request->all(), ['style_name' => ['required' ]]);
    //         if($validate === true) {
    //             return $this->response->jsonResponse(false, $this->response->message('Style Details', 'update'), $this->findStyle($style_id)->update($request->all()), 304);
    //         } else {
    //             return $validate;
    //         }
    //     }
    //     return $this->response->jsonResponse(true, "This ".$request->style_name. " Style Already Created", [], 305);
    // }
    public function updateStyle(Request $request) {
        $validate = $this->response->validate($request->all(), [
            'style_name' => ['required'],
        ]);
        if($validate === true) {
            return $this->response->jsonResponse(false, $this->response->message('Style Details', 'update'), $this->findStyle($request->style_id)->update($request->all()), 303);
        } else {
            return $validate;
        }
    }

    public function destroy($style_id) {
        $size = $this->findStyle($style_id);
        if($size) {
            return $this->response->jsonResponse(false, $this->response->message('Style Details', 'destroy'), $size->delete(), 306);
        }
        return $this->response->jsonResponse(true, 'Style Details Not Exists', [], 307);
    }

    public function findStyle($style_id) {
        return Style::find($style_id);
    }

    public function getActiveStyle() {
        return $this->response->jsonResponse(false, "Style Details Fetched Successfully", Style::where('active_status', 1)->orderBy('style_name', 'asc')->get(), 308);
    }

    public function searchStyle($search){
        if($search === "null") {
            return $this->response->jsonResponse(false, "Style Not Found", [], 309);
        }
        return $this->response->jsonResponse(false, $this->response->message('Style', 'search'), Style::where('style_name', 'LIKE', '%'.$search.'%')->get(), 310);
    }

    public function styleSwitch($style_id) {
        $size = $this->findStyle($style_id);
        if($size) {
            $value = $size->active_status === 1 ? 0 : 1;
            $msg = $value === 0 ? 'Deactivated': 'Activated';
            return $this->response->jsonResponse(false, 'Style Details '.$msg.' SuccessFully', $size->update(['active_status' => $value]), 311);
        }
        return $this->response->jsonResponse(true, 'Style Details Not Exists', [], 312);
    }
}
