<?php

namespace App\Http\Controllers\Api\Workshop;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Models\Workshop\WorkshopMaterialRequestion;
use App\Models\Workshop\WorkshopMaterialRequestionItem;
use App\Models\MaterialRequestionApprove;
use App\Service\Workshop\WorkshopMaterialRequestionService;
use App\Service\Workshop\WorkshopTransactionStore;  
use App\Service\SequenceService;
use App\Service\StockEntryService;
use App\Service\StaticMaster;
use App\Models\Item;
use Auth;
use Illuminate\Support\Facades\Validator;

class WorkshopMaterialRequestionController extends Controller
{
    //Create Update Material Request by Workshop
    public function createWorkshopRequestion(Request $request)
    {
        $validator=validator($request->all(),[
            'requestion_id'=>'required',
            'date'=>'required'
        ]);

        if ($validator->fails()) { 

            return [
                'success' => false, 
                'message' => $validator->errors()->first()
            ];

        } else {

            try {

                $obj['requestion_id'] = $request['requestion_id'];
                $obj['date'] = $request['date']??null;
                $obj['job_card_id'] = $request['job_card_id']??0;
                $obj['user_id'] = $request['user_id']??0;
                $obj['remarks'] = $request['remarks'];
                $obj['attachments'] = $request['attachments'];
                $obj['status'] = 'Pending';

                // Update MaterialRequestion with MaterialRequestion id
                if ($request->has('id') && $request['id'] > 0) {

                    $requisition = WorkshopMaterialRequestion::where('id', $request['id'])->first();
                    $requisition->update($obj);

                    WorkshopMaterialRequestionItem::where('requestion_id',$request['id'])->delete();

                    $requisitionItem=new WorkshopMaterialRequestionService;
                    $requisitionItem->materialRequestionItem($request,$requisition);

                    return response()->json(['success' => true, 'message' => 'MaterialRequestion updated successfully', 'requisition' => $requisition], 200);

                } else {
                    // Creating new MaterialRequestion
                    $requisition = WorkshopMaterialRequestion::create($obj);
                    
                    $requisitionItem=new WorkshopMaterialRequestionService;
                    $requisitionItem->materialRequestionItem($request,$requisition);

                    $module='workshop_material_request';

                    // Update Sequence
                    $sequence=new SequenceService;
                    $sequence->updateSequence($module);

                    return response()->json(['success' => true, 'message' => 'Workshop MaterialRequestion  created successfully', 'requisition' => $requisition], 200);
                }
            } catch (\Exception $e) {
                return response()->json(['success' => false, 'message' => 'Failed to create Workshop MaterialRequestion ', 'error' => $e->getMessage()], 500);
            }
            
        }
    }

    //List of Requestion
    public function workshopRequestionList(Request $request){
        try{

            $requisition=WorkshopMaterialRequestion::with('entry_by:id,display_name','job_card','user:id,display_name')
                ->where('rolledback',0);

            if($request['search']!=null && $request['search']!=''){

                $requisition = $requisition->where(function ($q) use ($request) {
                    $q->where('requestion_id', 'LIKE', '%' . $request['search'] . '%');
                });
            }

                // get custom_date 
            if ($request['custom_date']!='' && isset($request['custom_date'])) {
                $customDate = $request->custom_date;
                $requisition = $requisition->whereDate('date', $customDate);
            }

            if ($request['status']!='' && isset($request['status'])) {
                $status = $request->status;
                $requisition = $requisition->where('status', $status);
            }
    
            if ($request['from_date']!='' && isset($request['to_date'])) {
    
                $fromDate = $request->from_date;
    
                $toDate = $request->to_date;
    
                // 'fromDate' and 'toDate'
                $requisition = $requisition->whereBetween('date', [$fromDate, $toDate]);
    
            }

            // Count all the items
            $requisition_count=$requisition->count();

            if(isset($request['sort_by']) && $request['sort_by']!='' && $request['sort_by']!=null){
                
                $sort_order=$request['sort_order']==1?'asc':'desc';
    
                $requisition = $requisition->orderby($request['sort_by'],$sort_order);
    
            }else{
                $requisition = $requisition->orderby('id','desc');
            }

            // Pagination
            $requisition=$requisition->skip($request['noofrec']*($request['currentpage']-1))->take($request['noofrec']??100)
                // ->orderBy('id','desc')
                ->get();

        }catch (\Illuminate\Database\Eloquent\ModelNotFoundException $e) {
            return [
                'success' => false,
                'message' => 'Workshop MaterialRequestion not found'
            ];
        }
            
        $response=[
            'success'=>true,
            'count'=>$requisition_count,
            'requisition'=>$requisition,
            'message'=>'Workshop Item Requestion List Fetch Successfully'
        ];

        return response()->json($response, 200);
    }

    //Requestion Details
    public function workshopRequisitionDetails(Request $request)
    {
        $validator=validator($request->all(),[
            'requestion_id'=>'required'
        ]);

        if ($validator->fails()) { 

            return [
                'success' => false, 
                'message' => $validator->errors()->first()
            ];

        } else {

            $requestion_id = $request->requestion_id;

            $requisition =  WorkshopMaterialRequestion::where('id', $requestion_id)
                            ->with('requisition_items','job_card','user')
                            ->first();

            $response = [
                'message'=>'Workshop Requisition Details Listed Successfully',
                'success'=>true,
                'data' => $requisition,
            ];

            return response()->json($response, 200);
        }
    }

    // Requestion Delete
    public function workshop_requisition_delete(Request $request){

        $user=Auth::user();
        // Validate Item
        $validator=validator($request->all(),[
           'requestion_id'=>'required|integer'
       ]);

       if ($validator->fails()) { 
           return [
               'success' => false, 
               'message' => $validator->errors()->first(),
           ];

       } else {

           try{
               $requisition = WorkshopMaterialRequestion::where('id',$request->input('requestion_id'))->first();

                if($requisition['status']=='Pending'){
                    $requisition['rolledback']=1;
                    $requisition->update();
                    
                    $requisitionItem = WorkshopMaterialRequestionItem::where('requestion_id',$request->input('requestion_id'))->update('rolledback',1);

                    $response=[
                        'success'=>true,
                        'message'=>'Workshop Product Requestion Deleted successfully.'
                    ];
               }else{
                    $response=[
                        'success'=>false,
                        'message'=>'You can not Delete Product MaterialRequestion in Partially and Completed Status.'
                    ];
               }
               
           } catch (\Illuminate\Database\Eloquent\ModelNotFoundException $e) {
               // Return error message in JSON if ID not found
               return response()->json(['success'=>false,'message' => 'Invalid Id found'], 404);
           }

           return response()->json($response);

       }
    }

    // Get All Items Requestion Lists
     // Display all the list of items to be approved from accounts
    public function requestion_approval_list(Request $request){
     
        $material_request=WorkshopMaterialRequestionItem::where('rolledback',0)
            ->with('item','material_request');

        if($request['search']!=null && $request['search']!=''){

            $material_request = $material_request->where(function ($q) use ($request) {
                $q->where('requestion_id', 'LIKE', '%' . $request['search'] . '%')
                // ->orWhere('warehouse_name', 'LIKE', '%' . $request['search'] . '%')
                ->orWhere('item_name', 'LIKE', '%' . $request['search'] . '%');
            });
        }
        if($request['status']!=null && $request['status']!=''){
            $material_request=$material_request->where('status',$request['status']);
        }

        if ($request['custom_date']!='' && isset($request['custom_date'])) {

            $customDate = $request->custom_date;

            // $material_request = $material_request->whereDate('date', $customDate);
            $material_request= $material_request->whereHas('material_request',function($query) use ($customDate){
                $query->whereDate('date', $customDate);
            });

        }

        if ($request['from_date']!='' && isset($request['to_date'])) {

            $fromDate = $request->from_date;

            $toDate = $request->to_date;

            // 'fromDate' and 'toDate'
            // $material_request = $material_request->whereBetween('date', [$fromDate, $toDate]);
            $material_request= $material_request->whereHas('material_request',function($query) use ($fromDate, $toDate){
                $query->whereBetween('date', [$fromDate, $toDate]);
            });
        }

        // Count all the items
        $material_request_count=$material_request->count();

        if(isset($request['sort_by']) && $request['sort_by']!='' && $request['sort_by']!=null){
                
            $sort_order=$request['sort_order']==1?'asc':'desc';

            $material_request = $material_request->orderby($request['sort_by'],$sort_order);

        }else{
            $material_request = $material_request->orderby('id','desc');
        }

        // Pagination
        $material_request=$material_request->skip($request['noofrec']*($request['currentpage']-1))->take($request['noofrec']??100)
            // ->orderBy('id','desc')
            ->get();

        foreach ($material_request as $key => $value) {
            $to_be_receive=MaterialRequestionApprove::where('w_requestion_item_id',$value['id'])
            ->where('is_received',0)
            ->sum('quantity');

            $value['to_be_received']=$to_be_receive;
        }

        $response=[
            'success'=>true,
            'count'=>$material_request_count,
            'material_request_item'=>$material_request,
            'message'=>'Workshop Material Requestion List Fetch Successfully'
        ];

        return response()->json($response, 200);
    }

     // Request of Order Approval from the accounts
    public function requisition_approval(Request $request){
        $user=Auth::user();
        // Validate Item
        $validator=validator($request->all(),[
           'requestion_item_id'=>'required|integer',
           'warehouse_id'=>'required|integer',
           'item_id'=>'required|integer',
           'transaction_date'=>'required',
           'quantity'=>'required' 
       ]);

       if ($validator->fails()) { 
           return [
               'success' => false, 
               'message' => $validator->errors()->first(),
           ];

       } else {

            try{

                $obj['w_requestion_item_id']=$request['requestion_item_id'];
                $obj['item_id']=$request['item_id']??0;
                $obj['transaction_date']=$request['transaction_date'];
                $obj['unit_id']=$request['unit_id']??0;
                $obj['warehouse_id']=$request['warehouse_id']??0;
                $obj['zone_id']=$request['zone_id']??0;
                $obj['rack_id']=$request['rack_id']??0;
                $obj['bin_id']=$request['bin_id']??0;
                $obj['goods_condition_type']=1;
                $obj['quantity']=$request['quantity']??0;   //Approved Quantity
                $obj['remarks']=$request['remarks'];
                $obj['upload_documents']=$request['upload_documents'];

                $requisition_item = WorkshopMaterialRequestionItem::where('id',$request->input('requestion_item_id'))->first();

                if($requisition_item['quantity']==$request['quantity']){
                    $requisition_item['status']='Approved';
                }else if($requisition_item['quantity']>$request['quantity']){
                    $requisition_item['status']='Partially Approved';
                }

                // Creating Transaction of approved order
                $transaction=MaterialRequestionApprove::create($obj);

                // Update existing quantity
                $requisition_item['approved_qty']=intval($requisition_item['approved_qty']) + intval($request['quantity']);

                //Update status of the Requestion item
                if($requisition_item['quantity']==$requisition_item['approved_qty']){
                    $requisition_item['status']='Completed';
                }else if($requisition_item['quantity']>$requisition_item['approved_qty']){
                    $requisition_item['status']='Partially Approved';
                }

                $requisition_item->update();

                // check status for all items and update Requestion status
                $all_material_items=WorkshopMaterialRequestionItem::where('requestion_id',$requisition_item['requestion_id'])->get();

                $check_stat=false;
                foreach ($all_material_items as $key => $value) {
                    if($value['status']!='Completed'){
                        $check_stat=true;
                        break;
                    }
                }

                if($check_stat){
                    $requestion=WorkshopMaterialRequestion::where('id',$requisition_item['requestion_id'])->first();
                    $requestion['status']='Partially';
                    $requestion->update();
                }else{
                    $requestion=WorkshopMaterialRequestion::where('id',$requisition_item['requestion_id'])->first();
                    $requestion['status']='Completed';
                    $requestion->update();
                }

                //Set Item move to store
                $item=Item::where('id',$request['item_id'])->lockForUpdate()->first();
                if($item){
                    $item['in_workshop']=1; // Set item in workshop store
                    $item->update();
                }

                // Saving Stock Entries
                $stockEntryService = new StockEntryService;
                $stockEntryService->mannualStockMove($request,$transaction,StaticMaster::$WORKSHOP_TRANSFER,2);
                
                $response=[
                    'success'=>true,
                    'message'=>'Workshop MaterialRequestion Approved successfully.',
                    'material_request_approval'=>$transaction
                ];

            } catch (\Illuminate\Database\Eloquent\ModelNotFoundException $e) {
               // Return error message in JSON if ID not found
               return response()->json(['message' => 'Invalid Id found'], 404);
           }

           return response()->json($response);

       }
    }


    // request receive from the manufacturing to the default store
    public function requisition_receive(Request $request){

        $user=Auth::user();
        // Validate Item
        $validator=validator($request->all(),[
           'transaction_date'=>'required',
           'requestion_item_id'=>'required',
           'item_id'=>'required',
           'fy'=>'required',
           'quantity'=>'required'
        ]);

        if ($validator->fails()) { 
            return [
                'success' => false, 
                'message' => $validator->errors()->first(),
            ];

        } else {

            //We are treating item level wise from requestion item table
            $obj['transaction_date']=$request['transaction_date'];
            $obj['transaction_id']=$request['requestion_item_id']??0;
            $obj['item_id']=$request['item_id']??0;
            $obj['unit_id']=$request['unit_id']??0;
            $obj['store_id']=1;    //default store 
            $obj['fy']=$request['fy'];
            $obj['quantity']=$request['quantity']??0;
            $obj['transaction_type']=StaticMaster::$WORKSHOP_TRANSFER;
            $obj['taken_by']=$request['taken_by'];
            $obj['issued_by']=$request['issued_by'];
            $obj['vehicle_no']=$request['vehicle_no'];
            $obj['driver_name']=$request['driver_name'];
            $obj['driver_contact']=$request['driver_contact'];
            $obj['attachments']=$request['attachments'];
            $obj['notes']=$request['notes'];
            $obj['inout']=1;

            // Stock added in store
            $store=WorkshopTransactionStore::create($obj);

            //Updating stock value from the source end i.e. requestion item table
            $material_item=WorkshopMaterialRequestionItem::where('id',$request['requestion_item_id'])->first();

            if($material_item){
                //Updating the received quantity and status on requestion item table
                $material_item['received_qty']=$material_item['received_qty'] + $request['quantity'];

                if($material_item['quantity']==$material_item['received_qty']){

                    $material_item['status']='Completed';

                }else{
                    $material_item['status']='Partially Received';
                }

                $material_item->update();
            }

            // checking the all status in Requestion Item Table
            $check_stat=false;
            $all_material_items=WorkshopMaterialRequestionItem::where('requestion_id',$material_item['requestion_id'])->get();

            if(!$all_material_items->isEmpty()){
                
                foreach ($all_material_items as $key => $value) {
                    if($value['status']!='Completed'){
                        $check_stat=true;
                        break;
                    }
                }

                if($check_stat){
                    $requestion=WorkshopMaterialRequestion::where('id',$material_item['requestion_id'])->first();
                    $requestion['status']='Partially';
                    $requestion->update();
                }else{
                    $requestion=WorkshopMaterialRequestion::where('id',$material_item['requestion_id'])->first();
                    $requestion['status']='Completed';
                    $requestion->update();
                }
            }else{
                $requestion=WorkshopMaterialRequestion::where('id',$material_item['requestion_id'])->first();
                $requestion['status']='Partially';
                $requestion->update();
            }

            // Updating Store quantity in item table
            $item=Item::where('id',$request['item_id'])->first();

            if($item){
                $item['workshop_store']=$item['workshop_store']+$request['quantity'];

                $item->update();
            }

            $response=[
                'success'=>true,
                'message'=>'Stock Received successfully',
            ];
            
            return response()->json($response);
        }
    }
} 
