<?php

namespace App\Http\Controllers;

use App\Models\ClientAudit;
use Illuminate\Support\Facades\Log;
use Illuminate\Http\Request;
use App\Models\Clients;
use App\Repositories\ResponseRepository;

class ClientsController extends Controller
{
    public function __construct(ResponseRepository $response)
    {
        $this->response = $response;
        $this->storeRules = [
            'client_name' => 'required',
            'client_nick_name' => 'required',
            'active_status' => 'required',
        ];
    }
    public function indexClient()
    {
        $audits = ClientAudit::with('client')->orderBy('created_at', 'desc')
            ->take(3)
            ->get();
        $data['data'] =  Clients::orderBy('client_id', 'desc')->get();
        $data['recent_changes'] = $audits;
        return $this->response->jsonResponse(false, " Client Details Fetched Successfully", $data, 300);
    }

    public function getActiveClients()
    {
        return $this->response->jsonResponse(false, "Client Detail Fetched Successfully",  Clients::where('active_status', 1)->orderBy('client_id', 'desc')->get(), 300);
    }

    public function store(Request $request)
    {
        $validate = $this->response->validate($request->all(), $this->storeRules);
        if ($validate === true) {
            $client =  Clients::create($request->all());
            $this->logAudit('create', null, $client->toArray(), $client->client_id);
            return $this->response->jsonResponse(false, $this->response->message('Client', 'store'), $client, 301);
        } else {
            return $validate;
        }
    }

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

    public function updateClients(Request $request)
    {
        $validate = $this->response->validate($request->all(), [
            'client_name' => 'required',
            'client_nick_name' => 'required',
            'active_status' => 'required',

        ]);
        if ($validate === true) {

            $client = $this->findClients($request->client_id);
            $oldData = $client->toArray();
            $client->update($request->all());
            $this->logAudit('update', $oldData, $request->all(), $client->client_id);
            return $this->response->jsonResponse(false, $this->response->message('Client', 'update'), $client, 303);
        } else {
            return $validate;
        }
    }

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

    public function findClients($id)
    {
        return Clients::find($id);
    }

    public function searchClients($search)
    {
        if ($search === "null") {
            return $this->response->jsonResponse(false, $this->response->message('Client', 'search'), [], 910);
        }
        return $this->response->jsonResponse(false, $this->response->message('Client', 'search'), Clients::where('client_name', 'LIKE', '%' . $search . '%')->get(), 911);
    }
    public function clientsSwitch($id)
    {
        $client = $this->findClients($id);
        if ($client) {

            $oldStatus = $client->active_status;
            $newStatus = $oldStatus === 1 ? 0 : 1;
            $msg = $newStatus === 0 ? 'Deactivated' : 'Activated';

            $client->update(['active_status' => $newStatus]);

            $this->logAudit('status_change', ['active_status' => $oldStatus], ['active_status' => $newStatus], $id);
            return $this->response->jsonResponse(false, 'Client ' . $msg . ' Successfully', $client, 306);
        }
        return $this->response->jsonResponse(true, 'Client Not Exists', [], 307);
    }
    protected function logAudit($action, $oldData, $newData, $clientId)
    {
        ClientAudit::create([
            'client_id' => $clientId,
            'action' => $action,
            'old_data' => $oldData ? json_encode($oldData) : null,
            'new_data' => json_encode($newData),
        ]);
    }
}
