<?php

namespace App\Http\Controllers\Medicine;

use App\Helpers\ValidationHelper;
use App\Http\Controllers\Controller;
use App\Models\LeafSetting;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Mockery\Exception;

class LeafSettingController extends Controller
{
    /**
     * Display a listing of the resource.
     */
    public function index(): JsonResponse
    {
        try {
            return successResponseWithData(LeafSetting::query()->where('user_id', '=', Auth::id())->orderBy('id', 'DESC')->paginate(1000), 'success', 200);
        } catch (Exception $exception) {
            return errorResponse($exception, 500);
        }
    }

    /**
     * Show the form for creating a new resource.
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     */
    public function store(Request $request): JsonResponse
    {
        $validator = ValidationHelper::validator($request, [
            'leaf_type' => 'required',
            'total_number' => 'required',
        ]);
        if ($validator->fails()) {
            return validationErrorResponse($validator);
        }

        try {
            return successResponseWithData(LeafSetting::newLeaf($request), 'Create successfully', 201);
        } catch (Exception $exception) {
            return errorResponse($exception, 500);
        }
    }

    /**
     * Display the specified resource.
     */
    public function show(string $id): JsonResponse
    {
        try {
            return successResponseWithData(LeafSetting::query()->find($id), 'success', 200);
        } catch (Exception $exception) {
            return errorResponse($exception, 500);
        }
    }

    /**
     * Show the form for editing the specified resource.
     */
    public function edit(string $id): JsonResponse
    {
        try {
            return successResponseWithData(LeafSetting::query()->find($id), 'success', 200);
        } catch (Exception $exception) {
            return errorResponse($exception, 500);
        }
    }

    /**
     * Update the specified resource in storage.
     */
    public function update(Request $request, string $id): JsonResponse
    {
        $validator = ValidationHelper::validator($request, [
            'leaf_type' => 'required',
            'total_number' => 'required',
        ]);
        if ($validator->fails()) {
            return validationErrorResponse($validator);
        }

        try {
            return successResponseWithData(LeafSetting::updateLeaf($request, $id), 'Update successfully', 200);
        } catch (Exception $exception) {
            return errorResponse($exception, 500);
        }
    }

    /**
     * Remove the specified resource from storage.
     */
    public function destroy(string $id): JsonResponse
    {
        try {
            //            LeafSetting::query()->find($id)->delete();

            return successResponse('Delete success', 200);
        } catch (Exception $exception) {
            return errorResponse($exception, 500);
        }
    }
}
