<?php
$servername = "localhost";
$username = "saadigit_moneyquotes_user";
$password = "niKKor!123!";

try {
  $conn = new PDO("mysql:host=$servername;dbname=saadigit_moneyquotes", $username, $password);
  // set the PDO error mode to exception
  $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch(PDOException $e) {
}

function getTotalQuotes($conn) {
    $stmt = $conn->prepare("SELECT COUNT(id) As 'Count' FROM quotes;");
    $stmt->execute();
    $c = 0;
    while ($row = $stmt->fetch()) {
        $c = $row['Count'];
    }
    return $c;
}

function getQuoteCountByAuthor($conn) {
    $stmt = $conn->prepare("SELECT author, COUNT(id) as Count FROM `quotes` GROUP BY author ORDER BY Count DESC, author ASC");
    $stmt->execute();
    return $stmt;
}

function getQuoteCountByTopic($conn) {
    $stmt = $conn->prepare("SELECT category, COUNT(id) as Count FROM `quotes` GROUP BY category ORDER BY Count DESC, author ASC");
    $stmt->execute();
    return $stmt;
}

function getRandomQuote($conn) {
    $stmt = $conn->prepare("SELECT author, quote FROM quotes ORDER BY RAND() LIMIT 1;");
    $stmt->execute();
    return $stmt;
}

function getQuoteByQuoteId($conn,$id) {
    $stmt = $conn->prepare("SELECT author, quote FROM quotes WHERE id = '".$id."' LIMIT 1;");
    $stmt->execute();
    return $stmt;
}

function getAuthorAttributesByAuthor($conn,$author) {
    $stmt = $conn->prepare("SELECT link, link_name FROM author_attributes WHERE author = :author LIMIT 1;");
    $stmt->execute(['author' => $author]);
    return $stmt;
}

function getRelatedQuotesByQuote($conn,$quote) {
    $stmt = $conn->prepare("SELECT id, author, category, quote FROM quotes WHERE MATCH (quote) AGAINST (:keywords IN NATURAL LANGUAGE MODE) ORDER BY author ASC, quote ASC LIMIT 1,10");
    $stmt->execute(['keywords' => $quote]);
    return $stmt;
}

function getBestQuoteBySlug($conn,$slug) {
    $stmt = $conn->prepare("SELECT * FROM best WHERE slug = :slug");
    $stmt->execute(['slug' => $slug]);
    return $stmt;
}

function runBestQueryBySlug($conn,$query) {
    $stmt = $conn->prepare($query);
    $stmt->execute();
    return $stmt;
}

function displayQuoteRow($row) {
    echo '<div class="row text-start">';
    echo '<div class="card">';

    $linkAuthor = str_replace(" ","-",$row['author']);
    $linkAuthor = str_replace("'","-",$linkAuthor);
    $linkAuthor = str_replace(".","",$linkAuthor);

    ?>
    <a class="quoteLink" href="quote/<?php echo $linkAuthor; ?>/<?php echo $row['id']; ?>">
            <div class="card-body">
                <h5 class="card-title"><?php echo $row['quote']; ?></h5>
            </div>
        </a>
        <div class="card-footer">
            <span class="badge bg-light text-dark"><a href="/search?authorExact=<?php echo $row['author']; ?>"><?php echo $row['author']; ?></a></span>
            <span class="badge bg-light text-dark">Filed in <a href="/search?topic=<?php echo $row['category']; ?>"><?php echo $row['category']; ?></a></span>
            <span class="badge bg-light text-dark cpyToClipboard" title="Copy to clipboard" quote="<?php echo $row['quote']; ?> -- <?php echo $row['author']; ?>" id="quote<?php echo $row['id']; ?>" onclick="copyToClipboard(this)">
                <svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-clipboard-plus" viewBox="0 0 16 16">
                <path fill-rule="evenodd" d="M8 7a.5.5 0 0 1 .5.5V9H10a.5.5 0 0 1 0 1H8.5v1.5a.5.5 0 0 1-1 0V10H6a.5.5 0 0 1 0-1h1.5V7.5A.5.5 0 0 1 8 7z"/>
                <path d="M4 1.5H3a2 2 0 0 0-2 2V14a2 2 0 0 0 2 2h10a2 2 0 0 0 2-2V3.5a2 2 0 0 0-2-2h-1v1h1a1 1 0 0 1 1 1V14a1 1 0 0 1-1 1H3a1 1 0 0 1-1-1V3.5a1 1 0 0 1 1-1h1v-1z"/>
                <path d="M9.5 1a.5.5 0 0 1 .5.5v1a.5.5 0 0 1-.5.5h-3a.5.5 0 0 1-.5-.5v-1a.5.5 0 0 1 .5-.5h3zm-3-1A1.5 1.5 0 0 0 5 1.5v1A1.5 1.5 0 0 0 6.5 4h3A1.5 1.5 0 0 0 11 2.5v-1A1.5 1.5 0 0 0 9.5 0h-3z"/>
              </svg>
            </span>
        </div>
    </div>
</div> <br />
    <?php
}

function switchQuotes() {
    // UPDATE quotes SET quote = REPLACE(quote, "’", "'")
}
?>
