package persistence

import (
	"context"
	"database/sql"
)

// PermissionRepository handles database operations for permissions.
type PermissionRepository struct {
	db *sql.DB
}

// NewPermissionRepository creates a new PermissionRepository
func NewPermissionRepository(db *sql.DB) *PermissionRepository {
	return &PermissionRepository{db: db}
}

// getExecutor returns the transactional executor if present in context, or the DB connection.
// This allows permission writes to participate in the caller's transaction.
func (r *PermissionRepository) getExecutor(ctx context.Context) Executor {
	if tx, ok := ctx.Value(txContextKey{}).(*sql.Tx); ok && tx != nil {
		return tx
	}
	return r.db
}
