<?php
require_once __DIR__ . '/../models/Blog.php';
require_once __DIR__ . '/../models/SiteSettings.php';

class BlogController {
    private $blog;
    private $siteSettings;
    
    public function __construct() {
        $this->blog = new Blog();
        $this->siteSettings = new SiteSettings();
    }
    
    public function index() {
        $settings = $this->siteSettings->getAllSettings();
        
        // Get blog posts with pagination
        $page = isset($_GET['page']) ? (int)$_GET['page'] : 1;
        $perPage = 10;
        $offset = ($page - 1) * $perPage;
        
        $posts = $this->blog->getAllPosts($perPage, $offset);
        $totalPosts = $this->blog->getPostCount();
        $totalPages = ceil($totalPosts / $perPage);
        
        $pageTitle = 'Blog';
        $pageDescription = 'Read our latest blog posts about gaming tips, news, and updates.';
        
        include __DIR__ . '/../views/blog.php';
    }
    
    public function show($slug) {
        $settings = $this->siteSettings->getAllSettings();
        $post = $this->blog->getPostBySlug($slug);
        
        if (!$post) {
            header('HTTP/1.0 404 Not Found');
            include __DIR__ . '/../views/404.php';
            return;
        }
        
        // Get recent posts for sidebar
        $recentPosts = $this->blog->getRecentPosts(5);
        
        $pageTitle = $post['title'];
        $pageDescription = $post['meta_description'] ?? substr(strip_tags($post['content']), 0, 160);
        
        // Set post title variable for SEO title template
        $postTitle = $post['title'];
        
        include __DIR__ . '/../views/blog-post.php';
    }
    
    public function tag($tag) {
        $settings = $this->siteSettings->getAllSettings();
        
        // Get blog posts with pagination
        $page = isset($_GET['page']) ? (int)$_GET['page'] : 1;
        $perPage = 10;
        $offset = ($page - 1) * $perPage;
        
        $posts = $this->blog->getPostsByTag($tag, $perPage, $offset);
        
        $pageTitle = 'Tag: ' . $tag;
        $pageDescription = 'Blog posts tagged with ' . $tag;
        
        include __DIR__ . '/../views/blog.php';
    }
} 