package storage

import (
	"context"
	"log"
	"madsky.ru/go-tracker/internal/database"
	"madsky.ru/go-tracker/internal/model/setting"
)

type ServerSettingsRepository interface {
	Find(ctx context.Context) ([]*setting.Setting, error)
}

type ServerSettingsStore struct {
	client database.Client
}

func (store *ServerSettingsStore) Find(ctx context.Context) ([]*setting.Setting, error) {
	query := "select id, name, value from public.server_settings"

	rows, err := store.client.Query(ctx, query)
	if err != nil {
		log.Println("ServerSettingsStore query err", err)
		return nil, err
	}

	res := make([]*setting.Setting, 0)

	for rows.Next() {
		var r setting.Setting

		err := rows.Scan(&r.ID, &r.Name, &r.Value)
		if err != nil {
			log.Println("ServerSettingsStore scan err", err)
			return nil, err
		}
		res = append(res, &r)
	}

	if err := rows.Err(); err != nil {
		log.Println("ServerSettingsStore rows err", err)
		return nil, err
	}

	return res, nil
}
