<?php
namespace Api\Models;

use Api\Core\Database;

class User {
    private $db;

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

    public function authenticate($username, $password) {
        $stmt = $this->db->prepare("SELECT * FROM Utilisateurs WHERE code_utilisateur = ? AND statut = 'ACTIF'");
        $stmt->execute([$username]);
        $user = $stmt->fetch();

        if ($user && isset($user['mot_de_passe'])) {
            if (password_verify($password, $user['mot_de_passe'])) {
                return $user;
            }
            
            // Fallback for plain text
            if ($password === $user['mot_de_passe']) {
                return $user;
            }
        }

        return false;
    }
}
