<?php
require_once __DIR__ . '/../config/database.php';

/**
 * Admin Class for handling admin operations
 * Optimized for performance on shared hosting
 */
class Admin {
    private $db;

    public function __construct() {
        $this->db = Database::getInstance();
    }

    /**
     * Authenticate admin user
     * @param string $email
     * @param string $password
     * @return bool|array Returns user data if authenticated, false otherwise
     */
    public function authenticate($email, $password) {
        $conn = $this->db->getConnection();
        if (!$conn) return false;

        $email = $this->db->escapeString($email);

        $stmt = $conn->prepare("SELECT id, email, password, expiry_date FROM admin WHERE email = ? LIMIT 1");
        $stmt->bind_param("s", $email);
        $stmt->execute();
        $result = $stmt->get_result();

        if ($result->num_rows === 1) {
            $admin = $result->fetch_assoc();

            // 🔒 Expiry check
            if (!empty($admin['expiry_date']) && strtotime($admin['expiry_date']) < time()) {
                return ['expired' => true, 'expiry_date' => $admin['expiry_date']];
            }

            // Plaintext password comparison
            if ($password === $admin['password']) {
                unset($admin['password'], $admin['expiry_date']);
                return $admin;
            }
        }

        $stmt->close();
        return false;
    }

    /**
     * Get expiry date for countdown display
     * @return string|null
     */
    public function getExpiryDate() {
        $conn = $this->db->getConnection();
        if (!$conn) return null;

        $sql = "SELECT expiry_date FROM admin ORDER BY id ASC LIMIT 1";
        $result = $conn->query($sql);

        if ($result && $row = $result->fetch_assoc()) {
            return $row['expiry_date'];
        }

        return null;
    }

    /**
     * Change admin password
     */
    public function changePassword($adminId, $currentPassword, $newPassword) {
        $conn = $this->db->getConnection();
        if (!$conn) return false;

        $stmt = $conn->prepare("SELECT password FROM admin WHERE id = ? LIMIT 1");
        $stmt->bind_param("i", $adminId);
        $stmt->execute();
        $result = $stmt->get_result();

        if ($result->num_rows === 1) {
            $admin = $result->fetch_assoc();
            
            if ($currentPassword === $admin['password']) {
                // Store plaintext
                $updateStmt = $conn->prepare("UPDATE admin SET password = ? WHERE id = ?");
                $updateStmt->bind_param("si", $newPassword, $adminId);
                $success = $updateStmt->execute();
                $updateStmt->close();
                return $success;
            }
        }

        $stmt->close();
        return false;
    }

    /**
     * Get admin details by ID
     */
    public function getAdminById($adminId) {
        $conn = $this->db->getConnection();
        if (!$conn) return false;

        $stmt = $conn->prepare("SELECT id, email, date FROM admin WHERE id = ? LIMIT 1");
        $stmt->bind_param("i", $adminId);
        $stmt->execute();
        $result = $stmt->get_result();

        if ($result->num_rows === 1) {
            return $result->fetch_assoc();
        }

        $stmt->close();
        return false;
    }

    /**
     * Update admin verification password
     */
    public function updateVerificationPassword($adminId, $verifyPass) {
        $conn = $this->db->getConnection();
        if (!$conn) return false;

        $stmt = $conn->prepare("UPDATE admin SET verifypass = ? WHERE id = ?");
        $stmt->bind_param("si", $verifyPass, $adminId);
        $success = $stmt->execute();
        $stmt->close();

        return $success;
    }
}
