<?php
class Student {
    private $conn;
    private $table_name = "students";

    public $id;
    public $student_id;
    public $full_name;
    public $program;
    public $department;
    public $grade;
    public $graduation_date;
    public $photo_path;
    public $certificate_id;
    public $certificate_type;
    public $issue_date;
    public $qr_code_path;

    public function __construct($db) {
        $this->conn = $db;
    }

    public function read() {
        $query = "SELECT s.*, c.certificate_type, c.issue_date, c.qr_code_path 
                  FROM " . $this->table_name . " s
                  LEFT JOIN certificates c ON s.id = c.student_id
                  ORDER BY s.full_name";

        $stmt = $this->conn->prepare($query);
        $stmt->execute();
        return $stmt;
    }

    // public function read() {
    //     $query = "SELECT s.*, c.certificate_type, c.issue_date, c.qr_code_path 
    //               FROM " . $this->table_name . " s
    //               LEFT JOIN certificates c ON s.id = c.student_id
    //               ORDER BY s.full_name";

    //     $stmt = $this->conn->prepare($query);
    //     $stmt->execute();
    //     return $stmt;
    // }

    public function readOne() {
        $query = "SELECT s.*, c.certificate_type, c.issue_date, c.qr_code_path 
                  FROM " . $this->table_name . " s
                  LEFT JOIN certificates c ON s.id = c.student_id
                  WHERE s.id = ? LIMIT 0,1";

        $stmt = $this->conn->prepare($query);
        $stmt->bindParam(1, $this->id);
        $stmt->execute();

        $row = $stmt->fetch(PDO::FETCH_ASSOC);

        if($row) {
            $this->student_id = $row['student_id'];
            $this->full_name = $row['full_name'];
            $this->program = $row['program'];
            $this->department = $row['department'];
            $this->grade = $row['grade'];
            $this->graduation_date = $row['graduation_date'];
            $this->photo_path = $row['photo_path'];
            $this->certificate_id = $row['certificate_id'];
            $this->certificate_type = $row['certificate_type'];
            $this->issue_date = $row['issue_date'];
            $this->qr_code_path = $row['qr_code_path'];
        }
    }

    public function search($keywords) {
        $query = "SELECT s.*, c.certificate_type, c.issue_date, c.qr_code_path 
                  FROM " . $this->table_name . " s
                  LEFT JOIN certificates c ON s.id = c.student_id
                  WHERE s.full_name LIKE ? OR s.student_id LIKE ? OR s.program LIKE ?
                  ORDER BY s.full_name";

        $stmt = $this->conn->prepare($query);

        $keywords = "%{$keywords}%";
        $stmt->bindParam(1, $keywords);
        $stmt->bindParam(2, $keywords);
        $stmt->bindParam(3, $keywords);

        $stmt->execute();
        return $stmt;
    }
    public function generateQRContent() {
    // Get base URL for image paths
    $protocol = isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? "https" : "http";
    $host = $_SERVER['HTTP_HOST'];
    $baseUrl = $protocol . "://" . $host . dirname($_SERVER['PHP_SELF']);
    
    // Build photo URL
    $photoUrl = $this->photo_path;
    if ($this->photo_path && !filter_var($this->photo_path, FILTER_VALIDATE_URL)) {
        $photoUrl = $baseUrl . '/' . $this->photo_path;
    }
    
    $content = "GATEWAY ICT POLYTECHNIC SAAPADE\n";
    $content .= "==============================\n";
    $content .= "STUDENT ID: " . $this->student_id . "\n";
    $content .= "FULL NAME: " . $this->full_name . "\n";
    $content .= "PROGRAM: " . $this->program . "\n";
    $content .= "DEPARTMENT: " . $this->department . "\n";
    $content .= "GRADE: " . $this->grade . "\n";
    $content .= "GRADUATION DATE: " . $this->graduation_date . "\n";
    $content .= "CERTIFICATE ID: " . ($this->certificate_id ?: 'Pending') . "\n";
    // $content .= "PHOTO URL: " . $photoUrl . "\n";
    // $content .= "==============================\n";
    // $content .= "Verify: " . $baseUrl . "/verify.php?id=" . ($this->certificate_id ?: $this->id);
    
    return $content;
}


}
?>