// Code generated by ent, DO NOT EDIT.

package ent

import (
	"encoding/json"
	"fmt"
	"strings"
	"time"

	"entgo.io/ent"
	"entgo.io/ent/dialect/sql"
	"github.com/Wei-Shaw/sub2api/ent/announcement"
	"github.com/Wei-Shaw/sub2api/internal/domain"
)

// Announcement is the model entity for the Announcement schema.
type Announcement struct {
	config `json:"-"`
	// ID of the ent.
	ID int64 `json:"id,omitempty"`
	// 公告标题
	Title string `json:"title,omitempty"`
	// 公告内容（支持 Markdown）
	Content string `json:"content,omitempty"`
	// 状态: draft, active, archived
	Status string `json:"status,omitempty"`
	// 通知模式: silent(仅铃铛), popup(弹窗提醒)
	NotifyMode string `json:"notify_mode,omitempty"`
	// 展示条件（JSON 规则）
	Targeting domain.AnnouncementTargeting `json:"targeting,omitempty"`
	// 开始展示时间（为空表示立即生效）
	StartsAt *time.Time `json:"starts_at,omitempty"`
	// 结束展示时间（为空表示永久生效）
	EndsAt *time.Time `json:"ends_at,omitempty"`
	// 创建人用户ID（管理员）
	CreatedBy *int64 `json:"created_by,omitempty"`
	// 更新人用户ID（管理员）
	UpdatedBy *int64 `json:"updated_by,omitempty"`
	// CreatedAt holds the value of the "created_at" field.
	CreatedAt time.Time `json:"created_at,omitempty"`
	// UpdatedAt holds the value of the "updated_at" field.
	UpdatedAt time.Time `json:"updated_at,omitempty"`
	// Edges holds the relations/edges for other nodes in the graph.
	// The values are being populated by the AnnouncementQuery when eager-loading is set.
	Edges        AnnouncementEdges `json:"edges"`
	selectValues sql.SelectValues
}

// AnnouncementEdges holds the relations/edges for other nodes in the graph.
type AnnouncementEdges struct {
	// Reads holds the value of the reads edge.
	Reads []*AnnouncementRead `json:"reads,omitempty"`
	// loadedTypes holds the information for reporting if a
	// type was loaded (or requested) in eager-loading or not.
	loadedTypes [1]bool
}

// ReadsOrErr returns the Reads value or an error if the edge
// was not loaded in eager-loading.
func (e AnnouncementEdges) ReadsOrErr() ([]*AnnouncementRead, error) {
	if e.loadedTypes[0] {
		return e.Reads, nil
	}
	return nil, &NotLoadedError{edge: "reads"}
}

// scanValues returns the types for scanning values from sql.Rows.
func (*Announcement) scanValues(columns []string) ([]any, error) {
	values := make([]any, len(columns))
	for i := range columns {
		switch columns[i] {
		case announcement.FieldTargeting:
			values[i] = new([]byte)
		case announcement.FieldID, announcement.FieldCreatedBy, announcement.FieldUpdatedBy:
			values[i] = new(sql.NullInt64)
		case announcement.FieldTitle, announcement.FieldContent, announcement.FieldStatus, announcement.FieldNotifyMode:
			values[i] = new(sql.NullString)
		case announcement.FieldStartsAt, announcement.FieldEndsAt, announcement.FieldCreatedAt, announcement.FieldUpdatedAt:
			values[i] = new(sql.NullTime)
		default:
			values[i] = new(sql.UnknownType)
		}
	}
	return values, nil
}

// assignValues assigns the values that were returned from sql.Rows (after scanning)
// to the Announcement fields.
func (_m *Announcement) assignValues(columns []string, values []any) error {
	if m, n := len(values), len(columns); m < n {
		return fmt.Errorf("mismatch number of scan values: %d != %d", m, n)
	}
	for i := range columns {
		switch columns[i] {
		case announcement.FieldID:
			value, ok := values[i].(*sql.NullInt64)
			if !ok {
				return fmt.Errorf("unexpected type %T for field id", value)
			}
			_m.ID = int64(value.Int64)
		case announcement.FieldTitle:
			if value, ok := values[i].(*sql.NullString); !ok {
				return fmt.Errorf("unexpected type %T for field title", values[i])
			} else if value.Valid {
				_m.Title = value.String
			}
		case announcement.FieldContent:
			if value, ok := values[i].(*sql.NullString); !ok {
				return fmt.Errorf("unexpected type %T for field content", values[i])
			} else if value.Valid {
				_m.Content = value.String
			}
		case announcement.FieldStatus:
			if value, ok := values[i].(*sql.NullString); !ok {
				return fmt.Errorf("unexpected type %T for field status", values[i])
			} else if value.Valid {
				_m.Status = value.String
			}
		case announcement.FieldNotifyMode:
			if value, ok := values[i].(*sql.NullString); !ok {
				return fmt.Errorf("unexpected type %T for field notify_mode", values[i])
			} else if value.Valid {
				_m.NotifyMode = value.String
			}
		case announcement.FieldTargeting:
			if value, ok := values[i].(*[]byte); !ok {
				return fmt.Errorf("unexpected type %T for field targeting", values[i])
			} else if value != nil && len(*value) > 0 {
				if err := json.Unmarshal(*value, &_m.Targeting); err != nil {
					return fmt.Errorf("unmarshal field targeting: %w", err)
				}
			}
		case announcement.FieldStartsAt:
			if value, ok := values[i].(*sql.NullTime); !ok {
				return fmt.Errorf("unexpected type %T for field starts_at", values[i])
			} else if value.Valid {
				_m.StartsAt = new(time.Time)
				*_m.StartsAt = value.Time
			}
		case announcement.FieldEndsAt:
			if value, ok := values[i].(*sql.NullTime); !ok {
				return fmt.Errorf("unexpected type %T for field ends_at", values[i])
			} else if value.Valid {
				_m.EndsAt = new(time.Time)
				*_m.EndsAt = value.Time
			}
		case announcement.FieldCreatedBy:
			if value, ok := values[i].(*sql.NullInt64); !ok {
				return fmt.Errorf("unexpected type %T for field created_by", values[i])
			} else if value.Valid {
				_m.CreatedBy = new(int64)
				*_m.CreatedBy = value.Int64
			}
		case announcement.FieldUpdatedBy:
			if value, ok := values[i].(*sql.NullInt64); !ok {
				return fmt.Errorf("unexpected type %T for field updated_by", values[i])
			} else if value.Valid {
				_m.UpdatedBy = new(int64)
				*_m.UpdatedBy = value.Int64
			}
		case announcement.FieldCreatedAt:
			if value, ok := values[i].(*sql.NullTime); !ok {
				return fmt.Errorf("unexpected type %T for field created_at", values[i])
			} else if value.Valid {
				_m.CreatedAt = value.Time
			}
		case announcement.FieldUpdatedAt:
			if value, ok := values[i].(*sql.NullTime); !ok {
				return fmt.Errorf("unexpected type %T for field updated_at", values[i])
			} else if value.Valid {
				_m.UpdatedAt = value.Time
			}
		default:
			_m.selectValues.Set(columns[i], values[i])
		}
	}
	return nil
}

// Value returns the ent.Value that was dynamically selected and assigned to the Announcement.
// This includes values selected through modifiers, order, etc.
func (_m *Announcement) Value(name string) (ent.Value, error) {
	return _m.selectValues.Get(name)
}

// QueryReads queries the "reads" edge of the Announcement entity.
func (_m *Announcement) QueryReads() *AnnouncementReadQuery {
	return NewAnnouncementClient(_m.config).QueryReads(_m)
}

// Update returns a builder for updating this Announcement.
// Note that you need to call Announcement.Unwrap() before calling this method if this Announcement
// was returned from a transaction, and the transaction was committed or rolled back.
func (_m *Announcement) Update() *AnnouncementUpdateOne {
	return NewAnnouncementClient(_m.config).UpdateOne(_m)
}

// Unwrap unwraps the Announcement entity that was returned from a transaction after it was closed,
// so that all future queries will be executed through the driver which created the transaction.
func (_m *Announcement) Unwrap() *Announcement {
	_tx, ok := _m.config.driver.(*txDriver)
	if !ok {
		panic("ent: Announcement is not a transactional entity")
	}
	_m.config.driver = _tx.drv
	return _m
}

// String implements the fmt.Stringer.
func (_m *Announcement) String() string {
	var builder strings.Builder
	builder.WriteString("Announcement(")
	builder.WriteString(fmt.Sprintf("id=%v, ", _m.ID))
	builder.WriteString("title=")
	builder.WriteString(_m.Title)
	builder.WriteString(", ")
	builder.WriteString("content=")
	builder.WriteString(_m.Content)
	builder.WriteString(", ")
	builder.WriteString("status=")
	builder.WriteString(_m.Status)
	builder.WriteString(", ")
	builder.WriteString("notify_mode=")
	builder.WriteString(_m.NotifyMode)
	builder.WriteString(", ")
	builder.WriteString("targeting=")
	builder.WriteString(fmt.Sprintf("%v", _m.Targeting))
	builder.WriteString(", ")
	if v := _m.StartsAt; v != nil {
		builder.WriteString("starts_at=")
		builder.WriteString(v.Format(time.ANSIC))
	}
	builder.WriteString(", ")
	if v := _m.EndsAt; v != nil {
		builder.WriteString("ends_at=")
		builder.WriteString(v.Format(time.ANSIC))
	}
	builder.WriteString(", ")
	if v := _m.CreatedBy; v != nil {
		builder.WriteString("created_by=")
		builder.WriteString(fmt.Sprintf("%v", *v))
	}
	builder.WriteString(", ")
	if v := _m.UpdatedBy; v != nil {
		builder.WriteString("updated_by=")
		builder.WriteString(fmt.Sprintf("%v", *v))
	}
	builder.WriteString(", ")
	builder.WriteString("created_at=")
	builder.WriteString(_m.CreatedAt.Format(time.ANSIC))
	builder.WriteString(", ")
	builder.WriteString("updated_at=")
	builder.WriteString(_m.UpdatedAt.Format(time.ANSIC))
	builder.WriteByte(')')
	return builder.String()
}

// Announcements is a parsable slice of Announcement.
type Announcements []*Announcement
