package repository

import (
	"context"
	v1 "github.com/go-nunu/nunu-layout-advanced/api/v1"
	"github.com/go-nunu/nunu-layout-advanced/internal/model"
)

type HostRepository interface {
	GetHost(ctx context.Context, id int64) (*model.Host, error)
	GetHostConfig(ctx context.Context, hostId int) ([]*model.Host, error)
	GetProductConfigOption(ctx context.Context, id []int) ([]v1.ProductConfigOption, error)
	GetProductConfigOptionSub(ctx context.Context, id []int) ([]v1.ProductConfigOptionSub, error)
	GetDomainById(ctx context.Context, id int) (string, error)
}

func NewHostRepository(
	repository *Repository,
) HostRepository {
	return &hostRepository{
		Repository: repository,
	}
}

type hostRepository struct {
	*Repository
}

func (r *hostRepository) GetHost(ctx context.Context, id int64) (*model.Host, error) {
	var host model.Host

	return &host, nil
}

func (r *hostRepository) GetHostConfig(ctx context.Context, hostId int) ([]*model.Host, error) {
	var res []*model.Host
	if err := r.DB(ctx).Where("relid = ?", hostId).Find(&res).Error; err != nil {
		return nil, err
	}
	return res, nil
}

func (r *hostRepository) GetProductConfigOption(ctx context.Context, id []int) ([]v1.ProductConfigOption, error) {
	var res []v1.ProductConfigOption
	if err := r.DB(ctx).Table("shd_product_config_options").Where("id IN ?", id).Find(&res).Error; err != nil {
		return nil, err
	}
	return res, nil
}

func (r *hostRepository) GetProductConfigOptionSub(ctx context.Context, id []int) ([]v1.ProductConfigOptionSub, error) {
	var res []v1.ProductConfigOptionSub
	if err := r.DB(ctx).Table("shd_product_config_options_sub").Where("id IN ?", id).Find(&res).Error; err != nil {
		return nil, err
	}
	return res, nil
}

func (r *hostRepository) GetDomainById(ctx context.Context, id int) (string, error) {
	var res string
	if err := r.DB(ctx).Table("shd_host").
		Where("id = ?", id).
		Pluck("domain", &res).Error; err != nil {
		return "", err
	}
	return res, nil
}