<?php

namespace App\Utility;

use App\Models\WhatsappTemplates;
use App\Services\SendWhatsAppService;
use App\Models\User;

class WhatsappUtility
{
    public static function whatsappPhoneForApi(?string $phone, string $defaultCountryCode = '91'): ?string
    {
        if (empty($phone)) {
            return null;
        }

        $digits = preg_replace('/\D/', '', $phone);
        $digits = ltrim($digits, '0');

        if (strlen($digits) === 10) {
            return $defaultCountryCode . $digits;
        }

        if (strlen($digits) === 12 && str_starts_with($digits, '91')) {
            return $digits;
        }

        if (strlen($digits) > 10 && str_starts_with($digits, $defaultCountryCode)) {
            return $digits;
        }

        return $digits;
    }

    public static function order_placement($customer = null, $order = null, $pdfUrl = '', ?string $senderName = null)
    {
        $siteName =  ucwords(\App\Models\Setting::get('site_name', env('APP_NAME', 'KiyoKart')));
        $mobile = self::whatsappPhoneForApi($customer->mobile); // e.g. 9505889960 → 919505889960 
        $data = [
            "phone_number" => $mobile,
            "sender_name"=> $senderName ?? auth()->user()?->name ?? $siteName,
            "template_name" => "order",
            "template_language" => "en_us",
            "header_document" => $pdfUrl,
            // "header_document" => "https://kiyokart.in/uploads/invoices/SALE-PCF-73-212.pdf",
            "header_document_name" => '#'.$order->id,
            "field_1" => ucwords($customer->name),
            "field_2" => '#'.$order->id,
            "field_3" => $siteName,
            "contact_first_name" => ucwords($customer->name),
            "contact_email" => $customer->email,
            "contact_groups" => $siteName,
            "contact_country" => "India",
            "contact_language" => "en_us",
            "whatsapp_template" => "order_placement"
        ];

        (new SendWhatsAppService())->sendWhatsApp($customer->mobile , $siteName , $data , $order);
    }

    public static function pos_order_placement($customer = null, $order = null, $pdfUrl = '', ?string $recipientMobile = null, ?string $senderName = null)
    {
        $siteName =  ucwords(\App\Models\Setting::get('site_name', env('APP_NAME', 'KiyoKart')));
        $toMobile = $recipientMobile ?: $customer->mobile;
        
        $toMobile = self::whatsappPhoneForApi($toMobile);
        $data = [
            "phone_number" => $toMobile,
            "sender_name"=> $senderName ?? auth()->user()?->name ?? $siteName,
            "template_name" => "order",
            "template_language" => "en_us",
            "header_document" => $pdfUrl,
            // "header_document" => "https://kiyokart.in/uploads/invoices/SALE-PCF-73-212.pdf",
            "header_document_name" => '#'.$order->id,
            "field_1" => ucwords($customer->name),
            "field_2" => '#'.$order->id,
            "field_3" => $siteName,
            "contact_first_name" => ucwords($customer->name),
            "contact_email" => $customer->email,
            "contact_groups" => $siteName,
            "contact_country" => "India",
            "contact_language" => "en_us",
            "whatsapp_template" => "pos_order_placement"
        ];

        (new SendWhatsAppService())->sendWhatsApp($toMobile, $siteName, $data, $order);
    }

    public static function order_tracking_details($customer = null, $order = null)
    {
        $siteName =  ucwords(\App\Models\Setting::get('site_name', env('APP_NAME', 'KiyoKart')));
        $mobile = self::whatsappPhoneForApi($customer->mobile); // e.g. 9505889960 → 919505889960 
        $data = [
            "phone_number" => $mobile,
            "sender_name"=> auth()->user()->name,
            "template_name" => "order_tracking_details",
            "template_language" => "en_us",
            "field_1" => ucwords($customer->name),
            "field_2" => '#'.$order->id,
            "field_3" => env('ORDERS_URL', 'https://kiyosnap.com/nbqxq'),
            "field_4" => $siteName,
            "contact_first_name" => ucwords($customer->name),
            "contact_email" => $customer->email,
            "contact_groups" => $siteName,
            "contact_country" => "India",
            "contact_language" => "en_us",
            "whatsapp_template" => "order_tracking_details"
        ];

        (new SendWhatsAppService())->sendWhatsApp($customer->mobile , $siteName , $data , $order);
    }
}
