<?php
/**
 * Couple Portal — Wedding Report (Post Wedding Summary) (RC-022)
 * Location: app/views/invitation/report.php
 */

if (!function_exists('format_money')) {
    function format_money(float $amount, string $currency = 'IDR'): string
    {
        if ($currency === 'IDR') {
            return 'Rp ' . number_format($amount, 0, ',', '.');
        }
        return $currency . ' ' . number_format($amount, 2, '.', ',');
    }
}
?>

<style>
    @media print {
        /* Hide layout templates */
        .portal-sidebar, 
        .portal-topbar, 
        .no-print, 
        header,
        button,
        .btn,
        form {
            display: none !important;
        }
        body, 
        .portal-main,
        .max-w-6xl {
            padding: 0 !important;
            margin: 0 !important;
            background: #ffffff !important;
            color: #000000 !important;
            width: 100% !important;
            max-width: 100% !important;
        }
        .page-break-avoid {
            page-break-inside: avoid;
        }
        .card-print {
            border: 1px solid #e2e8f0 !important;
            box-shadow: none !important;
            background: #ffffff !important;
        }
    }
</style>

<div class="max-w-6xl mx-auto space-y-6">

    <!-- ── LOCKED STATE ─────────────────────────────────────────────────── -->
    <?php if ($isLocked): ?>
        <div class="bg-white border border-slate-100 p-8 rounded-3xl text-center max-w-lg mx-auto shadow-sm space-y-5 mt-10">
            <div class="w-16 h-16 bg-amber-50 text-amber-500 rounded-full flex items-center justify-center mx-auto text-2xl">
                <i class="fa-solid fa-lock"></i>
            </div>
            <div class="space-y-2">
                <h2 class="text-lg font-black text-slate-800">Wedding Report Unavailable</h2>
                <p class="text-xs text-slate-500 leading-relaxed">
                    This report compiles check-in records, final expenses, and messages, and will become active after your wedding has passed.
                </p>
            </div>
            <div class="border-t border-slate-50 pt-5 space-y-4">
                <p class="text-[10px] text-slate-400 font-bold uppercase tracking-wider">Are you ready to lock details early?</p>
                <form method="POST" action="<?= url('/app/couple/report/complete') ?>">
                    <button type="submit" class="btn btn-primary w-full py-2.5 text-xs font-black shadow-sm">
                        Mark Wedding as Completed
                    </button>
                </form>
            </div>
        </div>

    <!-- ── ACTIVE REPORT ────────────────────────────────────────────────── -->
    <?php else: ?>
        
        <!-- Header Controls -->
        <div class="flex flex-col md:flex-row md:items-center md:justify-between gap-4 no-print">
            <div>
                <div class="page-breadcrumb">
                    <a href="<?= url('/app/couple/dashboard') ?>">Dashboard</a>
                    <span class="sep">/</span>
                    <span>Summary Report</span>
                </div>
                <h1 class="page-h1">
                    <span class="page-header-icon">
                        <i class="fa-solid fa-file-invoice"></i>
                    </span>
                    Wedding Summary Report
                </h1>
                <p class="page-subtitle">Your post-wedding summary — financials, guests, and memories.</p>
            </div>
            <div class="flex items-center gap-2">
                <span class="inline-flex items-center gap-1.5 px-3 py-1.5 text-xs font-semibold rounded-xl border border-transparent" style="background-color:var(--brand-rose-gold-lt); color:var(--color-secondary-600);">
                    Post-Wedding
                </span>
                <button onclick="window.print()" class="btn btn-primary btn-md flex items-center gap-1.5">
                    <i class="fa-solid fa-print"></i> Print / Save PDF
                </button>
            </div>
        </div>

        <div class="grid grid-cols-1 lg:grid-cols-3 gap-6">

            <!-- COLUMN 1: LEFT -->
            <div class="lg:col-span-2 space-y-6">

                <!-- SECTION 1: WEDDING SUMMARY -->
                <div class="bg-white p-6 rounded-3xl border border-slate-100 shadow-sm card-print space-y-4">
                    <h3 class="font-black text-slate-800 text-sm flex items-center gap-2 border-b border-slate-50 pb-3">
                        <i class="fa-solid fa-heart text-pink-500"></i> Wedding Summary
                    </h3>
                    <div class="grid grid-cols-1 sm:grid-cols-2 gap-4 text-xs">
                        <div>
                            <span class="text-slate-450 block font-bold text-[9px] uppercase tracking-wider">Bride &amp; Groom</span>
                            <span class="font-extrabold text-slate-800 text-base mt-0.5 block"><?= escape_html($workspace['groom_name']) ?> &amp; <?= escape_html($workspace['bride_name']) ?></span>
                        </div>
                        <div>
                            <span class="text-slate-450 block font-bold text-[9px] uppercase tracking-wider">Date &amp; Schedule</span>
                            <span class="font-extrabold text-slate-800 mt-0.5 block"><?= date('d F Y', strtotime($workspace['wedding_date'])) ?></span>
                        </div>
                        <div>
                            <span class="text-slate-450 block font-bold text-[9px] uppercase tracking-wider">Wedding Venue</span>
                            <span class="font-extrabold text-slate-800 mt-0.5 block"><?= escape_html($venue_name) ?></span>
                        </div>
                        <div>
                            <span class="text-slate-450 block font-bold text-[9px] uppercase tracking-wider">Operational Status</span>
                            <span class="font-bold text-indigo-700 mt-0.5 block uppercase text-[10px]">
                                <?= $workspace['workspace_status'] === 'post_event' ? 'Completed' : 'Wedding Concluded' ?>
                                <?php if ($workspace['wedding_day_at']): ?>
                                    · <?= date('d M Y', strtotime($workspace['wedding_day_at'])) ?>
                                <?php endif; ?>
                            </span>
                        </div>
                    </div>
                </div>

                <!-- SECTION 2 & 3: GUEST ATTENDANCE & WEDDING DAY SUMMARY -->
                <div class="bg-white p-6 rounded-3xl border border-slate-100 shadow-sm card-print space-y-5 page-break-avoid">
                    <h3 class="font-black text-slate-800 text-sm flex items-center gap-2 border-b border-slate-50 pb-3">
                        <i class="fa-solid fa-clipboard-user" style="color:var(--brand-rose-gold);"></i> Guest Registry &amp; Attendance
                    </h3>

                    <!-- Statistics Roll-up -->
                    <div class="grid grid-cols-2 sm:grid-cols-4 gap-3 text-center">
                        <div class="p-3 bg-slate-50 border border-slate-100 rounded-2xl">
                            <span class="text-[8px] font-bold text-slate-450 block uppercase tracking-wider">Invited Pax</span>
                            <span class="text-lg font-black text-slate-800 block mt-1"><?= $total_invited ?></span>
                        </div>
                        <div class="p-3 bg-slate-50 border border-slate-100 rounded-2xl">
                            <span class="text-[8px] font-bold text-slate-450 block uppercase tracking-wider">RSVP Confirmed</span>
                            <span class="text-lg font-black block mt-1" style="color:var(--brand-rose-gold);"><?= $confirmed ?></span>
                        </div>
                        <div class="p-3 bg-slate-50 border border-slate-100 rounded-2xl">
                            <span class="text-[8px] font-bold text-slate-450 block uppercase tracking-wider">Checked In</span>
                            <span class="text-lg font-black text-emerald-600 block mt-1"><?= $checked_in ?></span>
                        </div>
                        <div class="p-3 bg-slate-50 border border-slate-100 rounded-2xl">
                            <span class="text-[8px] font-bold text-slate-450 block uppercase tracking-wider">Attendance Rate</span>
                            <span class="text-lg font-black text-slate-800 block mt-1"><?= number_format($attendance_percent, 1) ?>%</span>
                        </div>
                    </div>

                    <!-- Flow breakdowns -->
                    <div class="grid grid-cols-1 sm:grid-cols-3 gap-4 text-xs pt-2">
                        <div class="p-3 border border-slate-100 rounded-2xl">
                            <span class="text-slate-450 block font-bold text-[8px] uppercase tracking-wider">Walk-In Records</span>
                            <span class="font-black text-slate-800 mt-1 block"><?= $walk_ins ?> Guests</span>
                        </div>
                        <div class="p-3 border border-slate-100 rounded-2xl">
                            <span class="text-slate-450 block font-bold text-[8px] uppercase tracking-wider">No Show Rate</span>
                            <span class="font-black text-slate-800 mt-1 block"><?= $no_show ?> Guests</span>
                        </div>
                        <div class="p-3 border border-slate-100 rounded-2xl">
                            <span class="text-slate-450 block font-bold text-[8px] uppercase tracking-wider">Peak Hour Window</span>
                            <span class="font-black mt-1 block" style="color:var(--brand-rose-gold);"><?= $peak_checkin_hour ?></span>
                        </div>
                    </div>

                    <div class="grid grid-cols-1 sm:grid-cols-2 gap-4 text-xs">
                        <div>
                            <span class="text-slate-400 block font-bold text-[8px] uppercase tracking-wider">First Gate Check-In</span>
                            <span class="font-bold text-slate-700 mt-0.5 block"><?= $first_checkin ? date('d M Y, H:i:s', strtotime($first_checkin)) : '—' ?></span>
                        </div>
                        <div>
                            <span class="text-slate-400 block font-bold text-[8px] uppercase tracking-wider">Last Gate Check-In</span>
                            <span class="font-bold text-slate-700 mt-0.5 block"><?= $last_checkin ? date('d M Y, H:i:s', strtotime($last_checkin)) : '—' ?></span>
                        </div>
                    </div>
                </div>

                <!-- SECTION 4: BUDGET PLANNER SUMMARY -->
                <div class="bg-white p-6 rounded-3xl border border-slate-100 shadow-sm card-print space-y-4 page-break-avoid">
                    <h3 class="font-black text-slate-800 text-sm flex items-center gap-2 border-b border-slate-50 pb-3">
                        <i class="fa-solid fa-wallet" style="color:var(--brand-rose-gold);"></i> Financial Summary
                    </h3>

                    <div class="grid grid-cols-2 sm:grid-cols-4 gap-3 text-center">
                        <div class="p-3 bg-slate-50 border border-slate-100 rounded-2xl">
                            <span class="text-[8px] font-bold text-slate-450 block uppercase tracking-wider">Target Budget</span>
                            <span class="text-base font-black text-slate-800 block mt-1"><?= format_money($total_budget, $currency) ?></span>
                        </div>
                        <div class="p-3 bg-slate-50 border border-slate-100 rounded-2xl">
                            <span class="text-[8px] font-bold text-slate-450 block uppercase tracking-wider">Negotiated Deals</span>
                            <span class="text-base font-black block mt-1" style="color:var(--brand-rose-gold);"><?= format_money($actual_deal_value, $currency) ?></span>
                        </div>
                        <div class="p-3 bg-slate-50 border border-slate-100 rounded-2xl">
                            <span class="text-[8px] font-bold text-slate-450 block uppercase tracking-wider">Paid Amount</span>
                            <span class="text-base font-black text-emerald-600 block mt-1"><?= format_money($paid, $currency) ?></span>
                        </div>
                        <div class="p-3 bg-slate-50 border border-slate-100 rounded-2xl">
                            <span class="text-[8px] font-bold text-slate-450 block uppercase tracking-wider">Remaining Balance</span>
                            <span class="text-base font-black text-slate-800 block mt-1"><?= format_money($remaining_cash, $currency) ?></span>
                        </div>
                    </div>

                    <div class="flex justify-between items-center p-4 bg-slate-50 border border-slate-100 rounded-2xl text-xs">
                        <div>
                            <span class="text-slate-450 font-bold uppercase text-[8px]">Overall Variance Room</span>
                            <span class="text-base font-black text-slate-800 mt-0.5 block"><?= format_money(abs($budget_difference), $currency) ?></span>
                        </div>
                        <span class="px-3.5 py-1 bg-white border border-slate-100 rounded-xl font-bold uppercase text-[10px] text-slate-700">
                            <?= $budget_difference >= 0 ? 'Under Budget (Saving)' : 'Over Budget (Overrun)' ?>
                        </span>
                    </div>
                </div>

            </div>

            <!-- COLUMN 2: RIGHT -->
            <div class="space-y-6">

                <!-- SECTION 5: TIMELINE SUMMARY -->
                <div class="bg-white p-6 rounded-3xl border border-slate-100 shadow-sm card-print space-y-4">
                    <h3 class="font-black text-slate-800 text-sm flex items-center gap-2 border-b border-slate-50 pb-3">
                        <i class="fa-solid fa-clock-rotate-left" style="color:var(--brand-rose-gold);"></i> Timeline Progress
                    </h3>
                    <div class="space-y-3 text-xs">
                        <div class="flex justify-between">
                            <span class="text-slate-500">Total Tasks Created</span>
                            <strong class="text-slate-850"><?= $total_tasks ?></strong>
                        </div>
                        <div class="flex justify-between">
                            <span class="text-slate-500">Completed Checklist</span>
                            <strong class="text-slate-850"><?= $tasks_completed ?></strong>
                        </div>
                        <div class="flex justify-between">
                            <span class="text-slate-500">Outstanding Tasks</span>
                            <strong class="text-slate-850"><?= $tasks_remaining ?></strong>
                        </div>
                        <div class="flex justify-between">
                            <span class="text-slate-500">Overdue Milestones</span>
                            <strong class="text-rose-600"><?= $tasks_overdue ?></strong>
                        </div>
                        <div class="border-t border-slate-50 pt-2.5 flex justify-between items-center">
                            <span class="font-bold text-[10px] text-slate-450 uppercase">Completion Rate</span>
                            <strong class="text-sm font-black text-slate-800"><?= number_format($timeline_percent, 1) ?>%</strong>
                        </div>
                    </div>
                </div>

                <!-- SECTION 6: VENDOR SUPPORT SUMMARY -->
                <div class="bg-white p-6 rounded-3xl border border-slate-100 shadow-sm card-print space-y-4 page-break-avoid">
                    <h3 class="font-black text-slate-800 text-sm flex items-center gap-2 border-b border-slate-50 pb-3">
                        <i class="fa-solid fa-handshake" style="color:var(--brand-rose-gold);"></i> Vendor Summary
                    </h3>
                    <div class="space-y-3 text-xs">
                        <div class="flex justify-between">
                            <span class="text-slate-500">Total Profiles Logged</span>
                            <strong class="text-slate-850"><?= $vendors_count ?></strong>
                        </div>
                        <div class="flex justify-between">
                            <span class="text-slate-500">Booked &amp; Contracted</span>
                            <strong style="color:var(--brand-rose-gold);"><?= $assigned_vendors ?></strong>
                        </div>
                        <div class="flex justify-between">
                            <span class="text-slate-500">Fully Settled Payments</span>
                            <strong class="text-emerald-600"><?= $completed_payments ?></strong>
                        </div>
                        <div class="flex justify-between">
                            <span class="text-slate-500">Outstanding Installments</span>
                            <strong class="text-slate-800"><?= $outstanding_payments ?></strong>
                        </div>
                        <?php if ($largest_vendor_expense): ?>
                            <div class="border-t border-slate-50 pt-2.5">
                                <span class="text-[8px] font-bold text-slate-400 block uppercase">Largest Vendor Contract</span>
                                <span class="font-bold text-slate-800 block mt-0.5 truncate"><?= escape_html($largest_vendor_expense['name']) ?></span>
                                <span class="font-black text-xs block" style="color:var(--brand-rose-gold);"><?= format_money($largest_vendor_expense['contract_value'], $currency) ?></span>
                            </div>
                        <?php endif; ?>
                    </div>
                </div>

            </div>

        </div>

        <!-- ── SECTION 8: FACTUAL HIGHLIGHTS ────────────────────────────────── -->
        <div class="bg-white p-6 rounded-3xl border border-slate-100 shadow-sm card-print space-y-4 page-break-avoid">
            <h3 class="font-black text-slate-800 text-sm flex items-center gap-2 border-b border-slate-50 pb-3">
                <i class="fa-solid fa-award" style="color:var(--brand-rose-gold);"></i> Factual Highlights
            </h3>
            <div class="grid grid-cols-1 sm:grid-cols-2 md:grid-cols-5 gap-4 text-xs">
                <?php if ($largest_expense_cat): ?>
                    <div class="p-3 border border-slate-100 rounded-2xl">
                        <span class="text-slate-400 block font-bold text-[8px] uppercase tracking-wider">Top Allocated Category</span>
                        <strong class="text-slate-700 block mt-1 truncate"><?= escape_html($largest_expense_cat['name']) ?></strong>
                        <span class="text-[10px] font-bold block mt-0.5" style="color:var(--brand-rose-gold);"><?= format_money($largest_expense_cat['total_est'], $currency) ?></span>
                    </div>
                <?php endif; ?>

                <?php if ($largest_payment): ?>
                    <div class="p-3 border border-slate-100 rounded-2xl">
                        <span class="text-slate-400 block font-bold text-[8px] uppercase tracking-wider">Largest Paid Milestone</span>
                        <strong class="text-slate-700 block mt-1"><?= format_money($largest_payment['amount'], $currency) ?></strong>
                        <span class="text-[8px] text-slate-400 block mt-0.5">Paid: <?= date('d M Y', strtotime($largest_payment['payment_date'])) ?></span>
                    </div>
                <?php endif; ?>

                <?php if ($highest_rsvp_group): ?>
                    <div class="p-3 border border-slate-100 rounded-2xl">
                        <span class="text-slate-400 block font-bold text-[8px] uppercase tracking-wider">Highest RSVP Group</span>
                        <strong class="text-slate-700 block mt-1 capitalize"><?= escape_html($highest_rsvp_group['guest_group']) ?></strong>
                        <span class="text-[10px] text-emerald-650 font-bold block mt-0.5"><?= $highest_rsvp_group['yes_cnt'] ?> Attending</span>
                    </div>
                <?php endif; ?>

                <?php if ($latest_payment): ?>
                    <div class="p-3 border border-slate-100 rounded-2xl">
                        <span class="text-slate-400 block font-bold text-[8px] uppercase tracking-wider">Most Recent Payment</span>
                        <strong class="text-slate-700 block mt-1 truncate"><?= escape_html($latest_payment['name']) ?></strong>
                        <span class="text-[10px] text-slate-500 font-bold block mt-0.5"><?= format_money($latest_payment['amount'], $currency) ?></span>
                    </div>
                <?php endif; ?>

                <?php if ($latest_task): ?>
                    <div class="p-3 border border-slate-100 rounded-2xl">
                        <span class="text-slate-400 block font-bold text-[8px] uppercase tracking-wider">Latest Task Completed</span>
                        <strong class="text-slate-700 block mt-1 truncate"><?= escape_html($latest_task['title']) ?></strong>
                        <span class="text-[8px] text-slate-400 block mt-0.5"><?= date('d M Y', strtotime($latest_task['act_date'])) ?></span>
                    </div>
                <?php endif; ?>
            </div>
        </div>

        <!-- ── SECTION 7: GUEST ENGAGEMENT (WISHES) ─────────────────────────── -->
        <div class="bg-white p-6 rounded-3xl border border-slate-100 shadow-sm card-print space-y-4 page-break-avoid">
            <div class="flex justify-between items-center border-b border-slate-50 pb-3">
                <h3 class="font-black text-slate-800 text-sm flex items-center gap-2">
                    <i class="fa-solid fa-comments" style="color:var(--brand-rose-gold);"></i> Guest Engagement (Wishes Wall)
                </h3>
                <span class="text-xs font-bold px-2.5 py-0.5 rounded-lg border border-transparent" style="background-color:var(--brand-rose-gold-lt); color:var(--color-secondary-600);">
                    Total Wishes: <?= $wishes_count ?>
                </span>
            </div>

            <?php if (empty($latest_wishes)): ?>
                <div class="text-center py-8 text-slate-400 text-xs">
                    No guest wishes recorded.
                </div>
            <?php else: ?>
                <div class="grid grid-cols-1 md:grid-cols-2 gap-4">
                    <?php foreach ($latest_wishes as $w): ?>
                        <div class="p-3.5 border border-slate-100 rounded-2xl bg-slate-50/50 text-xs flex flex-col justify-between space-y-2">
                            <p class="text-slate-650 italic leading-relaxed">
                                "<?= escape_html($w['message']) ?>"
                            </p>
                            <div class="flex justify-between items-center border-t border-slate-50 pt-2 text-[10px] text-slate-450">
                                <strong class="text-slate-700"><?= escape_html($w['full_name']) ?></strong>
                                <span><?= date('d M Y, H:i', strtotime($w['created_at'])) ?></span>
                            </div>
                        </div>
                    <?php endforeach; ?>
                </div>
            <?php endif; ?>
        </div>

    <?php endif; ?>

</div>
