<?php

namespace App\Http\Controllers;

use DB;
use App\Models\User;
use App\Models\EmpType;
use Illuminate\Http\Request;
use App\Models\EmpPersonalDetails;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Validator;

class EmpTypeController extends Controller
{
    public function index()
    {
        $userTable = $this->getUserTable();
        $query = EmpType::query();
        if ($userTable === "customer") {
            $query->where('del', '0')->where('customer_id', auth()->id())
                ->where('workspace_id', auth()->user()->current_workspace_id);
        }
        if ($userTable === "emp") {
          
            $query->where('del', '0')->where('customer_id', auth()->user()->customer_id) // Assuming customer_id links employee to projects
                ->where('workspace_id', auth()->user()->workspace_id);
        }
      
        $emp_types =  $query->get();
      
        return $this->success($emp_types, 'Employee Type Get Successfully');
    }

    public function create()
    {
        //
    }

    public function store(Request $request)
    {
        $user = auth()->user();
        $validator = Validator::make($request->all(), [
            'title' => 'required'
                ]);
        
        if ($validator->fails()) {

            $errors = $validator->errors();

            return $this->error($errors,422);
        } else {
          
           $validatedData =  $validator->validated();
           $userTable = $this->getUserTable();
           $auth_id =0;
           $workspace_id = 0;
           if ($userTable === "customer") {
               $auth_id = Auth::user()->id;
               $workspace_id = Auth::user()->current_workspace_id;  
               $authPersonalDetails = User::where('id', Auth::user()->id)->first(); 
           }
           if ($userTable === "emp") {
               $auth_id = auth()->user()->customer_id;
               $workspace_id = auth()->user()->workspace_id;
               $authPersonalDetails = EmpPersonalDetails::where('emp_id', Auth::user()->id)->first();  
           }
           $validatedData['customer_id'] = $auth_id;
           $validatedData['workspace_id'] = $workspace_id;
           $validatedData['created_by'] =  Auth::user()->id;
           EmpType::insert($validatedData);

           $emp_types =  EmpType::where('customer_id', $auth_id)
                                ->where('workspace_id', $workspace_id)
                                ->where('del', '0') 
                                ->get();

           $data['emp_types'] = $emp_types;

           return $this->success($data, 'Employee Type Saved Successfully');

        }        

    }

    public function show($id)
    {
        //
    }

    public function type_edit(Request $request)
    {
        $id=$request->id;
        $userTable = $this->getUserTable();
        $emp_types = EmpType::find($id);
        if (
            $userTable == "customer" && ($emp_types->workspace_id != auth()->user()->current_workspace_id || $emp_types->customer_id != auth()->user()->id)
        ) {
            return $this->message('You do not have access to this Employee Type', 403);
        }

        if ($userTable == "emp" && ($emp_types->customer_id != auth()->user()->customer_id || $emp_types->workspace_id != auth()->user()->workspace_id)) {
            return $this->message('You do not have access to this Employee Type', 403);
        }
        return $this->success($emp_types, 'Get Employee Type Details Successfully');
    }

    public function type_update(Request $request)
    {
        $user = auth()->user();
        $validator = Validator::make($request->all(), [
            'id' => 'required',
            'title' => 'required'
        ]);
        
        if ($validator->fails()) {

            $errors = $validator->errors();

            return $this->error($errors,422);
            
        } else {
            $id=$request->id;
            $validatedData =  $validator->validated();
            $emp_types = EmpType::where('id', $id)->first();
            $userTable = $this->getUserTable();
            if ($userTable == "customer" && ($emp_types->workspace_id != auth()->user()->current_workspace_id || $emp_types->customer_id != auth()->user()->id)
            ) {
                return $this->message('You do not have access to this Employee Type', 403);
            }

            if ($userTable == "emp" && ($emp_types->customer_id != auth()->user()->customer_id || $emp_types->workspace_id != auth()->user()->workspace_id)) {
                return $this->message('You do not have access to this Employee Type', 403);
            }
            if (!$emp_types) {
                return $this->notFound('Employee Type Not Found', 404);
            }
            $emp_types->update($validatedData);

            if($userTable == "customer"){
                $data['emp_types'] = EmpType::where('customer_id', $user->id)
                ->where('workspace_id', $user->current_workspace_id)
                ->where('del', '0')
                ->get();
            }
            elseif($userTable == "emp"){
                $data['emp_types'] = EmpType::where('customer_id',auth()->user()->customer_id)
                ->where('workspace_id', auth()->user()->workspace_id)
                ->where('del', '0')
                ->get();
            }

            return $this->success($data, 'Employee Type Updated Successfully');
        }        
    }

    public function type_delete(Request $request)
    {
        $id=$request->id;
        $userTable = $this->getUserTable();
            $emp_types = EmpType::find($id);
        
            if ($userTable == "customer" && ($emp_types->workspace_id != auth()->user()->current_workspace_id || $emp_types->customer_id != auth()->user()->id)) {
                return $this->message('You do not have access to this Employee Type', 403);
            }

            if ($userTable == "emp" && ($emp_types->customer_id != auth()->user()->customer_id || $emp_types->workspace_id != auth()->user()->workspace_id)) {
                return $this->message('You do not have access to this Employee Type', 403);
            }
            $emp_types->del='1';
            $emp_types->save();

            $user = auth()->user();
            if($userTable == "customer"){
                    $data['emp_types'] = EmpType::where('customer_id', $user->id)
                    ->where('workspace_id', $user->current_workspace_id)
                    ->where('del', '0')
                    ->get();
                }
                elseif($userTable == "emp"){
                    $data['emp_types'] = EmpType::where('customer_id',auth()->user()->customer_id)
                    ->where('workspace_id', auth()->user()->workspace_id)
                    ->where('del', '0')
                    ->get();
                }
    
            return $this->success($data, 'Employee Type Deleted Successfully');
    }
}
