<?php

namespace App\Http\Controllers;

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

class ReassignedImageController extends Controller
{
    public function __construct(ResponseRepository $response){
        $this->response = $response;
        $this->storeRules = [
            'template_check_list_id' => 'required',
            'hop_image_upload_id' => 'required',
            // 're_assigned_description' => 'required',
            'status_id' => 'required',


        ];
    }

    public function indexReassignedImage() {
        return $this->response->jsonResponse(false," Re Assigned Image Details Fetched Successfully", ReassignedImage::orderBy('re_assigned_image_id', 'desc')->get(), 300);
    }

    public function store(Request $request)
{
    $validate = $this->response->validate($request->all(), $this->storeRules);

    if ($validate === true) {
        if ($request->hasFile('re_assigned_images') && $request->has('template_check_list_id') && $request->has('re_assigned_description')) {
            $uploadUrl = $this->response->cloudinaryImage($request->file('re_assigned_images'), 'Reassigned Image', 're_assigned_images');

            $data = [
                're_assigned_images' => $uploadUrl,
                'template_check_list_id' => $request->input('template_check_list_id'),
                // 're_assigned_description' => $request->input('re_assigned_description'),
                'hop_image_upload_id' => $request->input('hop_image_upload_id'),
                'status_id' => $request->input('status_id')

            ];

            $hopImage = ReassignedImage::create($data);

            return $this->response->jsonResponse(false, $this->response->message(' Mark Up Saved', 'store'), $hopImage, 301);
        } else {
            return $this->response->jsonResponse(true, 'Image  not found', [], 203);
        }
    } else {
        return $validate;
    }
}


    public function show($id) {
        return $this->response->jsonResponse(false, $this->response->message('Re Assigned Image', 'show'), $this->findReassignedImage($id), 302);
    }

    public function updateReassignedImage(Request $request) {
        $validate = $this->response->validate($request->all(), [
            'template_check_list_id' => 'required',
            'hop_image_upload_id' => 'required',
            'description' => 'required',
        ]);
        if($validate === true) {
            return $this->response->jsonResponse(false, $this->response->message('Re Assigned Image', 'update'), $this->findReassignedImage($request->re_assigned_image_id)->update($request->all()), 303);
        } else {
            return $validate;
        }
    }

    public function destroy($id) {
        $size = $this->findReassignedImage($id);
        if($size) {
            return $this->response->jsonResponse(false, $this->response->message('Re Assigned Image', 'destroy'), $size->delete(), 304);
        }
        return $this->response->jsonResponse(true, 'Re Assigned Image Not Exists', [], 305);
    }

    public function findReassignedImage($id) {
        return ReassignedImage::find($id);
    }

    public function reassignedImageSwitch($id) {
        $size = $this->findReassignedImage($id);
        if($size) {
            $value = $size->active_status === 1 ? 0 : 1;
            $msg = $value === 0 ? 'Deactivated': 'Activated';
            return $this->response->jsonResponse(false, 'ReassignedImage '.$msg.' Successfully', $size->update(['active_status' => $value]), 306);
        }
        return $this->response->jsonResponse(true, 'ReassignedImage Not Exists', [], 307);
    }

    // public function searchAccounts($search){
    //     if($search === "null") {
    //         return $this->response->jsonResponse(false, $this->response->message('Accounts', 'search'), [], 910);
    //     }
    //     return $this->response->jsonResponse(false, $this->response->message('Accounts', 'search'), Accounts::where('account_name', 'LIKE', '%'.$search.'%')->get(), 911);
    // }

}
