<?php

namespace App\Http\Controllers\Medicine;

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

class UnitController extends Controller
{
    /**
     * Display a listing of the resource.
     */
    public function index(): JsonResponse
    {
        try {
            return successResponseWithData(Unit::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, [
            'unit_name' => 'required',
            //            'status' => 'required',
        ]);
        if ($validator->fails()) {
            return validationErrorResponse($validator);
        }

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

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

    public function change_status(string $id): JsonResponse
    {
        try {
            return successResponse(Unit::change_status($id), 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(Unit::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, [
            'unit_name' => 'required',
            //            'status' => 'required',
        ]);
        if ($validator->fails()) {
            return validationErrorResponse($validator);
        }

        try {
            return successResponseWithData(Unit::updateUnit($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 {
            //            Unit::query()->find($id)->delete();

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