<?php
declare(strict_types=1);

namespace App\Models;

use App\Core\Model;

/**
 * User Model
 */
class UserModel extends Model
{
    protected string $table = 'users';

    public function findByEmail(string $email, ?int $tenantId = null): ?array
    {
        if ($tenantId) {
            $stmt = $this->db->prepare("SELECT * FROM users WHERE email = ? AND tenant_id = ? LIMIT 1");
            $stmt->execute([$email, $tenantId]);
            return $stmt->fetch() ?: null;
        }
        return $this->findBy('email', $email);
    }

    public function getTenantUsers(int $tenantId): array
    {
        $stmt = $this->db->prepare("SELECT u.*, r.name as role 
                                   FROM users u 
                                   LEFT JOIN roles r ON u.role_id = r.id 
                                   WHERE u.tenant_id = ?");
        $stmt->execute([$tenantId]);
        return $stmt->fetchAll();
    }
}
