package schema

import (
	"time"

	"juwan-backend/pkg/types"

	"entgo.io/ent"
	"entgo.io/ent/dialect"
	"entgo.io/ent/schema/field"
	"entgo.io/ent/schema/index"
)

type Posts struct {
	ent.Schema
}

func (Posts) Fields() []ent.Field {
	return []ent.Field{
		field.Int64("id").Unique().Immutable(),
		field.Int64("author_id"),
		field.String("author_role").MaxLen(20).Default("consumer"),
		field.String("title").MaxLen(500),
		field.String("content"),
		field.Other("images", types.TextArray{}).
			SchemaType(map[string]string{dialect.Postgres: "text[]"}).
			Optional(),
		field.Other("tags", types.TextArray{}).
			SchemaType(map[string]string{dialect.Postgres: "text[]"}).
			Optional(),
		field.Int64("linked_order_id").Optional().Nillable(),
		field.Int64("quoted_post_id").Optional().Nillable(),
		field.Int("like_count").Optional().Default(0),
		field.Int("comment_count").Optional().Default(0),
		field.Bool("pinned").Optional().Default(false),
		field.Time("created_at").Default(time.Now).Immutable(),
		field.Time("updated_at").Default(time.Now).UpdateDefault(time.Now),
		field.Time("deleted_at").Optional().Nillable(),
	}
}

func (Posts) Indexes() []ent.Index {
	return []ent.Index{
		index.Fields("author_id", "created_at"),
		index.Fields("created_at"),
		index.Fields("linked_order_id"),
		index.Fields("quoted_post_id"),
	}
}

func (Posts) Edges() []ent.Edge {
	return nil
}
