package api

import (
	"context"
	"log/slog"

	"git.maximotejeda.com/maximo/tgb-user/internal/adapters/db"
	"git.maximotejeda.com/maximo/tgb-user/internal/application/core/domain"
	"git.maximotejeda.com/maximo/tgb-user/internal/ports"
)

type Application struct {
	db  ports.DBPort
	log *slog.Logger
}

func NewApplication(db ports.DBPort) *Application {
	log := slog.Default()
	log = log.With("adapter", "application")
	return &Application{
		db:  db,
		log: log,
	}
}

// todo Export differents Methods to map to grpc contracts
func (a Application) Create(ctx context.Context, user *domain.User) (bool, error) {
	ok, err := a.db.Create(ctx, user)
	return ok, err
}
func (a Application) Get(ctx context.Context, tgb_id int64) (*domain.User, error) {
	user, err := a.db.Get(ctx, tgb_id)
	return user, err
}
func (a Application) Edit(ctx context.Context, user *domain.User) (bool, error) {
	ok, err := a.db.Edit(ctx, user)
	if err != nil {
		return false, err
	}
	return ok, err
}
func (a Application) Delete(ctx context.Context, id int64) (bool, error) {
	ok, err := a.db.Delete(ctx, id)
	return ok, err
}
func (a Application) AddBot(ctx context.Context, tgb_id int64, botname string) (bool, error) {
	ok, err := a.db.AddBot(ctx, tgb_id, botname)
	return ok, err
}

func (a Application) DeleteBot(ctx context.Context, tgb_id int64, botname string) (bool, error) {
	ok, err := a.db.DeleteBot(ctx, tgb_id, botname)
	return ok, err
}

func (a Application) GetBots(ctx context.Context, tgb_id int64) ([]*domain.Bot, error) {
	return a.db.GetBots(ctx, tgb_id)
}

func (a Application) GetAllBotsUsers(ctx context.Context, bot_name string) ([]*domain.User, error) {
	return a.db.GetAllBotUsers(ctx, bot_name)
}
func (a Application) GetAllBots(ctx context.Context) ([]*domain.Bot, error) {
	return a.db.GetAllBots(ctx)
}

func (a Application) CreateBot(ctx context.Context, botName string) (bool, error) {
	return a.db.CreateBot(ctx, botName)
}
func (a Application) CreateAccessRequest(ctx context.Context, tgbID int64, botName string) (bool, error) {
	return a.db.CreateAccessRequest(ctx, tgbID, botName)
}
func (a Application) GrantAccess(ctx context.Context, tgbID int64, botName string) (bool, error) {
	return a.db.GrantAccess(ctx, tgbID, botName)
}
func (a Application) BanUser(ctx context.Context, tgbID, until int64, botName string) (bool, error) {
	return a.db.BanUser(ctx, tgbID, until, botName)
}
func (a Application) UnBanUser(ctx context.Context, tgbID int64, botName string) (bool, error) {
	return a.db.UnBanUser(ctx, tgbID, botName)
}

func (a Application) GetAllAccessRequest(ctx context.Context, botName string) ([]*db.BotAccessRequest, error) {
	return a.db.GetAllAccessRequest(ctx, botName)
}
func (a Application) GetAllBannedUsers(ctx context.Context, botName string) ([]*db.BotBannedAccess, error) {
	return a.db.GetAllBannedUsers(ctx, botName)
}
func (a Application) GetAccessRequest(ctx context.Context, tgbID int64) ([]*db.BotAccessRequest, error) {
	return a.db.GetAccessRequest(ctx, tgbID)
}
func (a Application) GetBannedBot(ctx context.Context, tgbID int64) ([]*db.BotBannedAccess, error) {
	return a.db.GetBannedBot(ctx, tgbID)
}
