<?php

use App\Models\Setting;
use Carbon\Carbon;

// Format the date
if (!function_exists('admin_info')) {
    function admin_info()
    {
        return [
            'address' => '640 Bazar Road,  Madda Badda, Dhaka.',
            'logo' => 'abxs',
            'name' => 'Khalilur Enterprise',
            'email' => 'unity-info@yahoo.com',
            'provider' => 'Md. Moenul Islam  
',
            'manager' => 'Abu Bokkor Kanun Mia (01710222843)',
            'phone' => '01999900335'
        ];
    }
}
// Invoice Formating
if (!function_exists('invoice_no')) {
    function invoice_no($lastInvoiceRow, $prefix = 'PIVO')
    {
        $year = Carbon::now()->format('Y');

        if ($lastInvoiceRow) {
            $nextId = (int) $lastInvoiceRow + 1;
            $formattedId = sprintf('%04d', $nextId);
        } else {
            $formattedId = '0001';
        }

        return $prefix . '-' . $formattedId . '-' . $year;
    }
}



if (!function_exists('setting')) {
    function setting()
    {
        return Setting::first();
    }
}

/**
 * Generate next sequential code
 * 
 * @param mixed $lastRecord The last record from database (model instance or code value)
 * @param string $prefix Prefix for the code (e.g., 'BR', 'CUST')
 * @param int $padding Number of digits for padding (default: 4)
 * @param string $codeField Field name to extract code from (default: 'code' or 'branch_code')
 * @param string $separator Separator between prefix and number (default: '-')
 * @return string Generated code (e.g., 'BR-0001', 'CUST-00001')
 */
if (!function_exists('generate_next_code')) {
    function generate_next_code($lastRecord = null, $prefix = 'BR', $padding = 4, $codeField = null, $separator = '-')
    {
        $nextNumber = 1;
        
        if ($lastRecord) {
            // If lastRecord is a model instance, extract code from specified field
            if (is_object($lastRecord)) {
                // Try to get code from specified field, or default fields
                $codeValue = null;
                if ($codeField && isset($lastRecord->$codeField)) {
                    $codeValue = $lastRecord->$codeField;
                } elseif (isset($lastRecord->code)) {
                    $codeValue = $lastRecord->code;
                } elseif (isset($lastRecord->branch_code)) {
                    $codeValue = $lastRecord->branch_code;
                }
                
                if ($codeValue) {
                    // Extract number from code (e.g., 'BR-0001' -> 1)
                    preg_match('/\d+$/', $codeValue, $matches);
                    $nextNumber = $matches ? (int)$matches[0] + 1 : 1;
                } else {
                    // If no code field found, use ID + 1
                    $nextNumber = isset($lastRecord->id) ? (int)$lastRecord->id + 1 : 1;
                }
            } elseif (is_numeric($lastRecord)) {
                // If lastRecord is a number
                $nextNumber = (int)$lastRecord + 1;
            } elseif (is_string($lastRecord)) {
                // If lastRecord is a string code
                preg_match('/\d+$/', $lastRecord, $matches);
                $nextNumber = $matches ? (int)$matches[0] + 1 : 1;
            }
        }
        
        $formattedNumber = sprintf('%0' . $padding . 'd', $nextNumber);
        
        return $prefix . $separator . $formattedNumber;
    }
}
