package bunmodel

import (
	"fmt"
	"time"

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

type Ennoblement struct {
	bun.BaseModel `bun:"table:ennoblements,alias:ennoblement"`

	ID         int       `bun:"id,pk,autoincrement,identity"`
	ServerKey  string    `bun:"server_key,nullzero"`
	VillageID  int       `bun:"village_id,nullzero"`
	Village    Village   `bun:"village,rel:belongs-to,join:village_id=id,join:server_key=server_key"`
	NewOwnerID int       `bun:"new_owner_id,nullzero"`
	NewOwner   Player    `bun:"new_owner,rel:belongs-to,join:new_owner_id=id,join:server_key=server_key"`
	NewTribeID int       `bun:"new_tribe_id,nullzero"`
	NewTribe   Tribe     `bun:"new_tribe,rel:belongs-to,join:new_tribe_id=id,join:server_key=server_key"`
	OldOwnerID int       `bun:"old_owner_id,nullzero"`
	OldOwner   Player    `bun:"old_owner,rel:belongs-to,join:old_owner_id=id,join:server_key=server_key"`
	OldTribeID int       `bun:"old_tribe_id,nullzero"`
	OldTribe   Tribe     `bun:"old_tribe,rel:belongs-to,join:old_tribe_id=id,join:server_key=server_key"`
	Points     int       `bun:"points"`
	CreatedAt  time.Time `bun:"created_at,nullzero"`
}

func (e Ennoblement) ToDomain() (domain.Ennoblement, error) {
	converted, err := domain.UnmarshalEnnoblementFromDatabase(
		e.ID,
		e.ServerKey,
		e.VillageID,
		e.NewOwnerID,
		e.NewTribeID,
		e.OldOwnerID,
		e.OldTribeID,
		e.Points,
		e.CreatedAt,
	)
	if err != nil {
		return domain.Ennoblement{}, fmt.Errorf(
			"couldn't construct domain.Ennoblement (id=%d,serverKey=%s): %w",
			e.ID,
			e.ServerKey,
			err,
		)
	}

	return converted, nil
}

type Ennoblements []Ennoblement

func (es Ennoblements) ToDomain() (domain.Ennoblements, error) {
	res := make(domain.Ennoblements, 0, len(es))

	for _, e := range es {
		converted, err := e.ToDomain()
		if err != nil {
			return nil, err
		}

		res = append(res, converted)
	}

	return res, nil
}
