<?php

/* =========================================================
   COMPLETE BLISS - PUBLIC SCHEME DETAILS
========================================================= */

require_once __DIR__ . '/config/database.php';


/* =========================================================
   GET SCHEME ID
========================================================= */

$schemeId =
    isset($_GET['id'])
        ? (int) $_GET['id']
        : 0;


$scheme = null;


if ($schemeId > 0) {

    $statement =
        $pdo->prepare(
            "
            SELECT
                id,
                plan_name,
                plan_type,
                amount,
                frequency,
                duration,
                description,
                plan_image,
                package_items,
                start_month,
                start_year
            FROM plans
            WHERE
                id = :id
                AND status = 'active'
            LIMIT 1
            "
        );


    $statement->execute([
        'id' => $schemeId
    ]);


    $scheme =
        $statement->fetch(
            PDO::FETCH_ASSOC
        );

}


/* =========================================================
   PREPARE SCHEME DATA
========================================================= */

$packageItems = [];
$schemeImage = '';
$schemeStart = '';


if ($scheme) {

    /* IMAGE */

    if (!empty($scheme['plan_image'])) {

        $schemeImage =
            'assets/uploads/plans/' .
            rawurlencode(
                $scheme['plan_image']
            );

    }


    /* PACKAGE ITEMS */

    if (!empty($scheme['package_items'])) {

        $packageItems =
            preg_split(
                '/\r\n|\r|\n/',
                trim(
                    $scheme['package_items']
                )
            );


        $packageItems =
            array_values(
                array_filter(
                    array_map(
                        'trim',
                        $packageItems
                    )
                )
            );

    }


    /* START DATE */

    $startMonth =
        (int) (
            $scheme['start_month']
            ?? 0
        );


    $startYear =
        (int) (
            $scheme['start_year']
            ?? 0
        );


    if (
        $startMonth >= 1 &&
        $startMonth <= 12 &&
        $startYear > 0
    ) {

        $monthNames = [
            1  => 'January',
            2  => 'February',
            3  => 'March',
            4  => 'April',
            5  => 'May',
            6  => 'June',
            7  => 'July',
            8  => 'August',
            9  => 'September',
            10 => 'October',
            11 => 'November',
            12 => 'December'
        ];


        $schemeStart =
            $monthNames[$startMonth] .
            ' ' .
            $startYear;

    }

}


/* =========================================================
   DYNAMIC SEO
========================================================= */

if ($scheme) {


    /*
    |--------------------------------------------------------------------------
    | Page Title
    |--------------------------------------------------------------------------
    */

    $pageTitle =
        $scheme['plan_name']
        .
        ' | Complete Bliss Solutions';



    /*
    |--------------------------------------------------------------------------
    | Meta Description
    |--------------------------------------------------------------------------
    */

    $cleanDescription =
        trim(
            preg_replace(
                '/\s+/',
                ' ',
                strip_tags(
                    $scheme['description'] ?? ''
                )
            )
        );


    if ($cleanDescription !== '') {

        if (strlen($cleanDescription) > 155) {

            $pageDescription =
                substr(
                    $cleanDescription,
                    0,
                    152
                )
                .
                '...';

        } else {

            $pageDescription =
                $cleanDescription;

        }

    } else {

        $pageDescription =
            'Explore the '
            .
            $scheme['plan_name']
            .
            ' contribution scheme from Complete Bliss Solutions.';

    }



    /*
    |--------------------------------------------------------------------------
    | Keywords
    |--------------------------------------------------------------------------
    */

    $pageKeywords =
        $scheme['plan_name']
        .
        ', Complete Bliss Solutions, '
        .
        $scheme['plan_type']
        .
        ' scheme, '
        .
        $scheme['frequency']
        .
        ' contribution, contribution schemes Nigeria';



    /*
    |--------------------------------------------------------------------------
    | Build Current Website Base URL
    |--------------------------------------------------------------------------
    */

    $schemeIsHttps =
        (
            !empty($_SERVER['HTTPS'])
            &&
            $_SERVER['HTTPS'] !== 'off'
        );


    $schemeProtocol =
        $schemeIsHttps
            ? 'https'
            : 'http';


    $schemeHost =
        $_SERVER['HTTP_HOST']
        ?? 'localhost';


    $schemeBasePath =
        rtrim(
            str_replace(
                '\\',
                '/',
                dirname(
                    $_SERVER['SCRIPT_NAME']
                    ?? ''
                )
            ),
            '/'
        );


    if ($schemeBasePath === '.') {

        $schemeBasePath = '';

    }



    /*
    |--------------------------------------------------------------------------
    | Canonical URL
    |--------------------------------------------------------------------------
    */

    $canonicalUrl =
        $schemeProtocol
        .
        '://'
        .
        $schemeHost
        .
        $schemeBasePath
        .
        '/scheme.php?id='
        .
        (int)$scheme['id'];



    /*
    |--------------------------------------------------------------------------
    | Social Sharing Image
    |--------------------------------------------------------------------------
    */

    if ($schemeImage !== '') {

        $ogImage =
            $schemeProtocol
            .
            '://'
            .
            $schemeHost
            .
            $schemeBasePath
            .
            '/'
            .
            ltrim(
                $schemeImage,
                '/'
            );

    }


} else {


    /*
    |--------------------------------------------------------------------------
    | Scheme Not Found SEO
    |--------------------------------------------------------------------------
    */

    $pageTitle =
        'Scheme Not Found | Complete Bliss Solutions';


    $pageDescription =
        'The requested Complete Bliss contribution scheme could not be found.';


    $pageRobots =
        'noindex, follow';

}



/* =========================================================
   HEADER
========================================================= */

require_once
    __DIR__ .
    '/public-site/includes/header.php';


require_once
    __DIR__ .
    '/public-site/includes/navbar.php';

?>


<main>


<?php if ($scheme): ?>


    <!-- =====================================================
         SCHEME HERO
    ====================================================== -->

    <section class="cbx-scheme-detail-hero">


        <div class="cbx-container">


            <div class="cbx-scheme-detail-grid">


                <!-- =========================================
                     IMAGE
                ========================================== -->

                <div class="cbx-scheme-detail-image">


                    <?php if ($schemeImage !== ''): ?>


                        <img
                            src="<?= htmlspecialchars($schemeImage) ?>"
                            alt="<?= htmlspecialchars($scheme['plan_name']) ?>"
                        >


                    <?php else: ?>


                        <div class="cbx-scheme-detail-placeholder">

                            <i class="fa-solid fa-image"></i>

                        </div>


                    <?php endif; ?>



                    <div class="cbx-scheme-detail-image-overlay"></div>



                    <div class="cbx-scheme-detail-image-badges">


                        <span>

                            <?= htmlspecialchars(
                                $scheme['plan_type']
                            ) ?>

                        </span>


                        <span>

                            <i class="fa-regular fa-calendar"></i>

                            <?= htmlspecialchars(
                                $scheme['frequency']
                            ) ?>

                        </span>


                    </div>


                </div>



                <!-- =========================================
                     INFORMATION
                ========================================== -->

                <div class="cbx-scheme-detail-info">


                    <a
                        href="schemes.php"
                        class="cbx-scheme-detail-back"
                    >

                        <i class="fa-solid fa-arrow-left"></i>

                        All Schemes

                    </a>


                    <div class="cbx-scheme-detail-tag">

                        <i class="fa-solid fa-layer-group"></i>

                        COMPLETE BLISS SCHEME

                    </div>


                    <h1>

                        <?= htmlspecialchars(
                            $scheme['plan_name']
                        ) ?>

                    </h1>



                    <?php if (!empty($scheme['description'])): ?>


                        <p class="cbx-scheme-detail-description">

                            <?= nl2br(
                                htmlspecialchars(
                                    $scheme['description']
                                )
                            ) ?>

                        </p>


                    <?php endif; ?>



                    <!-- =====================================
                         MAIN DETAILS
                    ====================================== -->

                    <div class="cbx-scheme-detail-stats">


                        <div>


                            <span>

                                <i class="fa-solid fa-wallet"></i>

                                Contribution

                            </span>


                            <strong>

                                ₦<?= number_format(
                                    (float)
                                    $scheme['amount'],
                                    2
                                ) ?>

                            </strong>


                            <small>
                                per hand
                            </small>


                        </div>



                        <div>


                            <span>

                                <i class="fa-regular fa-clock"></i>

                                Frequency

                            </span>


                            <strong>

                                <?= htmlspecialchars(
                                    $scheme['frequency']
                                ) ?>

                            </strong>


                            <small>
                                contribution schedule
                            </small>


                        </div>



                        <div>


                            <span>

                                <i class="fa-regular fa-calendar-check"></i>

                                Duration

                            </span>


                            <strong>

                                <?= number_format(
                                    (int)
                                    $scheme['duration']
                                ) ?>

                            </strong>


                            <small>
                                months
                            </small>


                        </div>


                    </div>



                    <?php if ($schemeStart !== ''): ?>


                        <div class="cbx-scheme-start">


                            <div>

                                <i class="fa-solid fa-calendar-day"></i>

                            </div>


                            <span>

                                <small>
                                    SCHEME START
                                </small>

                                <strong>
                                    <?= htmlspecialchars($schemeStart) ?>
                                </strong>

                            </span>


                        </div>


                    <?php endif; ?>



                    <!-- =====================================
                         CTA
                    ====================================== -->

                    <div class="cbx-scheme-detail-actions">


                        <a
                            href="register.php"
                            class="cbx-scheme-detail-primary"
                        >

                            Become a Member to Join

                            <i class="fa-solid fa-arrow-right"></i>

                        </a>


                        <a
                            href="index.php#contact"
                            class="cbx-scheme-detail-secondary"
                        >

                            <i class="fa-regular fa-circle-question"></i>

                            Ask a Question

                        </a>


                    </div>



                    <div class="cbx-scheme-detail-note">

                        <i class="fa-solid fa-circle-info"></i>

                        <span>
                            You need a Complete Bliss member account
                            before you can join a scheme.
                        </span>

                    </div>


                </div>


            </div>


        </div>


    </section>



    <!-- =====================================================
         WHAT YOU GET
    ====================================================== -->

    <?php if (!empty($packageItems)): ?>


        <section class="cbx-scheme-package-section">


            <div class="cbx-container">


                <div class="cbx-scheme-package-heading">


                    <div>

                        <span>

                            <i class="fa-solid fa-gift"></i>

                            PACKAGE DETAILS

                        </span>


                        <h2>
                            What you can expect
                        </h2>


                        <p>
                            Here are the items or benefits currently
                            listed for this Complete Bliss scheme.
                        </p>

                    </div>


                </div>



                <div class="cbx-scheme-package-grid">


                    <?php
                    foreach (
                        $packageItems
                        as $index => $item
                    ):
                    ?>


                        <div class="cbx-scheme-package-item">


                            <div>

                                <i class="fa-solid fa-circle-check"></i>

                            </div>


                            <span>

                                <small>

                                    ITEM
                                    <?= str_pad(
                                        $index + 1,
                                        2,
                                        '0',
                                        STR_PAD_LEFT
                                    ) ?>

                                </small>


                                <strong>

                                    <?= htmlspecialchars(
                                        $item
                                    ) ?>

                                </strong>

                            </span>


                        </div>


                    <?php endforeach; ?>


                </div>


            </div>


        </section>


    <?php endif; ?>



    <!-- =====================================================
         HOW THIS SCHEME WORKS
    ====================================================== -->

    <section class="cbx-scheme-how">


        <div class="cbx-container">


            <div class="cbx-scheme-how-shell">


                <div class="cbx-scheme-how-heading">


                    <span>
                        SIMPLE PROCESS
                    </span>


                    <h2>
                        How joining this scheme works
                    </h2>


                    <p>
                        Once you become a member, the process
                        is straightforward.
                    </p>


                </div>



                <div class="cbx-scheme-how-steps">


                    <div>


                        <span>
                            01
                        </span>


                        <div>

                            <i class="fa-solid fa-user-plus"></i>

                        </div>


                        <h3>
                            Create Account
                        </h3>


                        <p>
                            Register as a Complete Bliss member
                            and access your dashboard.
                        </p>


                    </div>



                    <div>


                        <span>
                            02
                        </span>


                        <div>

                            <i class="fa-solid fa-wallet"></i>

                        </div>


                        <h3>
                            Prepare Your Wallet
                        </h3>


                        <p>
                            Make sure your wallet has enough
                            funds for the required contribution.
                        </p>


                    </div>



                    <div>


                        <span>
                            03
                        </span>


                        <div>

                            <i class="fa-solid fa-hand-pointer"></i>

                        </div>


                        <h3>
                            Choose Your Hands
                        </h3>


                        <p>
                            Select the number of hands you want
                            when joining the scheme.
                        </p>


                    </div>



                    <div>


                        <span>
                            04
                        </span>


                        <div>

                            <i class="fa-solid fa-chart-line"></i>

                        </div>


                        <h3>
                            Follow Your Progress
                        </h3>


                        <p>
                            Track scheduled contributions and
                            progress from your member dashboard.
                        </p>


                    </div>


                </div>


            </div>


        </div>


    </section>



    <!-- =====================================================
         FINAL CTA
    ====================================================== -->

    <section class="cbx-scheme-final-cta">


        <div class="cbx-container">


            <div class="cbx-scheme-final-shell">


                <div>


                    <span>
                        INTERESTED IN THIS SCHEME?
                    </span>


                    <h2>

                        Start with

                        <strong>

                            <?= htmlspecialchars(
                                $scheme['plan_name']
                            ) ?>

                        </strong>

                    </h2>


                    <p>
                        Create your Complete Bliss account to
                        access available schemes and begin your
                        contribution journey.
                    </p>


                </div>


                <a href="register.php">

                    Create My Account

                    <i class="fa-solid fa-arrow-right"></i>

                </a>


            </div>


        </div>


    </section>



<?php else: ?>


    <!-- =====================================================
         NOT FOUND
    ====================================================== -->

    <section class="cbx-scheme-not-found">


        <div class="cbx-container">


            <div class="cbx-scheme-not-found-card">


                <div>

                    <i class="fa-solid fa-layer-group"></i>

                </div>


                <span>
                    SCHEME NOT AVAILABLE
                </span>


                <h1>
                    We couldn't find this scheme.
                </h1>


                <p>
                    The scheme may no longer be active,
                    or the link you followed may be incorrect.
                </p>


                <a href="schemes.php">

                    <i class="fa-solid fa-arrow-left"></i>

                    Browse Available Schemes

                </a>


            </div>


        </div>


    </section>


<?php endif; ?>


</main>


<?php

require_once
    __DIR__ .
    '/public-site/includes/footer.php';

?>