package schema

import (
	"juwan-backend/pkg/types"
	"time"

	"entgo.io/ent"
	"entgo.io/ent/dialect"
	"entgo.io/ent/dialect/entsql"
	"entgo.io/ent/schema"
	"entgo.io/ent/schema/field"
	"github.com/shopspring/decimal"
)

var (
	decFive = decimal.RequireFromString("5.00")
)

// PlayerServices holds the schema definition for the PlayerServices entity.
type PlayerServices struct {
	ent.Schema
}

// Annotations of the PlayerServices.
func (PlayerServices) Annotations() []schema.Annotation {
	return []schema.Annotation{
		entsql.Annotation{
			Table: "player_services",
			Checks: map[string]string{
				"chk_price_positive": "price > 0",
				"chk_service_rating": "rating >= 0 AND rating <= 5",
			},
		},
	}
}

// Fields of the PlayerServices.
func (PlayerServices) Fields() []ent.Field {
	return []ent.Field{
		field.Int64("id").Immutable().Unique(),
		field.Int64("player_id"),
		field.Int64("game_id"),
		field.String("title").MaxLen(200),
		field.String("description").Optional().Nillable(),
		field.Other("price", decimal.Decimal{}).
			SchemaType(map[string]string{
				dialect.Postgres: "decimal(10,2)",
			}),
		field.String("unit").MaxLen(20),
		field.String("rank_range").MaxLen(100).Optional().Nillable(),
		field.Other("availability", types.TextArray{}).SchemaType(map[string]string{
			dialect.Postgres: "text[]",
		}).Optional().Nillable().Default([]string{}),
		field.Other("rating", decimal.Decimal{}).
			Default(decFive).
			SchemaType(map[string]string{
				dialect.Postgres: "decimal(3,2)",
			}),
		field.Bool("is_active").Optional().Default(true),
		field.Time("created_at").Default(time.Now).Immutable(),
		field.Time("updated_at").Default(time.Now).UpdateDefault(time.Now),
	}
}

// Edges of the PlayerServices.
func (PlayerServices) Edges() []ent.Edge {
	return nil
}
