<?php

namespace App\Http\Controllers\Medicine;

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

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

    public function medicine_companies(): JsonResponse
    {
        try {
            return successResponseWithData([
                'medicine_companies' => MedicineCompany::query()->where(['user_id' => Auth::id(), 'status' => '1'])->orderBy('id', 'DESC')->get(['id', 'company_name']),
            ], 'success', 200);
        } catch (Exception $exception) {
            return errorResponse($exception, 500);
        }
    }

    /**
     * Store a newly created resource in storage.
     */
    public function store(Request $request): JsonResponse
    {
        $validator = ValidationHelper::validator($request, [
            'medicine_company_id' => 'required|integer|exists:medicine_companies,id',
            'supplier_name' => 'required',
            'supplier_email' => 'required|email',
        ]);
        if ($validator->fails()) {
            return validationErrorResponse($validator);
        }

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

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

    public function change_status(string $id): JsonResponse
    {
        try {
            return successResponse(Supplier::change_status($id), 200);
        } catch (Exception $exception) {
            return errorResponse($exception, 500);
        }
    }

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

    /**
     * Update the specified resource in storage.
     */
    public function update(Request $request, string|int $id): JsonResponse
    {
        $validator = ValidationHelper::validator($request, [
            'medicine_company_id' => 'required|integer|exists:medicine_companies,id',
            'supplier_name' => 'required',
            'supplier_email' => 'required|email',
        ]);
        if ($validator->fails()) {
            return validationErrorResponse($validator);
        }

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

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

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