<?php

namespace App\Http\Controllers;

use App\Models\AccountAudit;
use Illuminate\Support\Facades\Log;
use Illuminate\Http\Request;
use App\Models\Accounts;
use App\Repositories\ResponseRepository;

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

    public function indexAccounts()
    {
        $audits = AccountAudit::with('account')->orderBy('created_at', 'desc')
            ->take(3)
            ->get();
        $data['data'] =  Accounts::orderBy('accounts_id', 'desc')->get();
        $data['recent_changes'] = $audits;
        return $this->response->jsonResponse(false, " Accounts Details Fetched Successfully", $data, 300);
    }

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

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


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

    public function updateAccounts(Request $request)
    {
        $validate = $this->response->validate($request->all(), [
            'account_name' => 'required',
            'active_status' => 'required'
        ]);

        if ($validate === true) {
            $account = $this->findAccounts($request->accounts_id);
            $oldData = $account->toArray();
            $account->update($request->all());
            $this->logAudit('update', $oldData, $request->all(), $account->accounts_id);

            return $this->response->jsonResponse(false, $this->response->message('Accounts', 'update'), $account, 303);
        } else {
            return $validate;
        }
    }


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

    public function findAccounts($id)
    {
        return Accounts::find($id);
    }

    public function accountsSwitch($id)
    {
        $account = $this->findAccounts($id);
        if ($account) {
            $oldStatus = $account->active_status;
            $newStatus = $oldStatus === 1 ? 0 : 1;
            $msg = $newStatus === 0 ? 'Deactivated' : 'Activated';

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

            $this->logAudit('status_change', ['active_status' => $oldStatus], ['active_status' => $newStatus], $id);

            return $this->response->jsonResponse(false, 'Accounts ' . $msg . ' Successfully', $account, 306);
        }
        return $this->response->jsonResponse(true, 'Accounts Not Exists', [], 307);
    }


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

    protected function logAudit($action, $oldData, $newData, $accountId)
    {
        AccountAudit::create([
            'account_id' => $accountId,
            'action' => $action,
            'old_data' => $oldData ? json_encode($oldData) : null,
            'new_data' => json_encode($newData),
            'user_id' => null
        ]);
    }
}
