<?php

namespace App\Helpers;

use App\Models\Shipment;
use App\Models\Setting;

class TrackingHelper
{
    public static function generateNumber()
    {
        // Get settings
        $prefix = Setting::get('delivery_prefix', 'SHIP');
        $digits = Setting::get('tracking_digits', 7);

        // Get last shipment number
        $lastShipment = Shipment::latest('id')->first();
        $lastNumber = $lastShipment ? $lastShipment->id : 0;
        
        // Generate new number
        $newNumber = $lastNumber + 1;
        $paddedNumber = str_pad($newNumber, $digits, '0', STR_PAD_LEFT);
        
        return $prefix . $paddedNumber;
    }

    public static function validateNumber($number)
    {
        return Shipment::where('tracking_number', $number)->exists();
    }
}