package repository

import (
	"context"

	sq "github.com/Masterminds/squirrel"

	"git.dmitriygnatenko.ru/dima/dmitriygnatenko-v2/internal/helper/errors"
	"git.dmitriygnatenko.ru/dima/dmitriygnatenko-v2/internal/model"
)

const languageTableName = "language"

var languageTableColumns = []string{
	"id",
	"url",
	"title",
	"is_default",
}

type LanguageRepository struct {
	db DB
}

func InitLanguageRepository(db DB) *LanguageRepository {
	return &LanguageRepository{db: db}
}

func (a LanguageRepository) GetAll(
	ctx context.Context,
) ([]model.Language, error) {
	var res []model.Language

	q, v, err := sq.Select(languageTableColumns...).
		From(languageTableName).
		ToSql()

	if err != nil {
		return nil, errors.Wrap(err, "build query")
	}

	err = a.db.SelectContext(ctx, &res, q, v...)
	if err != nil {
		return nil, errors.Wrap(err, "select")
	}

	return res, nil
}
