<?php

namespace App\Http\Controllers;


use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;
use App\Models\Accounts;
use App\Models\Clients;
use App\Repositories\ResponseRepository;
use Illuminate\Validation\Rule;
use App\Models\Projects;



class ProjectsController extends Controller
{
    public function __construct(ResponseRepository $response)
    {

        $this->response = $response;
        $this->storeRules = [
            'accounts_id' => 'required',
            'client_id' => 'required',
            'project_name' => 'required',
            'active_status' => 'required',
        ];
    }

    public function indexadd()
    {
        return $this->response->jsonResponse(false, "Projects Detail Fetched Successfully",  Projects::with('accountslist', 'clientslist',)->orderBy('projects_id', 'desc')->get(), 300);
    }

    public function getProjectsDetailsId($projects_id)
    {
        return $this->response->jsonResponse(false, 'Projects Details Fetched Successfully', Projects::where('projects_id', $projects_id)->with('clientslist', 'accountslist')->first(), 201);
    }



    // public function getActiveAllProjectsByType($type) {
    //     return $this->response->jsonResponse(false,"Projects Detail Fetched Successfully",  Projects::where('cost_type_name', $type)->where('active_status',1)->orderBy('projects_id', 'desc')->get() , 300);
    // }

    // public function indexProjects() {
    //     return $this->response->jsonResponse(false," Projects Details Fetched Successfully", Projects::orderBy('projects_id', 'desc')->get(), 300);
    // }

    public function getActiveAllProjects()
    {
        return $this->response->jsonResponse(false, "Projects Detail Fetched Successfully",  Projects::with('accountslist', 'clientslist',)->where('active_status', 1)->orderBy('projects_id', 'desc')->get(), 300);
    }

    public function getActiveAllProjectsRoles()
    {
        return $this->response->jsonResponse(false, "Projects Detail Fetched Successfully",  Projects::with('projectrolist',)->orderBy('projects_id', 'desc')->get(), 300);
    }


    public function getProjectName()
    {
        return $this->response->jsonResponse(false, "Projects Detail Fetched Successfully",  Projects::with('projectname')->orderBy('projects_id', 'desc')->get(), 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('Projects', 'store'), Projects::create($request->all()), 301);
        } else {
            return $validate;
        }
    }

    public function show($id)
    {
        return $this->response->jsonResponse(false, $this->response->message('Projects', 'show'), $this->findProjects($id), 302);
    }

    public function updateAddProjects(Request $request)
    {
        $validate = $this->response->validate($request->all(), [
            'project_name' => 'required',
            'active_status' => 'required',
        ]);
        if ($validate === true) {
            return $this->response->jsonResponse(false, $this->response->message('Projects', 'update'), $this->findProjects($request->projects_id)->update($request->all()), 303);
        } else {
            return $validate;
        }
    }

    public function destroy($id)
    {
        $size = $this->findProjects($id);
        if ($size) {
            return $this->response->jsonResponse(false, $this->response->message('Projects', 'destroy'), $size->delete(), 304);
        }
        return $this->response->jsonResponse(true, 'Projects Not Exists', [], 305);
    }

    public function findProjects($id)
    {
        return Projects::find($id);
    }

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

    // public function searchProjects($search){
    //     if($search === "null") {
    //         return $this->response->jsonResponse(false, $this->response->message('Projects', 'search'), [], 910);
    //     }
    //     return $this->response->jsonResponse(false, $this->response->message('Projects', 'search'), Projects::where('project_name', 'LIKE', '%'.$search.'%')->get(), 911);
    // }


    public function searchProjects($search)
    {
        if ($search === "null") {
            return $this->response->jsonResponse(false, $this->response->message('Projects', 'search'), [], 910);
        }
        return $this->response->jsonResponse(false, $this->response->message('Projects', 'search'), Projects::with('accountslist', 'clientslist',)->where('project_name', 'LIKE', '%' . $search . '%')->get(), 911);
    }
}
