<?php

namespace App\Core;

class TenantContext
{
    public static function conferenceId(): int
    {
        if (!empty($_SESSION['conference_id'])) {

            return (int)$_SESSION['conference_id'];
        }

        try {

            $db = Database::getInstance();

            /*
            |--------------------------------------------------------------------------
            | PRIORITY 1
            | URL ?id=
            |--------------------------------------------------------------------------
            */

            if (!empty($_GET['id'])) {

                $conferenceId =
                    (int)$_GET['id'];

                if ($conferenceId > 0) {

                    $_SESSION['conference_id'] =
                        $conferenceId;

                    return $conferenceId;
                }
            }

            /*
            |--------------------------------------------------------------------------
            | PRIORITY 2
            | USER ORGANIZATION CONFERENCE
            |--------------------------------------------------------------------------
            */

            if (!empty($_SESSION['org_id'])) {

                $stmt = $db->prepare("
                    SELECT id
                    FROM conferences
                    WHERE org_id = ?
                    ORDER BY id ASC
                    LIMIT 1
                ");

                $stmt->execute([
                    (int)$_SESSION['org_id']
                ]);

                $conferenceId =
                    (int)$stmt->fetchColumn();

                if ($conferenceId > 0) {

                    $_SESSION['conference_id'] =
                        $conferenceId;

                    return $conferenceId;
                }
            }

            /*
            |--------------------------------------------------------------------------
            | PRIORITY 3
            | FIRST AVAILABLE CONFERENCE
            |--------------------------------------------------------------------------
            */

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

            $conferenceId =
                (int)$stmt->fetchColumn();

            if ($conferenceId > 0) {

                $_SESSION['conference_id'] =
                    $conferenceId;

                return $conferenceId;
            }

        } catch (\Throwable $e) {

            error_log(
                'TenantContext conference error: ' .
                $e->getMessage()
            );
        }

        return 0;
    }

    public static function orgId(): int
    {
        if (!empty($_SESSION['org_id'])) {

            return (int)$_SESSION['org_id'];
        }

        try {

            $db = Database::getInstance();

            $conferenceId =
                self::conferenceId();

            if ($conferenceId > 0) {

                $stmt = $db->prepare("
                    SELECT org_id
                    FROM conferences
                    WHERE id = ?
                    LIMIT 1
                ");

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

                $orgId =
                    (int)$stmt->fetchColumn();

                if ($orgId > 0) {

                    $_SESSION['org_id'] =
                        $orgId;

                    return $orgId;
                }
            }

        } catch (\Throwable $e) {

            error_log(
                'TenantContext org error: ' .
                $e->getMessage()
            );
        }

        return 0;
    }

    public static function debug(): void
    {
        error_log(
            'TenantContext => conf=' .
            self::conferenceId() .
            ' org=' .
            self::orgId()
        );
    }
}
