package admin

import (
	"context"
	"database/sql"
	"time"
)

type Row struct {
	ID           int
	PasswordHash []byte // nil if not set
	CreatedAt    time.Time
}

// QGet returns the single admin row.
func QGet(ctx context.Context, db *sql.DB) (Row, error) {
	query := `SELECT id, password_hash, created_at FROM admin WHERE id = 1`

	var row Row
	err := db.QueryRowContext(ctx, query).Scan(
		&row.ID,
		&row.PasswordHash,
		&row.CreatedAt,
	)
	return row, err
}

// QSetPassword sets the admin password hash.
func QSetPassword(ctx context.Context, db *sql.DB, hash []byte) error {
	query := `UPDATE admin SET password_hash = $1 WHERE id = 1`
	_, err := db.ExecContext(ctx, query, hash)
	return err
}

// QHasPassword returns true if a password has been set.
func QHasPassword(ctx context.Context, db *sql.DB) (bool, error) {
	query := `SELECT password_hash IS NOT NULL FROM admin WHERE id = 1`
	var has bool
	err := db.QueryRowContext(ctx, query).Scan(&has)
	return has, err
}
