<?php

require_once __DIR__ . '/app/helpers.php';
require_once __DIR__ . '/app/analytics.php';
require_once __DIR__ . '/app/public-layout.php';
require_once __DIR__ . '/app/shop-products.php';

$business_name = sparrow_public_setting('business_name', 'Sparrow & Seed Creations');
$homepage_heading = trim(sparrow_public_setting('homepage_heading', 'Custom creations made with care'));
$homepage_intro = trim(sparrow_public_setting(
    'homepage_intro',
    'Custom shirts, koozies, gifts, and faith-filled designs made with care in Texas.'
));
$featured_count = max(1, min(20, (int) sparrow_public_setting('shop_featured_count', '4')));

sparrow_record_analytics_event('page_view', 'page', null, 'Home', '', '/');

$home_css = <<<'CSS'
.home-welcome {
    display: grid;
    gap: 16px;
}

.home-welcome p {
    margin: 0;
    color: var(--sparrow-muted);
    line-height: 1.65;
}

.home-scripture-grid {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 14px;
    margin-top: 18px;
}

.home-scripture-card {
    background: var(--sparrow-soft-pink);
    border: 1px solid var(--sparrow-border);
    border-radius: 18px;
    padding: 16px;
}

.home-scripture-card p {
    margin: 0;
    color: var(--sparrow-purple-dark);
    font-weight: bold;
}

.home-scripture-card small {
    display: block;
    margin-top: 6px;
    color: var(--sparrow-muted);
    font-weight: bold;
}

.home-side-grid {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 18px;
    margin-top: 18px;
}

.home-info-card {
    background: linear-gradient(135deg, #ffffff, var(--sparrow-bg-soft));
    border: 1px solid var(--sparrow-border);
    border-left: 5px solid var(--sparrow-pink);
    border-radius: 18px;
    padding: 20px;
    box-shadow: 0 2px 12px rgba(125, 90, 106, 0.08);
}

.home-info-card h2 {
    margin: 0 0 10px;
    color: var(--sparrow-purple-dark);
    font-size: 22px;
    line-height: 1.2;
}

.home-info-card p {
    margin: 0 0 14px;
    color: var(--sparrow-muted);
    line-height: 1.55;
}

.home-list {
    list-style: none;
    padding: 0;
    margin: 0 0 18px;
}

.home-list li {
    position: relative;
    padding-left: 26px;
    margin-bottom: 9px;
    color: var(--sparrow-muted);
}

.home-list li::before {
    position: absolute;
    left: 0;
    color: var(--sparrow-pink);
    font-weight: bold;
}

.home-button-row {
    display: flex;
    flex-wrap: wrap;
    gap: 10px;
}

.home-featured-grid {
    display: grid;
    grid-template-columns: repeat(4, 1fr);
    gap: 14px;
    margin-top: 16px;
}

.home-product-card {
    background: var(--sparrow-card);
    border: 1px solid var(--sparrow-border);
    border-radius: 18px;
    padding: 12px;
    box-shadow: 0 8px 24px rgba(70, 35, 60, 0.05);
}

.home-product-image {
    width: 100%;
    aspect-ratio: 1 / 1;
    border-radius: 18px;
    border: 1px dashed var(--sparrow-border);
    background: var(--sparrow-soft-pink);
    display: flex;
    align-items: center;
    justify-content: center;
    color: var(--sparrow-muted);
    text-align: center;
    padding: 12px;
    font-size: 13px;
    margin-bottom: 12px;
}

.home-product-card h3 {
    margin: 0 0 6px;
    color: var(--sparrow-purple-dark);
    font-size: 15px;
    line-height: 1.2;
}

.home-product-card p {
    margin: 0;
    color: var(--sparrow-muted);
    line-height: 1.4;
    font-size: 13px;
}

.home-product-image-link,
.home-product-title-link {
    color: inherit;
    text-decoration: none;
}

.home-product-title-link:hover {
    color: var(--sparrow-pink);
    text-decoration: none;
}

.home-product-card .home-product-image {
    width: 100%;
    height: 120px;
    max-height: 120px;
    object-fit: cover;
    border-radius: 14px;
    display: block;
}

@media (max-width: 1000px) {
    .home-featured-grid {
        grid-template-columns: repeat(3, 1fr);
    }
}

@media (max-width: 750px) {
    .home-featured-grid {
        grid-template-columns: repeat(2, 1fr);
    }

    .home-side-grid,
    .home-scripture-grid {
        grid-template-columns: 1fr !important;
    }

    .home-info-card {
        width: 100%;
        min-width: 0;
    }
}

@media (max-width: 480px) {
    .home-featured-grid {
        grid-template-columns: 1fr;
    }
}

.home-section-space {
    margin-top: 18px;
}

.home-quote-button {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    padding: 9px 16px;
    border-radius: 999px;
    background: var(--sparrow-soft-pink);
    border: 1px solid var(--sparrow-border);
    color: var(--sparrow-purple-dark);
    font-weight: bold;
    font-size: 13px;
    text-decoration: none;
}

.home-quote-button:hover {
    background: #ffffff;
    text-decoration: none;
}

.public-card h1,
.public-card h2,
.public-card h3 {
    color: var(--sparrow-purple-dark);
}

.public-card {
    border-left: 5px solid var(--sparrow-pink);
    box-shadow: 0 2px 12px rgba(125, 90, 106, 0.08);
}

.public-card h1::before,
.public-card h2::before {
    display: inline-block;
    margin-right: 8px;
    color: var(--sparrow-pink);
    font-size: 0.72em;
    vertical-align: 2px;
}

.home-info-card img {
    max-width: 100%;
    height: auto;
    border-radius: 14px;
}
CSS;

$featured_products = sparrow_get_featured_shop_products($featured_count, true);
sparrow_public_header('Home', 'home', $home_css);

?>

<section class="public-card public-hero">
    <h1><?php echo sparrow_escape($homepage_heading); ?></h1>
    <p>
        <?php echo nl2br(sparrow_escape($homepage_intro)); ?>
    </p>
</section>

<section class="public-card home-welcome">
    <h2>Faith-filled creations with purpose</h2>

    <p>
        Welcome to <?php echo sparrow_escape($business_name); ?>, a faith-driven small business rooted in Scripture,
        prayer, and a deep belief in God’s love and care for every single person.
    </p>

    <p>
        Our name is inspired by Matthew 10:31, reminding us that we are worth more than many sparrows
        and are never overlooked by God, and Luke 17:6, which teaches that even faith as small as a
        mustard seed can do extraordinary things.
    </p>

    <p>
        Each piece we create is more than just a product. It is a reflection of faith, intention,
        and craftsmanship. Every item is thoughtfully created with prayerful hands and a heart for
        encouragement.
    </p>

    <p>
        From meaningful awareness designs to joyful seasonal favorites, every item is made to bring
        a little hope, joy, and purpose into everyday moments.
    </p>

    <div class="home-scripture-grid">
        <div class="home-scripture-card">
            <p>“You are worth more than many sparrows.”</p>
            <small>Matthew 10:31</small>
        </div>

        <div class="home-scripture-card">
            <p>“Faith as small as a mustard seed...”</p>
            <small>Luke 17:6</small>
        </div>
    </div>
</section>

<section class="home-side-grid home-section-space">
    <article class="home-info-card">
        <h2>Handmade with care in Texas</h2>

        <p>
            We create faith-inspired, custom, and seasonal items with care, purpose, and a little sparkle.
        </p>

        <ul class="home-list">
            <li>Handmade with care in Texas</li>
            <li>New faith-inspired designs added regularly</li>
            <li>A portion of proceeds from select items supports meaningful causes</li>
        </ul>

        <a
    class="home-quote-button"
    href="<?php echo sparrow_escape(sparrow_url('/order.php')); ?>"
>
    Request a Quote
</a>
    </article>

    <article class="home-info-card">
        <h2>Spirit Radio</h2>

        <p>
            Go check out Spirit Radio, a local faith-filled station connected with my church family.
        </p>

        <p>
            Spirit Radio shares encouragement, music, and faith-filled programming from our local church family.
        </p>

        <a href="https://spiritradio.fm/" target="_blank" rel="noopener"><img src="<?= sparrow_escape(sparrow_url('/storage/spirit-943-logo.png')) ?>" border="0" width="280" height="160" alt="Spirit Radio 94.3 FM">
        </a>
    </article>
</section>

<section class="public-card home-section-space">
    <div class="section-heading-row">
        <div>
            <h2>Featured Products</h2>
            <p class="muted">
                A few premade items currently available in the shop.
            </p>
        </div>

        <a class="home-quote-button" href="<?php echo sparrow_escape(sparrow_url('/shop.php')); ?>">
            View Shop
        </a>
    </div>

    <?php if (!$featured_products): ?>
        <p class="muted">
            Featured products will show here soon.
        </p>
    <?php else: ?>
        <div class="home-featured-grid">
            <?php foreach ($featured_products as $product): ?>
                <?php
                    $image_src = '';

                    if (!empty($product['image_path'])) {
                        $image_src = sparrow_url($product['image_path']);
                    } elseif (!empty($product['source_image_urls'])) {
                        $image_urls = explode(',', (string) $product['source_image_urls']);
                        $image_src = trim($image_urls[0] ?? '');
                    }
                ?>

                <article class="home-product-card">
                    <a
                        href="<?php echo sparrow_escape(sparrow_url('/product.php?id=' . (int) $product['id'])); ?>"
                        class="home-product-image-link"
                    >
                        <?php if ($image_src !== ''): ?>
                            <img
                                class="home-product-image"
                                src="<?php echo sparrow_escape($image_src); ?>"
                                alt="<?php echo sparrow_escape($product['product_name']); ?>"
                            >
                        <?php else: ?>
                            <div class="home-product-image">
                                <?php echo sparrow_escape($business_name); ?>
                            </div>
                        <?php endif; ?>
                    </a>

                    <h3>
                        <a
                            class="home-product-title-link"
                            href="<?php echo sparrow_escape(sparrow_url('/product.php?id=' . (int) $product['id'])); ?>"
                        >
                            <?php echo sparrow_escape($product['product_name']); ?>
                        </a>
                    </h3>

                    <p>
                        $<?php echo sparrow_escape(number_format((float) $product['price'], 2)); ?>
                    </p>
                </article>
            <?php endforeach; ?>
        </div>
    <?php endif; ?>
</section>

<section class="public-card home-section-space">
    <h2>Custom Options</h2>

    <p>
        Not seeing what you're looking for, or looking to customize something? Send a request and we can help bring
        your idea to life.
    </p>

    <a
    class="home-quote-button"
    href="<?php echo sparrow_escape(sparrow_url('/order.php')); ?>"
>
    Request a Quote
</a>
</section>

<?php sparrow_public_footer(); ?>
