package main

import (
	// External
	werr "git.ahall.se/go/wrappederror"
	"github.com/jackc/pgx/v5"

	// Standard
	"context"
)

type WidgetType int

const (
	WIDGET_LABEL WidgetType = iota
	WIDGET_ONOFF
)

type Widget struct {
	Type        WidgetType
	X           int
	Y           int
	SpanX       int
	SpanY       int
	DatapointID int
	Attributes  map[string]string
}

type Dashboard struct {
	ID      int
	Name    string
	Widgets []Widget
}

func DashboardGet(name string) (dashboard Dashboard, err error) {
	var rows pgx.Rows
	rows, err = db.Query(context.Background(), `SELECT * FROM dashboard WHERE name = $1`, name)
	if err != nil {
		err = werr.Wrap(err)
		return
	}

	dashboard, err = pgx.CollectExactlyOneRow(rows, pgx.RowToStructByNameLax[Dashboard])
	if err != nil {
		err = werr.Wrap(err)
		return
	}

	return
}
