<?php

namespace App\Http\Controllers;

use Illuminate\Support\Facades\Log;
use Illuminate\Http\Request;
use App\Models\ProjectAllocation;
use App\Models\User;
use App\Repositories\ResponseRepository;
class ProjectAllocationController extends Controller
{
    public function __construct(ResponseRepository $response) {
        $this->response = $response;
        $this->storeRules = [

            'project_allocation_name' => 'nullable',
            'client_id' => 'nullable',
            'accounts_id' => 'nullable',
            'ueser_create_id' => 'nullable',
            // 'contrator_details_id' => 'nullable',
            // 'employee_details_id' => 'nullable',
            'projects_id' => 'required',
            'role_id' => 'required',
        ];
    }

    public function indexProjectAllocation() {
        return $this->response->jsonResponse(false," Project Allocation Details Fetched Successfully", ProjectAllocation::with('clientUserlist','contractorUserlist','employeeUserlist','projectList','rolesList')->orderBy('project_allocation_id', 'desc')->get(), 300);
    }
    public function indexProjectAllocationSubmitter() {
        return $this->response->jsonResponse(
            false,
            "Project Allocation Submitter Details Fetched Successfully",
            ProjectAllocation::with('projectList', 'rolesList','UserCreateList')
                ->whereHas('rolesList', function ($query) {
                    $query->where('role_name', 'Submitter');
                })
                ->orderBy('project_allocation_id', 'desc')
                ->get(),
            300
        );
    }
    public function indexProjectAllocationReviewer() {
        return $this->response->jsonResponse(
            false,
            "Project Allocation Reviewer Details Fetched Successfully",
            ProjectAllocation::with('projectList', 'rolesList','UserCreateList')
                ->whereHas('rolesList', function ($query) {
                    $query->where('role_name', 'Reviewer');
                })
                ->orderBy('project_allocation_id', 'desc')
                ->get(),
            300
        );
    }
    // public function indexProjectAllocationClient() {
    //     return $this->response->jsonResponse(false," Project Allocation Details Fetched Successfully", ProjectAllocation::with('projectList','rolesList')->orderBy('client_details_id', 'desc')->get(), 300);
    // }
    public function indexProjectAllocationClient($ueser_create_id) {
        $data = ProjectAllocation::with('projectList', 'rolesList','accountlist','clientlist',)
            ->where('ueser_create_id', $ueser_create_id)
            ->orderBy('project_allocation_id', 'desc')
            ->get();

        return $this->response->jsonResponse(false, "Project Allocation Details Fetched Successfully", $data, 300);
    }
    public function indexProjectAllocationContrator($contratorDetailsId) {
        $data = ProjectAllocation::with('projectList', 'rolesList','accountlist','clientlist')
            ->where('contrator_details_id', $contratorDetailsId)
            ->orderBy('project_allocation_id', 'desc')
            ->get();

        return $this->response->jsonResponse(false, "Project Allocation Details Fetched Successfully", $data, 300);
    }
    public function indexProjectAllocationEmployee($employeeDetailsId) {
        $data = ProjectAllocation::with('projectList', 'rolesList')
            ->where('employee_details_id', $employeeDetailsId)
            ->orderBy('project_allocation_id', 'desc')
            ->get();

        return $this->response->jsonResponse(false, "Project Allocation Details Fetched Successfully", $data, 300);
    }

    // public function store(Request $request) {
    //     $validate = $this->response->validate($request->all(), $this->storeRules);
    //     if($validate === true) {
    //         return $this->response->jsonResponse(false, $this->response->message('Project Allocation', 'store'), ProjectAllocation::create($request->all()), 301);
    //     } else {
    //         return $validate;
    //     }
    // }

    public function updateRoleInUserModel()
{
    // Find the corresponding User record using client_details_id
    $user = User::where('ueser_create_id', $this->ueser_create_id)->first();

    // Update the role_id in the User model
    if ($user) {
        $user->update([
            'role_id' => $this->role_id,
        ]);
    }
}

    

// public function store(Request $request) {
//     $validate = $this->response->validate($request->all(), $this->storeRules);

//     if ($validate === true) {
//         // Create a ProjectAllocation record
//         $projectAllocation = ProjectAllocation::create($request->all());

//         // Call the updateRoleInUserModel method
//         $projectAllocation->updateRoleInUserModel();

//         return $this->response->jsonResponse(false, $this->response->message('Project Allocation', 'store'), $projectAllocation, 301);
//     } else {
//         return $validate;
//     }
// }

// public function store(Request $request) {
//     $validate = $this->response->validate($request->all(), $this->storeRules);
//     if ($validate === true) {
//         // Create a ProjectAllocation record
//         $projectAllocation = ProjectAllocation::create($request->all());

//         // Update the User model with the provided data
//         $user = User::find($request->input('id'));
//         if ($user) {
//             $user->update([
//                 'client_details_id' => $request->input('client_details_id'),
//                 'role_id' => $request->input('role_id'),
//             ]);
//         }

//         return $this->response->jsonResponse(false, $this->response->message('Project Allocation', 'store'), $projectAllocation, 301);
//     } else {
//         return $validate;
//     }
// }

// public function store(Request $request) {
//     $validate = $this->response->validate($request->all(), $this->storeRules);
//     if ($validate === true) {
//         return $this->response->jsonResponse(false, $this->response->message('Project Allocation', 'store'), ProjectAllocation::create($request->all()), 301);
//     } else {
//         return $validate;
//     }
// }

// public function store(Request $request) {
//     $validate = $this->response->validate($request->all(), $this->storeRules);
    
//     if ($validate === true) {
//         // Create a ProjectAllocation record
//         $projectAllocation = ProjectAllocation::create($request->all());

//         // Manually update the User model with the same client_details_id and role_id
//         User::where('client_details_id', $projectAllocation->client_details_id)
//             ->update(['role_id' => $projectAllocation->role_id]);

//         return $this->response->jsonResponse(false, $this->response->message('Project Allocation', 'store'), $projectAllocation, 301);
//     } else {
//         return $validate;
//     }
// }
public function store(Request $request) {
    $validate = $this->response->validate($request->all(), $this->storeRules);
    
    if ($validate === true) {
        // Create a ProjectAllocation record
        $projectAllocation = ProjectAllocation::create($request->all());

        // Manually update the User model with the same client_details_id and role_id
        $user = User::where('ueser_create_id', $projectAllocation->ueser_create_id)->first();

        if ($user) {
            $user->role_id = $projectAllocation->role_id;
            $user->save();
        } else {
            // Handle the case where no matching user is found
        }

        return $this->response->jsonResponse(false, $this->response->message('Project Allocation', 'store'), $projectAllocation, 301);
    } else {
        return $validate;
    }
}

    public function show($id) {
        return $this->response->jsonResponse(false, $this->response->message('Project Allocation Us Details', 'show'), $this->findsProjectAllocation($id), 500);
    }
    public function updateProjectAllocation(Request $request) {
        $validate = $this->response->validate($request->all(), [
            'project_allocation_name' => 'nullable',
            'client_id' => 'nullable',
            'accounts_id' => 'nullable',
            'ueser_create_id' => 'nullable',
            // 'contrator_details_id' => 'nullable',
            // 'employee_details_id' => 'nullable',
            'projects_id' => 'required',
            'role_id' => 'required',
        ]);
        if($validate === true) {
            return $this->response->jsonResponse(false, $this->response->message('Project Allocation', 'update'), $this->findProjectAllocation($request->project_allocation_id)->update($request->all()), 303);
        } else {
            return $validate;
        }
    }

    public function findProjectAllocation($id) {
        // return ProjectAllocation::find($id);
          return ProjectAllocation::with('projectList', 'rolesList','accountlist','clientlist')->orderBy('project_allocation_id', 'desc')->find($id);


    }
    public function findsProjectAllocation($id) {
        return ProjectAllocation::find($id);
        //   return ProjectAllocation::with('projectList', 'rolesList','accountlist','clientlist')->orderBy('project_allocation_id', 'desc')->find($id);


    }
    public function projectAllocationSwitch($id) {
        $size = $this->findProjectAllocation($id);
        if($size) {
            $value = $size->active_status === 1 ? 0 : 1;
            $msg = $value === 0 ? 'Deactivated': 'Activated';
            return $this->response->jsonResponse(false, 'Project Allocation '.$msg.' Successfully', $size->update(['active_status' => $value]), 306);
        }
        return $this->response->jsonResponse(true, 'Project Allocation Not Exists', [], 307);
    }

}
