<?php

require_once __DIR__ . '/auth.php';
require_once __DIR__ . '/settings.php';

sparrow_start_session();

function sparrow_public_settings(): array
{
    static $settings = null;

    if ($settings === null) {
        $settings = sparrow_get_settings(sparrow_db());
    }

    return $settings;
}

function sparrow_public_setting(string $key, string $default = ''): string
{
    $settings = sparrow_public_settings();
    return isset($settings[$key]) ? (string) $settings[$key] : $default;
}

function sparrow_public_user_is_admin(): bool
{
    return !empty($_SESSION['sparrow_backyard_user_id'])
        || !empty($_SESSION['backyard_user_id'])
        || !empty($_SESSION['sparrow_user_id']);
}

function sparrow_public_user_is_customer(): bool
{
    return !empty($_SESSION['customer_portal_account_id'])
        || !empty($_SESSION['sparrow_customer_id'])
        || !empty($_SESSION['customer_id']);
}

function sparrow_public_nav_link(string $url, string $label, string $active_key, string $current_key): void
{
    $is_active = $active_key === $current_key;
    ?>
    <a
        class="public-sidebar-link <?php echo $is_active ? 'is-active' : ''; ?>"
        href="<?php echo sparrow_escape(sparrow_url($url)); ?>"
    >
        <?php echo sparrow_escape($label); ?>
    </a>
    <?php
}

function sparrow_public_header(string $page_title, string $active_key = '', string $extra_css = ''): void
{
    $business_name = sparrow_public_setting('business_name', 'Sparrow & Seed Creations');
    $announcement = trim(sparrow_public_setting('site_announcement'));
    ?>
    <!doctype html>
    <html lang="en">
    <head>
        <meta charset="utf-8">
        <title><?php echo sparrow_escape($page_title); ?> | <?php echo sparrow_escape($business_name); ?></title>
        <meta name="viewport" content="width=device-width, initial-scale=1">

        <style>
            :root {
                --sparrow-bg: #fbf7fa;
                --sparrow-card: #ffffff;
                --sparrow-border: #ead7e5;
                --sparrow-text: #352733;
                --sparrow-muted: #725d6d;
                --sparrow-purple: #9b5c8f;
                --sparrow-purple-dark: #864b7b;
                --sparrow-soft-pink: #f9f2f7;
            }

            * {
                box-sizing: border-box;
            }

            body {
                margin: 0;
                font-family: Arial, sans-serif;
                background: var(--sparrow-bg);
                color: var(--sparrow-text);
            }

            a {
                color: var(--sparrow-purple-dark);
            }

            .public-layout {
                min-height: 100vh;
                display: grid;
                grid-template-columns: 235px 1fr;
            }

            .public-sidebar {
                background: #ffffff;
                border-right: 1px solid var(--sparrow-border);
                padding: 20px 14px;
                position: sticky;
                top: 0;
                height: 100vh;
                overflow-y: auto;
            }

            .public-brand {
                display: block;
                text-decoration: none;
                padding: 12px 10px 18px;
                margin-bottom: 12px;
                border-bottom: 1px solid var(--sparrow-border);
            }

            .public-brand-title {
                display: block;
                color: var(--sparrow-purple-dark);
                font-size: 20px;
                font-weight: bold;
                line-height: 1.15;
            }

            .public-brand-subtitle {
                display: block;
                margin-top: 6px;
                color: var(--sparrow-muted);
                font-size: 12px;
                line-height: 1.35;
            }

            .public-sidebar-nav {
                display: grid;
                gap: 8px;
            }

            .public-sidebar-link {
                display: block;
                padding: 10px 12px;
                border-radius: 999px;
                text-decoration: none;
                color: var(--sparrow-purple-dark);
                font-weight: bold;
                font-size: 14px;
                border: 1px solid transparent;
            }

            .public-sidebar-link:hover,
            .public-sidebar-link.is-active {
                background: var(--sparrow-soft-pink);
                border-color: var(--sparrow-border);
            }

            .public-sidebar-note {
                margin-top: 22px;
                padding: 13px;
                border-radius: 18px;
                background: var(--sparrow-soft-pink);
                color: var(--sparrow-muted);
                font-size: 12px;
                line-height: 1.45;
                border: 1px solid var(--sparrow-border);
            }

            .public-main-wrap {
                min-width: 0;
                display: flex;
                flex-direction: column;
                min-height: 100vh;
            }

            .public-topbar {
                background: rgba(255, 255, 255, 0.82);
                border-bottom: 1px solid var(--sparrow-border);
                padding: 14px 22px;
                position: sticky;
                top: 0;
                z-index: 50;
                backdrop-filter: blur(8px);
            }

            .public-topbar-inner {
                max-width: 1180px;
                margin: 0 auto;
                display: flex;
                align-items: center;
                justify-content: space-between;
                gap: 12px;
            }

            .public-topbar-title {
                font-weight: bold;
                color: var(--sparrow-purple-dark);
            }

            .public-topbar-tagline {
                color: var(--sparrow-muted);
                font-size: 13px;
            }

            .public-content {
                width: 100%;
                max-width: 1180px;
                margin: 0 auto;
                padding: 28px 18px 50px;
                flex: 1;
            }

            .public-card {
                background: var(--sparrow-card);
                border: 1px solid var(--sparrow-border);
                border-radius: 22px;
                padding: 28px;
                box-shadow: 0 8px 30px rgba(70, 35, 60, 0.06);
            }

            .public-hero {
                text-align: center;
                margin-bottom: 22px;
            }

            .public-hero h1 {
                margin: 0 0 10px;
                color: var(--sparrow-purple-dark);
            }

            .public-hero p {
                margin: 0;
                color: var(--sparrow-muted);
                line-height: 1.5;
            }

            .public-footer {
                border-top: 1px solid var(--sparrow-border);
                background: #ffffff;
                padding: 18px;
                text-align: center;
                color: var(--sparrow-muted);
                font-size: 13px;
            }

            .public-announcement {
                padding: 12px 18px;
                background: var(--sparrow-soft-pink);
                border-bottom: 1px solid var(--sparrow-border);
                color: var(--sparrow-purple-dark);
                text-align: center;
                font-weight: bold;
                line-height: 1.45;
            }

            .public-footer-details {
                margin-top: 5px;
                font-size: 12px;
            }
			
			.public-footer-links {
    margin-top: 7px;
}

.public-footer-links a {
    color: var(--sparrow-purple-dark);
    font-weight: bold;
    text-decoration: none;
}

.public-footer-links a:hover {
    text-decoration: underline;
}

            @media (max-width: 820px) {
                .public-layout {
                    grid-template-columns: 1fr;
                }
				
				.public-topbar-actions {
    justify-content: center;
    margin-top: 10px;
}

                .public-sidebar {
                    position: static;
                    height: auto;
                    border-right: 0;
                    border-bottom: 1px solid var(--sparrow-border);
                }

                .public-brand {
                    text-align: center;
                }

                .public-sidebar-nav {
                    display: flex;
                    flex-wrap: wrap;
                    justify-content: center;
                }

                .public-sidebar-link {
                    font-size: 13px;
                    padding: 8px 11px;
                }

                .public-sidebar-note {
                    display: none;
                }

                .public-topbar {
                    position: static;
                }

                .public-topbar-inner {
                    display: block;
                    text-align: center;
                }

                .public-topbar-tagline {
                    margin-top: 4px;
                }
            }
			
			.public-topbar-actions {
    display: flex;
    gap: 8px;
    align-items: center;
    flex-wrap: wrap;
    justify-content: flex-end;
}

.public-topbar-link {
    display: inline-block;
    padding: 7px 12px;
    border-radius: 999px;
    border: 1px solid var(--sparrow-border);
    background: #ffffff;
    color: var(--sparrow-purple-dark);
    text-decoration: none;
    font-weight: bold;
    font-size: 13px;
}

.public-topbar-link:hover {
    background: var(--sparrow-soft-pink);
}

.reference-image-grid {
    display: grid;
    grid-template-columns: repeat(
        auto-fit,
        minmax(220px, 1fr)
    );
    gap: 16px;
}

.reference-image-field {
    padding: 14px;
    background: #fffafd;
    border: 1px solid #eadde7;
    border-radius: 10px;
}

.reference-image-field label {
    display: block;
    margin-bottom: 8px;
    font-weight: 600;
}

.reference-image-field input[type="file"] {
    width: 100%;
}

            <?php echo $extra_css; ?>
        </style>
    </head>

    <body>
        <div class="public-layout">
            <aside class="public-sidebar">
                <a class="public-brand" href="<?php echo sparrow_escape(sparrow_url('/index.php')); ?>">
                    <span class="public-brand-title"><?php echo sparrow_escape($business_name); ?></span>
                    <span class="public-brand-subtitle">Custom creations, gifts, and handmade work</span>
                </a>

<nav class="public-sidebar-nav">
    <?php sparrow_public_nav_link('/index.php', 'Home', 'home', $active_key); ?>
	<?php sparrow_public_nav_link('/about.php', 'About', 'about', $active_key); ?>
	<?php sparrow_public_nav_link('/blog.php', 'Product Stories', 'blog', $active_key); ?>
	<?php sparrow_public_nav_link('/shop.php', 'Shop', 'shop', $active_key); ?>
    <?php sparrow_public_nav_link('/order.php', 'Order Form', 'order', $active_key); ?>
    <?php sparrow_public_nav_link('/customer-portal.php', 'Customer Portal', 'portal', $active_key); ?>
    <?php sparrow_public_nav_link('/gallery.php', 'Gallery', 'gallery', $active_key); ?>
	<?php sparrow_public_nav_link('/contact.php', 'Contact', 'contact', $active_key); ?>
</nav>
            </aside>

                        <div class="public-main-wrap">
                <header class="public-topbar">
                    <div class="public-topbar-inner">
                        <div>
                            <div class="public-topbar-title">
                                <?php echo sparrow_escape($business_name); ?>
                            </div>

                            <div class="public-topbar-tagline">
                                Faith, gifts, custom work, and a little handmade joy.
                            </div>
                        </div>

                        <div class="public-topbar-actions">
<?php if (sparrow_public_user_is_admin()): ?>
    <a
        class="public-topbar-link"
        href="<?php echo sparrow_escape(sparrow_url('/backyard/dashboard.php')); ?>"
    >
        Backyard
    </a>

    <a
        class="public-topbar-link"
        href="<?php echo sparrow_escape(sparrow_url('/logout.php')); ?>"
    >
        Logout
    </a>
<?php elseif (sparrow_public_user_is_customer()): ?>
    <a
        class="public-topbar-link"
        href="<?php echo sparrow_escape(sparrow_url('/customer-portal.php')); ?>"
    >
        Customer Portal
    </a>

    <a
        class="public-topbar-link"
        href="<?php echo sparrow_escape(sparrow_url('/logout.php')); ?>"
    >
        Logout
    </a>
<?php else: ?>
    <a
        class="public-topbar-link"
        href="<?php echo sparrow_escape(sparrow_url('/login.php')); ?>"
    >
        Login
    </a>
<?php endif; ?>
                        </div>
                    </div>
                </header>

                <?php if ($announcement !== ''): ?>
                    <div class="public-announcement">
                        <?php echo nl2br(sparrow_escape($announcement)); ?>
                    </div>
                <?php endif; ?>

                <main class="public-content">
    <?php
}

function sparrow_public_footer(): void
{
    $business_name = sparrow_public_setting('business_name', 'Sparrow & Seed Creations');
    $footer_text = trim(sparrow_public_setting('site_footer_text'));
    $contact_parts = array_values(array_filter([
        trim(sparrow_public_setting('contact_email')),
        trim(sparrow_public_setting('contact_phone')),
        trim(sparrow_public_setting('facebook')),
        trim(sparrow_public_setting('instagram')),
    ]));
    ?>
                </main>

                <footer class="public-footer">
                    &copy; <?php echo date('Y'); ?> <?php echo sparrow_escape($business_name); ?>.
                    <?php if ($footer_text !== ''): ?>
                        <?php echo nl2br(sparrow_escape($footer_text)); ?>
                    <?php endif; ?>
					
					<div class="public-footer-links">
    <a href="<?php echo sparrow_escape(sparrow_url('/privacy-policy.php')); ?>">
        Privacy Policy
    </a>
    <span aria-hidden="true"> • </span>
    <a href="<?php echo sparrow_escape(sparrow_url('/refund-return-policy.php')); ?>">
        Refund &amp; Return Policy
    </a>
</div>
                    <?php if ($contact_parts): ?>
                        <div class="public-footer-details">
                            <?php echo sparrow_escape(implode(' • ', $contact_parts)); ?>
                        </div>
                    <?php endif; ?>
                </footer>
            </div>
        </div>
    </body>
    </html>
    <?php
}
