package adapter

import (
	"context"
	"database/sql"
	"errors"
	"fmt"
	"time"

	"gitea.dwysokinski.me/twhelp/corev3/internal/bun/bunmodel"
	"gitea.dwysokinski.me/twhelp/corev3/internal/domain"
	"github.com/uptrace/bun"
)

type PlayerSnapshotBunRepository struct {
	db bun.IDB
}

func NewPlayerSnapshotBunRepository(db bun.IDB) *PlayerSnapshotBunRepository {
	return &PlayerSnapshotBunRepository{db: db}
}

func (repo *PlayerSnapshotBunRepository) Create(
	ctx context.Context,
	params ...domain.CreatePlayerSnapshotParams,
) error {
	if len(params) == 0 {
		return nil
	}

	now := time.Now()
	playerSnapshots := make(bunmodel.PlayerSnapshots, 0, len(params))

	for _, p := range params {
		playerSnapshots = append(playerSnapshots, bunmodel.PlayerSnapshot{
			PlayerID:          p.PlayerID(),
			NumVillages:       p.NumVillages(),
			Points:            p.Points(),
			Rank:              p.Rank(),
			TribeID:           p.TribeID(),
			ServerKey:         p.ServerKey(),
			Date:              p.Date(),
			CreatedAt:         now,
			OpponentsDefeated: bunmodel.NewOpponentsDefeated(p.OD()),
		})
	}

	if _, err := repo.db.NewInsert().
		Model(&playerSnapshots).
		Ignore().
		Returning("").
		Exec(ctx); err != nil {
		return fmt.Errorf("something went wrong while inserting player snapshots into the db: %w", err)
	}

	return nil
}

func (repo *PlayerSnapshotBunRepository) List(
	ctx context.Context,
	params domain.ListPlayerSnapshotsParams,
) (domain.PlayerSnapshots, error) {
	var playerSnapshots bunmodel.PlayerSnapshots

	if err := repo.db.NewSelect().
		Model(&playerSnapshots).
		Apply(listPlayerSnapshotsParamsApplier{params: params}.apply).
		Scan(ctx); err != nil && !errors.Is(err, sql.ErrNoRows) {
		return nil, fmt.Errorf("couldn't select player snapshots from the db: %w", err)
	}

	return playerSnapshots.ToDomain()
}

type listPlayerSnapshotsParamsApplier struct {
	params domain.ListPlayerSnapshotsParams
}

//nolint:gocyclo
func (a listPlayerSnapshotsParamsApplier) apply(q *bun.SelectQuery) *bun.SelectQuery {
	if serverKeys := a.params.ServerKeys(); len(serverKeys) > 0 {
		q = q.Where("ps.server_key IN (?)", bun.In(serverKeys))
	}

	for _, s := range a.params.Sort() {
		switch s {
		case domain.PlayerSnapshotSortDateASC:
			q = q.Order("ps.date ASC")
		case domain.PlayerSnapshotSortDateDESC:
			q = q.Order("ps.date DESC")
		case domain.PlayerSnapshotSortIDASC:
			q = q.Order("ps.id ASC")
		case domain.PlayerSnapshotSortIDDESC:
			q = q.Order("ps.id DESC")
		case domain.PlayerSnapshotSortServerKeyASC:
			q = q.Order("ps.server_key ASC")
		case domain.PlayerSnapshotSortServerKeyDESC:
			q = q.Order("ps.server_key DESC")
		default:
			return q.Err(fmt.Errorf("%s: %w", s.String(), errInvalidSortValue))
		}
	}

	return q.Limit(a.params.Limit()).Offset(a.params.Offset())
}
