package audit

import (
	"context"

	"github.com/google/uuid"
	"github.com/jackc/pgx/v5/pgxpool"
)

type PGRepository struct {
	db *pgxpool.Pool
}

func NewPGRepository(db *pgxpool.Pool) *PGRepository {
	return &PGRepository{db: db}
}

func (r *PGRepository) Write(ctx context.Context, entry Entry) error {
	const query = `
		insert into audit_logs (id, actor_user_id, entity_type, entity_id, event_type, status, message, metadata)
		values ($1, $2, $3, $4, $5, $6, $7, $8::jsonb)
	`

	_, err := r.db.Exec(
		ctx,
		query,
		uuid.New(),
		entry.ActorUserID,
		entry.EntityType,
		entry.EntityID,
		entry.EventType,
		entry.Status,
		entry.Message,
		MarshalMetadata(entry.Metadata),
	)
	return err
}

func (r *PGRepository) List(ctx context.Context, limit int) ([]map[string]any, error) {
	rows, err := r.db.Query(ctx, `
		select id, event_type, entity_type, status, message, created_at
		from audit_logs
		order by created_at desc
		limit $1
	`, limit)
	if err != nil {
		return nil, err
	}
	defer rows.Close()

	var entries []map[string]any
	for rows.Next() {
		var id uuid.UUID
		var eventType, entityType, status, message string
		var createdAt any
		if err := rows.Scan(&id, &eventType, &entityType, &status, &message, &createdAt); err != nil {
			return nil, err
		}
		entries = append(entries, map[string]any{
			"id":          id,
			"event_type":  eventType,
			"entity_type": entityType,
			"status":      status,
			"message":     message,
			"created_at":  createdAt,
		})
	}

	return entries, rows.Err()
}
