<?php

namespace App\Http\Controllers;

use Illuminate\Support\Facades\Log;
use Illuminate\Http\Request;
use App\Models\Acknowledgement;
use App\Models\AcknowledgementAudit;
use App\Repositories\ResponseRepository;

class AcknowledgementController extends Controller
{
    public function __construct(ResponseRepository $response)
    {
        $this->response = $response;
        $this->storeRules = [
            'acknowledgement_name' => 'required',
            'active_status' => 'required',
        ];
    }

    public function indexAcknowledgement()
    {
        $audits = AcknowledgementAudit::with('acknowledgement')->orderBy('created_at', 'desc')
            ->take(3)
            ->get();
        $data['data'] =  Acknowledgement::orderBy('acknowledgement_id', 'desc')->get();
        $data['recent_changes'] = $audits;
        return $this->response->jsonResponse(false, " Acknowledgement Details Fetched Successfully", $data, 300);
    }

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

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

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

    public function updateAcknowledgement(Request $request)
    {
        $validate = $this->response->validate($request->all(), [
            'acknowledgement_name' => 'required',
            'active_status' => 'required'
        ]);
        if ($validate === true) {
            $result = $this->findAcknowledgement($request->acknowledgement_id);
            $oldData = $result->toArray();
            $result->update($request->all());
            $this->logAudit('update', $oldData, $request->all(), $result->acknowledgement_id);
            return $this->response->jsonResponse(false, $this->response->message('Acknowledgement', 'update'), $result, 303);
        } else {
            return $validate;
        }
    }

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

    public function findAcknowledgement($id)
    {
        return Acknowledgement::find($id);
    }

    public function acknowledgementSwitch($id)
    {
        $size = $this->findAcknowledgement($id);
        if ($size) {
            $oldStatus = $size->active_status;
            $newStatus = $oldStatus === 1 ? 0 : 1;
            $msg = $newStatus === 0 ? 'Deactivated' : 'Activated';
            $size->update(['active_status' => $newStatus]);
            $this->logAudit('status_change', ['active_status' => $oldStatus], ['active_status' => $newStatus], $id);
            return $this->response->jsonResponse(false, 'Acknowledgement ' . $msg . ' Successfully', $size, 306);
        }
        return $this->response->jsonResponse(true, 'Acknowledgement Not Exists', [], 307);
    }

    public function searchAcknowledgement($search)
    {
        if ($search === "null") {
            return $this->response->jsonResponse(false, $this->response->message('Acknowledgement', 'search'), [], 910);
        }
        return $this->response->jsonResponse(false, $this->response->message('Acknowledgement', 'search'), Acknowledgement::where('acknowledgement_name', 'LIKE', '%' . $search . '%')->get(), 911);
    }
    protected function logAudit($action, $oldData, $newData, $id)
    {
        AcknowledgementAudit::create([
            'acknowledgement_id' => $id,
            'action' => $action,
            'old_data' => $oldData ? json_encode($oldData) : null,
            'new_data' => json_encode($newData),
        ]);
    }
}
