<?php
function app_version()
{
    return 12;
    //    $setting = \App\Models\Setting::findOrFail(1);
//    return $setting->app_version;
}

function getAttributes()
{
    return \App\Models\Attribute::whereNull('parent_id')->orderBy('name')->get();
}

function getColor()
{
    return \App\Models\Attribute::with('children')->where('name', 'Color')->first();
}

function getSize()
{
    return \App\Models\Attribute::with('children')->where('name', 'Size')->first();
}

function getMaterial()
{
    return \App\Models\Attribute::with('children')->where('name', 'Material')->first();
}

function getBrand()
{
    return \App\Models\Attribute::with('children')->where('name', 'Brand')->first();
}

function getVendors()
{
    return \App\Models\Attribute::with('children')->where('name', 'Vendor')->first();
}

function getCustomers()
{
    return \App\Models\User::where('type', 3)->get();
}

function getListings()
{
    return \App\Models\Listing::where('status', 1)->get();
}

function getFtListings()
{
    return \App\Models\Listing::where(['status' => 1, 'is_featured' => 1])->get();
}

function generatePin($id)
{
    $randomAlpha = function ($length = 3) {
        $alpha = '';
        for ($i = 0; $i < $length; $i++) {
            $alpha .= chr(rand(97, 122));
        }
        return $alpha;
    };
    $rand_alpha_1 = $randomAlpha();
    $rand_alpha_2 = $randomAlpha();
    return $rand_alpha_1 . '-' . $id . $rand_alpha_2;
}

function getListingsCount()
{
    return \App\Models\Listing::count();
}

function getAttributesByType($type)
{
    return \App\Models\Attribute::where('type', $type)->whereNull('parent_id')->get();
}

function getCategories()
{
    return \App\Models\Category::where('status', 1)->get();
}
function getLE()
{
    return \App\Models\User::find(1)->landing_emails;
}

function generateReferenceNumber($userId, $table)
{
    $lastLead = \Illuminate\Support\Facades\DB::table($table)
        ->where('added_by', $userId)
        ->orderBy('reference_number', 'desc')
        ->first();
    return $lastLead ? $lastLead->reference_number + 1 : 1000;
}
