<?php

namespace App\Services;

use Carbon\Carbon;

class WhatsAppService
{
    public static function getBuketNotification($pesanan, $status)
    {
        if ($status === 'selesai') return null;

        $nama = $pesanan->pelanggan->nama;
        $produk = $pesanan->buket->nama;
        $invoice = $pesanan->no_invoice;
        $total = number_format($pesanan->total_bayar, 0, ',', '.');
        $req = $pesanan->request ?? '-';
        $ucapan = $pesanan->ucapan ?? '-';
        $tgl_obj = Carbon::parse($pesanan->tgl_ambil)->locale('id');
        $tanggal = $tgl_obj->translatedFormat('l, d F Y');
        $waktu = $tgl_obj->format('H:i');
        $msg = self::getBuketTemplate($status, $nama, $invoice, $produk, $total, $req, $ucapan, $tanggal, $waktu);
        $no_wa = self::formatPhoneNumber($pesanan->pelanggan->no_wa);

        return "https://wa.me/{$no_wa}?text=" . urlencode($msg);
    }

    private static function getBuketTemplate($status, $nama, $invoice, $produk, $total, $req, $ucapan, $tanggal, $waktu)
    {
        if ($status === 'diterima') {
            return "Halo Kak *{$nama}*,\n\n" .
                "Pesanan Anda dengan Nomor Invoice: *{$invoice}* telah kami *TERIMA* dan masuk dalam daftar proses pengerjaan.\n\n" .
                "*Rincian Pesanan:*\n" .
                "- *Produk:* {$produk}\n" .
                "- *Total Bayar:* Rp {$total}\n" .
                "- *Request:* {$req}\n" .
                "- *Ucapan:* \"{$ucapan}\"\n" .
                "- *Waktu Pengambilan:* {$tanggal} pukul {$waktu} WIB\n\n" .
                "Mohon simpan rincian ini dan ditunggu info selanjutnya ya Kak. Terima kasih!";
        }

        return "Halo Kak *{$nama}*,\n\n" .
            "Mengenai pesanan Anda dengan Nomor Invoice: *{$invoice}* terpaksa kami *TOLAK* dikarenakan:\n\n" .
            "*[TULIS ALASAN DI SINI]*\n\n" .
            "*Rincian Pesanan:*\n" .
            "- *Produk:* {$produk}\n" .
            "- *Total Bayar:* Rp {$total}\n" .
            "- *Waktu Pengambilan:* {$tanggal}\n\n" .
            "Admin kami akan segera menghubungi Kakak terkait proses pengembalian dana. Mohon maaf atas ketidaknyamanannya.";
    }

    private static function formatPhoneNumber($number)
    {
        $number = preg_replace('/[^0-9]/', '', $number);
        if (str_starts_with($number, '0')) {
            return '62' . substr($number, 1);
        } elseif (str_starts_with($number, '8')) {
            return '62' . $number;
        }
        return $number;
    }
    public static function getFotoNotification($pesanan, $status)
    {
        if ($status === 'selesai') return null;

        $nama = $pesanan->pelanggan->nama;
        $paket = $pesanan->paketFoto->nama;
        $invoice = $pesanan->no_invoice;
        $total = number_format($pesanan->total_bayar, 0, ',', '.');

        // Format Tanggal & Jam
        $tgl_obj = Carbon::parse($pesanan->tgl_booking)->locale('id');
        $tanggal = $tgl_obj->translatedFormat('l, d F Y');
        $jam = Carbon::parse($pesanan->jam_mulai)->format('H:i') . ' - ' . Carbon::parse($pesanan->jam_selesai)->format('H:i');

        // Logika List Additional
        $list_additional = $pesanan->detailAdditional->count() > 0
            ? $pesanan->detailAdditional->map(fn($item) => "- " . $item->additional->nama . " (x" . $item->qty . ")")->implode("\n")
            : "- Tidak ada additional";

        // Pilih Template
        $msg = self::getFotoTemplate($status, $nama, $invoice, $paket, $list_additional, $total, $tanggal, $jam);

        // Format Nomor HP
        $no_wa = self::formatPhoneNumber($pesanan->pelanggan->no_wa);

        return "https://wa.me/{$no_wa}?text=" . urlencode($msg);
    }

    private static function getFotoTemplate($status, $nama, $invoice, $paket, $additional, $total, $tanggal, $jam)
    {
        if ($status === 'diterima') {
            return "Halo Kak *{$nama}*,\n\n" .
                "Booking foto Anda dengan Invoice: *{$invoice}* telah kami *TERIMA*. \n\n" .
                "*Rincian Booking:*\n" .
                "- *Paket:* {$paket}\n" .
                "*Additional:*\n{$additional}\n" .
                "- *Total Bayar:* Rp {$total}\n\n" .
                "*Jadwal Sesi Foto:*\n" .
                " Tanggal: {$tanggal}\n" .
                " Jam: {$jam} WIB\n\n" .
                "Mohon datang 15 menit sebelum jadwal dimulai ya Kak. Sampai jumpa di studio!";
        }

        return "Halo Kak *{$nama}*,\n\n" .
            "Mohon maaf, booking foto Anda dengan Invoice *{$invoice}* terpaksa kami *TOLAK* dikarenakan:\n\n" .
            "*[TULIS ALASAN DI SINI]*\n\n" .
            "*Rincian Booking:*\n" .
            "- *Paket:* {$paket}\n" .
            "*Additional:*\n{$additional}\n" .
            "- *Total Bayar:* Rp {$total}\n" .
            "*Jadwal Sesi Foto:*\n" .
            " Tanggal: {$tanggal}\n" .
            " Jam: {$jam} WIB\n\n" .
            "Admin kami akan segera menghubungi Kakak untuk info pengembalian dana atau penjadwalan ulang. Terima kasih.";
    }

    public static function getPesananBuketMessage($transaksi, $pelanggan)
    {
        $total = number_format($transaksi->buket->harga, 0, ',', '.');

        $pesan = "Halo Admin! Saya sudah melakukan pembayaran untuk invoice *{$transaksi->no_invoice}*:\n\n" .
            "*Data Pemesan:*\n" .
            "- Nama: {$pelanggan->nama}\n" .
            "- WA: {$pelanggan->no_wa}\n\n" .
            "*Detail Produk:*\n" .
            "- Produk: {$transaksi->buket->nama}\n" .
            "- Total: Rp {$total}\n\n" .
            "Mohon segera diproses, ya! Terima kasih.";

        return "https://wa.me/6282337687878?text=" . urlencode($pesan);
    }
    public static function getBookingFotoMessage($booking, $paket, $addons, $grandTotal, $jamSelesai)
    {
        $txtAddons = "";
        if (count($addons) > 0) {
            $txtAddons = "\n*Additional:*";
            foreach ($addons as $item) {
                $txtAddons .= "\n- " . $item['nama'] . " (" . $item['qty'] . "x)";
            }
        }

        $pesan = "Halo Admin Flo.do! Saya sudah melakukan pembayaran untuk invoice *{$booking->no_invoice}*:\n\n" .
            "*Data Pemesan:*\n- Nama: {$booking->pelanggan->nama}\n- WA: {$booking->pelanggan->no_wa}\n\n" .
            "*Detail Booking:*\n- Paket: {$paket->nama}{$txtAddons}\n" .
            "- Tanggal: " . \Carbon\Carbon::parse($booking->tgl_booking)->translatedFormat('l, d F Y') . "\n" .
            "- Jam: {$booking->jam_mulai} - {$jamSelesai} WIB\n" .
            "- Total: Rp " . number_format($grandTotal, 0, ',', '.') . "\n\n" .
            "Mohon segera diproses, ya! Terima kasih.";

        return "https://wa.me/6282337687878?text=" . urlencode($pesan);
    }
}
