<?php

namespace App\Http\Controllers\Admin\Api;

use App\Classes\Constant;
use App\Http\Controllers\Controller;
use App\Models\Prescription;
use Illuminate\Contracts\Routing\UrlGenerator;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;
use Illuminate\Auth\Access\AuthorizationException;
class PrescriptionController extends Controller
{
    protected $url;
    protected $user;
    public function __construct(UrlGenerator $url)
    {
        $this->url = $url;
        $this->user = Auth::guard('admin')->user();
    }
    public function index(){}
    public function create(Request $request){
        try{
            $this->authorizeForUser($this->user,'add', new Prescription);
            $data = [
                'name'=>$request->input('name'),
                'brand'=>$request->input('brand'),
                'from'=>$request->input('from'),
                'dosage'=>$request->input('dosage'),
                'quantity'=>$request->input('quantity'),
                'direction_quantity'=>$request->input('direction_quantity'),
                'refill_quantity'=>$request->input('refill_quantity')
            ];
            Prescription::create($data);
            return response()
                ->json([
                    'success' => "Data Saved !"
                ], 200);
        } catch (AuthorizationException $e) {
            return  $e->getMessage();
        }
    }
    public function update($id,Request $request){
        try{
            $this->authorizeForUser($this->user,'edit', new Prescription);
            $prescription = $this->details($id);
            $prescription->name =$request->input('name');
            $prescription->brand =$request->input('brand');
            $prescription->from = $request->input('from');
            $prescription->dosage = $request->input('dosage');
            $prescription->quantity = $request->input('quantity');
            $prescription->direction_quantity = $request->input('direction_quantity');
            $prescription->refill_quantity = $request->input('refill_quantity');
            $prescription->save();
            return response()
                ->json([
                    'success' => "Data Updated !"
                ], 200);
        } catch (AuthorizationException $e) {
            return  $e->getMessage();
        }
    }
    public function details($id){
        try{
            // $this->authorizeForUser($this->user,'view', new Prescription);
            return Prescription::find($id);
        } catch (AuthorizationException $e) {
            return  $e->getMessage();
        }
    }
    public function edit($id)
    {
        try{
            $this->authorizeForUser($this->user,'edit', new Prescription);
            return response()
            ->json([
                'data' => $this->details($id)
            ], 200);
        } catch (AuthorizationException $e) {
            return  $e->getMessage();
        }
    }
    public function delete($id)
    {
        try{
            $this->authorizeForUser($this->user,'delete', new Prescription);
            Prescription::find($id)->delete();
            return response()
                ->json([
                    'success' => "Entry Deleted !"
                ], 200);
        } catch (AuthorizationException $e) {
            return  $e->getMessage();
        }
    }
}
