<?php
/**
 * Website Details Helper
 * 
 * This file provides functions to fetch website details from the database
 * Usage: include this file and call getWebsiteDetails() or getWebsiteDetail()
 */

/**
 * Get all website details
 * 
 * @param PDO $pdo Database connection
 * @return array|null Array of website details or null if not found
 */
function getWebsiteDetails($pdo) {
    try {
        $stmt = $pdo->query("SELECT * FROM website_details ORDER BY id DESC LIMIT 1");
        return $stmt->fetch(PDO::FETCH_ASSOC);
    } catch (PDOException $e) {
        error_log("Error fetching website details: " . $e->getMessage());
        return null;
    }
}

/**
 * Get a specific website detail
 * 
 * @param PDO $pdo Database connection
 * @param string $field Field name to retrieve
 * @return string|null Field value or null if not found
 */
function getWebsiteDetail($pdo, $field) {
    $details = getWebsiteDetails($pdo);
    return $details[$field] ?? null;
}

/**
 * Get call for papers for a specific journal
 * 
 * @param PDO $pdo Database connection
 * @param string $journal Journal code (ijrsi, ijriss, ijrias)
 * @return string Call for papers text
 */
function getCallForPapers($pdo, $journal) {
    $field = 'call_for_papers_' . strtolower($journal);
    return getWebsiteDetail($pdo, $field) ?? 'Open for Submissions';
}

/**
 * Get submission deadline for a specific journal
 * 
 * @param PDO $pdo Database connection
 * @param string $journal Journal code (ijrsi, ijriss, ijrias)
 * @return string Submission deadline text
 */
function getSubmissionDeadline($pdo, $journal) {
    $field = 'submission_deadline_' . strtolower($journal);
    return getWebsiteDetail($pdo, $field) ?? 'Rolling Submissions';
}

/**
 * Get volumes/issue for a specific journal
 * 
 * @param PDO $pdo Database connection
 * @param string $journal Journal code (ijrsi, ijriss, ijrias)
 * @return string Volumes/Issue text
 */
function getVolumesIssue($pdo, $journal) {
    $field = 'volumes_issue_' . strtolower($journal);
    return getWebsiteDetail($pdo, $field) ?? 'Volume 1, Issue 1';
}

/**
 * Get DOI for a specific journal
 * 
 * @param PDO $pdo Database connection
 * @param string $journal Journal code (ijrsi, ijriss, ijrias)
 * @return string DOI text
 */
function getDOI($pdo, $journal) {
    $field = 'doi_' . strtolower($journal);
    return getWebsiteDetail($pdo, $field) ?? '';
}

/**
 * Example usage in your website pages:
 * 
 * // Include this file and your database config
 * require_once 'path/to/journal_config_new.php';
 * require_once 'path/to/website_details_helper.php';
 * 
 * // Get all details
 * $websiteDetails = getWebsiteDetails($pdo);
 * 
 * // Get specific details for IJRSI
 * $callForPapers = getCallForPapers($pdo, 'ijrsi');
 * $deadline = getSubmissionDeadline($pdo, 'ijrsi');
 * $volume = getVolumesIssue($pdo, 'ijrsi');
 * $doi = getDOI($pdo, 'ijrsi');
 * 
 * // Use in your HTML
 * echo "<h2>" . htmlspecialchars($callForPapers) . "</h2>";
 * echo "<p>Deadline: " . htmlspecialchars($deadline) . "</p>";
 * echo "<p>Current Issue: " . htmlspecialchars($volume) . "</p>";
 * echo "<p>DOI: " . htmlspecialchars($doi) . "</p>";
 */
?>