<?php

namespace App\Modules\Admin\Operations\Repositories;

use App\Modules\Admin\Operations\Models\SupportImpersonationSession;

class SupportImpersonationSessionRepository
{
    /**
     * @param  array<string, mixed>  $data
     */
    public function create(array $data): SupportImpersonationSession
    {
        return SupportImpersonationSession::query()->create($data);
    }

    public function findActiveByTokenHash(string $tokenHash): ?SupportImpersonationSession
    {
        return SupportImpersonationSession::query()
            ->with('target:id,is_active')
            ->where('token_hash', $tokenHash)
            ->whereNull('revoked_at')
            ->whereNull('consumed_at')
            ->first();
    }

    public function findByIdForAdmin(int $id, int $adminUserId): ?SupportImpersonationSession
    {
        return SupportImpersonationSession::query()
            ->whereKey($id)
            ->where('admin_user_id', $adminUserId)
            ->first();
    }
}
