<?php

namespace App\Http\Controllers\Hardware;

use App\Http\Controllers\Controller;
use App\Models\Order;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;

class HardwareController extends Controller
{
    public function index()
    {
            return view('hardware.orders.screen');
    }

    public function getOrdersForScreen(){
        $data = Order::where('state', 1)
            ->where('isVisible', true)
            ->whereIn('proccess_type', [1, 3])
            ->with('employee')
            ->with('customer')
            ->with('customer.device')
            ->latest()->paginate(8);
        return $data;
    }

    public function stat(){
        $wait = count( Order::where('state', 1)->whereIn('proccess_type', [1, 3])->get() );
        $working = count( Order::where('state', 2)->whereIn('proccess_type', [1, 3])->get() );
        $done = count( \App\Models\Request::where('latest_updated_user', Auth::id())->where('work_type', 1)->get() );
        return view('hardware.stat', compact('wait', 'working', 'done'));
    }

    // return orders view
    public function ordersPage(){
        return view('hardware.orders.orders');
    }
    // return orders api
    public function getOrders(Request $request)
    {
        if($request->type == 3) {
            $orders = \App\Models\Request::where('work_type', 1)
                ->where('latest_updated_user', auth()->user()->id)
                ->where('current_state', 1)
                ->with('user')
                ->with('order.customer.device')
                ->latest()
                ->paginate(8);
        }else {
            if ($request->type == 0) {
                $orders = Order::whereIn('state', [1, 2, 3])
                    ->whereIn('proccess_type', [1, 3])
                    ->with('employee')
                    ->with('customer')
                    ->with('customer.device')
                    ->latest()->paginate(8);
            } else
            $orders = Order::where('state', $request->type)
                ->whereIn('proccess_type', [1, 3])
                ->with('employee')
                ->with('customer')
                ->with('customer.device')
                ->latest()->paginate(8);
        }
            return $orders;
    }

    // Employee accept order
    public function acceptOrder(Request $request)
    {
        $order = Order::findOrFail($request->order);
        $isAllowed = $this->checkAcceptOrder($order);
        if($isAllowed){
            DB::beginTransaction();
            $order->update([
                'state' => 2,
            ]);

            DB::table('requests')->insert([
                'user_id' => auth()->user()->id,
                'order_id' => $order->id,
                'type' => $this->getProccessType($order->proccess_type),
                'current_state' => 1,
                'latest_updated_user' => auth()->user()->id,
                'created_at' => now(),
                'updated_at' => now()
            ]);
            DB::commit();
            return response()->json(['message' => 'accepted successfully', 201]);
        } else{
            abort(302);
        }

    }

    public function finishedRepair(Request $request)
    {
        $request->validate([
            'cost' => 'required|numeric',
            'notes' => 'required'
        ]);
        $order = Order::findOrFail($request->order);
        // check if order state is 2 and user auth type hdc 2
        if($order->state == 2 && auth()->user()->type == 2){
            //check order type in request table is it single or pulrar
            $order_in_request = \App\Models\Request::where('order_id', $order->id)
//                                                ->where('user_id', Auth::id())
                                                ->where('current_state', 1)
                                                ->first();
            // hardware employee work alone
           if($order->proccess_type == 1){
              DB::beginTransaction();
               $order->update([
                   'state' => 3,
                   'cost' => $order->cost + $request->cost,
                    'notice' => $order->notice . '<br>' . '----' .\auth()->user()->name .'----' . '<b>الكلفة: '. $request->cost .'</b>' . '<br>' . $request->notes
               ]);
               $order_in_request->update([
                  'lates_updated_employee' => Auth::id(),
                   'work_type' => 1
               ]);
               DB::commit();
               return response()->json(['message' => 'Order Updated Sucessfully'], 201);
           } else{
                  if($order->proccess_type == 3){
                      DB::beginTransaction();
                      $order->update([
                          'state' => 1,
                          'proccess_type' => 2,
                          'cost' => $order->cost + $request->cost,
                          'notice' => $order->notice . '<br>' . '----' .\auth()->user()->name .'----' . '<b>الكلفة: '. $request->cost .'</b>' . '<br>' . $request->notes
                      ]);
                      $order_in_request->update([
                          'work_type' => 1
                      ]);
                      DB::commit();
                      return response()->json(['message' => 'Order Updated Sucessfully'], 201);
                  } else {
                      abort(302);
                  }
           }

        } else {
            return abort(302);
        }
    }













    // Check if accept order is allowed for login employee
    public function checkAcceptOrder($request)
    {
        if($request->state === 1 && $request->proccess_type === 1 || $request->proccess_type === 3 && auth()->user()->type === 2)
        {
            return true;
        } else return false;
    }
    // check if proccess type tow steps on one
    public function getProccessType($proccess_type)
    {
        if($proccess_type == 3 || $proccess_type == 4){
            return 2;
        } else {
            return 1;
        }
    }
}
