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 Disputes struct {
	ent.Schema
}

func (Disputes) Fields() []ent.Field {
	return []ent.Field{
		field.Int64("id").Unique().Immutable(),
		field.Int64("order_id").Unique(),
		field.Int64("initiator_id"),
		field.String("initiator_name").MaxLen(100),
		field.Int64("respondent_id"),
		field.String("reason"),
		field.Other("evidence", types.TextArray{}).
			SchemaType(map[string]string{dialect.Postgres: "text[]"}).
			Optional(),
		field.String("status").MaxLen(20).Default("open"),
		field.String("result").MaxLen(30).Optional().Nillable(),
		field.String("respondent_reason").Optional().Nillable(),
		field.Other("respondent_evidence", types.TextArray{}).
			SchemaType(map[string]string{dialect.Postgres: "text[]"}).
			Optional(),
		field.String("appeal_reason").Optional().Nillable(),
		field.Time("appealed_at").Optional().Nillable(),
		field.Int64("resolved_by").Optional().Nillable(),
		field.Time("resolved_at").Optional().Nillable(),
		field.Time("created_at").Default(time.Now).Immutable(),
		field.Time("updated_at").Default(time.Now).UpdateDefault(time.Now),
	}
}

func (Disputes) Indexes() []ent.Index {
	return []ent.Index{
		index.Fields("order_id"),
		index.Fields("status", "created_at"),
		index.Fields("initiator_id"),
		index.Fields("initiator_id", "status", "created_at"),
		index.Fields("respondent_id", "status", "created_at"),
	}
}

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