<?php

namespace App\Http\Controllers;

use App\Functions\ResponseHelper;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Routing\Controller;

class BasicController extends Controller
{
    use AuthorizesRequests, DispatchesJobs, ValidatesRequests;

    public $Client;

    public function __construct()
    {
        app()->setLocale(request()->lang ?? 'en');
        
        config()->set('currency_id', request()->currency_id ?? 1);

        if (auth('sanctum')->check()) {
            $user = auth('sanctum')->user();
            if ($user->lang !== app()->getLocale()) {
                $user->lang = app()->getLocale();
                $user->save();
            }
        }
    }

    public function CheckAuth()
    {
        if (! auth('sanctum')->check()) {
            return ResponseHelper::make([], __('trans.You not auth'), false, 404);
        } else {
            $this->Client = auth('sanctum')->user();
        }
    }

    public function CheckCount($Data)
    {
        if ($Data->count() < 1) {
            return ResponseHelper::make([], __('trans.Data not found'), true, 404);
        }
    }

    public function CheckExist($Model)
    {
        if (! $Model) {
            return ResponseHelper::make((object) [], __('trans.Data not found'), true, 404);
        }
    }
}
