<?php

namespace App\Controllers;

require_once BASE_PATH . '/app/helpers/permissions.php';

use PDO;

use App\Core\Controller;
use App\Core\Database;
use App\Core\TenantContext;

class ConferenceHomepageBuilderController extends Controller
{
    public function index()
    {
        $db = Database::getInstance();

/*
|--------------------------------------------------------------------------
| SUPERADMIN FALLBACK
|--------------------------------------------------------------------------
*/

if (
    (
        ec_is_superadmin()
    )
    &&
    empty($_SESSION['conference_id'])
) {

    $stmtConf = $db->query("
        SELECT id
        FROM conferences
        ORDER BY id ASC
        LIMIT 1
    ");

    $firstConference =
        $stmtConf->fetch(PDO::FETCH_ASSOC);

    if ($firstConference) {

        $_SESSION['conference_id']
            = (int)$firstConference['id'];

        $_SESSION['current_conference_id']
            = (int)$firstConference['id'];
    }
}

        $conferenceId =
            TenantContext::conferenceId();

        if ($conferenceId <= 0) {

            $conferenceId =
                (int)($_SESSION['conference_id'] ?? 0);
        }

        error_log(
            'HOMEPAGE CMS CONF ID: '
            . $conferenceId
        );

        $sections = [];

        try {

            $stmt = $db->prepare("
                SELECT *
                FROM conference_home_sections
                WHERE conference_id = ?
                ORDER BY
                    sort_order ASC,
                    id ASC
            ");

            $stmt->execute([
                $conferenceId
            ]);

            $sections =
                $stmt->fetchAll(PDO::FETCH_ASSOC);

            error_log(
                'HOMEPAGE SECTIONS LOADED: '
                . count($sections)
            );

        } catch (\Throwable $e) {

            error_log(
                'HOMEPAGE CMS ERROR: '
                . $e->getMessage()
            );
        }

        $this->render(
            'admin/homepage_builder/index',
            [
                'sections' => $sections,
                'conferenceId' => $conferenceId
            ]
        );
    }

    public function toggle()
    {
        $db = Database::getInstance();

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

        $stmt = $db->prepare("
            UPDATE conference_home_sections
            SET enabled =
                CASE
                    WHEN enabled = 1 THEN 0
                    ELSE 1
                END
            WHERE id = ?
            LIMIT 1
        ");

        $stmt->execute([
            $id
        ]);

        $_SESSION['success'] =
            'Homepage section updated';

        header(
            'Location: /easychair/admin/conference-home-builder'
        );

        exit;
    }

    public function moveUp()
    {
        $this->move(-10);
    }

    public function moveDown()
    {
        $this->move(10);
    }

    private function move(int $delta)
    {
        $db = Database::getInstance();

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

        $stmt = $db->prepare("
            UPDATE conference_home_sections
            SET sort_order = sort_order + ?
            WHERE id = ?
            LIMIT 1
        ");

        $stmt->execute([
            $delta,
            $id
        ]);

        $_SESSION['success'] =
            'Homepage section order updated';

        header(
            'Location: /easychair/admin/conference-home-builder'
        );

        exit;
    }
}
