<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Models\BookingFoto;
use App\Services\WhatsAppService; // Import Service
use Illuminate\Http\Request;

class PesananFotoController extends Controller
{
    public function index()
    {
        $pesanan = BookingFoto::with([
            'pelanggan',
            'paketFoto',
            'detailAdditional.additional'
        ])
            ->whereIn('status_booking', ['menunggu_verifikasi', 'diterima'])
            ->latest()
            ->get();
        return view('admin.pesanan.foto', compact('pesanan'));
    }

    public function updateStatus(Request $request, $id)
    {
        try {
            $pesanan = BookingFoto::with(['pelanggan', 'paketFoto', 'detailAdditional.additional'])->findOrFail($id);
            $status = $request->jenis === 'terima' ? 'diterima' : ($request->jenis === 'selesai' ? 'selesai' : 'ditolak');
            $labels = [
                'diterima' => 'telah diterima',
                'selesai'  => 'berhasil diselesaikan',
                'ditolak'  => 'telah ditolak'
            ];
            $sessionKey = $status === 'ditolak' ? 'error' : 'success';
            $sessionMsg = "Booking {$pesanan->no_invoice} " . $labels[$status] . "!";
            session()->flash($sessionKey, $sessionMsg);
            $pesanan->update(['status_booking' => $status]);
            $waUrl = WhatsAppService::getFotoNotification($pesanan, $status);
            return response()->json([
                'success' => true,
                'wa_url'  => $waUrl
            ]);
        } catch (\Exception $e) {
            return response()->json(['success' => false, 'message' => $e->getMessage()], 500);
        }
    }
}
