package pgtype

import (
	
	
	
	

	
	
)

const (
	microsecFromUnixEpochToY2K = 946_684_800 * 1_000_000
)

const (
	negativeInfinityMicrosecondOffset = -9223372036854775808
	infinityMicrosecondOffset         = 9223372036854775807
)

type TimestamptzScanner interface {
	ScanTimestamptz(v Timestamptz) error
}

type TimestamptzValuer interface {
	TimestamptzValue() (Timestamptz, error)
}

// Timestamptz represents the PostgreSQL timestamptz type.
type Timestamptz struct {
	Time             time.Time
	InfinityModifier InfinityModifier
	Valid            bool
}

// ScanTimestamptz implements the [TimestamptzScanner] interface.
func ( *Timestamptz) ( Timestamptz) error {
	* = 
	return nil
}

// TimestamptzValue implements the [TimestamptzValuer] interface.
func ( Timestamptz) () (Timestamptz, error) {
	return , nil
}

// Scan implements the [database/sql.Scanner] interface.
func ( *Timestamptz) ( any) error {
	if  == nil {
		* = Timestamptz{}
		return nil
	}

	switch src := .(type) {
	case string:
		return (&scanPlanTextTimestamptzToTimestamptzScanner{}).Scan([]byte(), )
	case time.Time:
		* = Timestamptz{Time: , Valid: true}
		return nil
	}

	return fmt.Errorf("cannot scan %T", )
}

// Value implements the [database/sql/driver.Valuer] interface.
func ( Timestamptz) () (driver.Value, error) {
	if !.Valid {
		return nil, nil
	}

	if .InfinityModifier != Finite {
		return .InfinityModifier.String(), nil
	}
	return .Time, nil
}

// MarshalJSON implements the [encoding/json.Marshaler] interface.
func ( Timestamptz) () ([]byte, error) {
	if !.Valid {
		return []byte("null"), nil
	}

	var  string

	switch .InfinityModifier {
	case Finite:
		 = .Time.Format(time.RFC3339Nano)
	case Infinity:
		 = "infinity"
	case NegativeInfinity:
		 = "-infinity"
	}

	return json.Marshal()
}

// UnmarshalJSON implements the [encoding/json.Unmarshaler] interface.
func ( *Timestamptz) ( []byte) error {
	var  *string
	 := json.Unmarshal(, &)
	if  != nil {
		return 
	}

	if  == nil {
		* = Timestamptz{}
		return nil
	}

	switch * {
	case "infinity":
		* = Timestamptz{Valid: true, InfinityModifier: Infinity}
	case "-infinity":
		* = Timestamptz{Valid: true, InfinityModifier: -Infinity}
	default:
		// PostgreSQL uses ISO 8601 for to_json function and casting from a string to timestamptz
		,  := time.Parse(time.RFC3339Nano, *)
		if  != nil {
			return 
		}

		* = Timestamptz{Time: , Valid: true}
	}

	return nil
}

type TimestamptzCodec struct {
	// ScanLocation is the location to return scanned timestamptz values in. This does not change the instant in time that
	// the timestamptz represents.
	ScanLocation *time.Location
}

func (*TimestamptzCodec) ( int16) bool {
	return  == TextFormatCode ||  == BinaryFormatCode
}

func (*TimestamptzCodec) () int16 {
	return BinaryFormatCode
}

func (*TimestamptzCodec) ( *Map,  uint32,  int16,  any) EncodePlan {
	if ,  := .(TimestamptzValuer); ! {
		return nil
	}

	switch  {
	case BinaryFormatCode:
		return encodePlanTimestamptzCodecBinary{}
	case TextFormatCode:
		return encodePlanTimestamptzCodecText{}
	}

	return nil
}

type encodePlanTimestamptzCodecBinary struct{}

func (encodePlanTimestamptzCodecBinary) ( any,  []byte) ( []byte,  error) {
	,  := .(TimestamptzValuer).TimestamptzValue()
	if  != nil {
		return nil, 
	}

	if !.Valid {
		return nil, nil
	}

	var  int64
	switch .InfinityModifier {
	case Finite:
		 := .Time.Unix()*1000000 + int64(.Time.Nanosecond())/1000
		 =  - microsecFromUnixEpochToY2K
	case Infinity:
		 = infinityMicrosecondOffset
	case NegativeInfinity:
		 = negativeInfinityMicrosecondOffset
	}

	 = pgio.AppendInt64(, )

	return , nil
}

type encodePlanTimestamptzCodecText struct{}

func (encodePlanTimestamptzCodecText) ( any,  []byte) ( []byte,  error) {
	,  := .(TimestamptzValuer).TimestamptzValue()
	if  != nil {
		return nil, 
	}

	if !.Valid {
		return nil, nil
	}

	switch .InfinityModifier {
	case Finite:
		 = pgdatetime.AppendTimestamp(, .Time.UTC(), "Z")
	case Infinity:
		 = append(, "infinity"...)
	case NegativeInfinity:
		 = append(, "-infinity"...)
	}

	return , nil
}

func ( *TimestamptzCodec) ( *Map,  uint32,  int16,  any) ScanPlan {
	switch  {
	case BinaryFormatCode:
		if ,  := .(TimestamptzScanner);  {
			return &scanPlanBinaryTimestamptzToTimestamptzScanner{location: .ScanLocation}
		}
	case TextFormatCode:
		if ,  := .(TimestamptzScanner);  {
			return &scanPlanTextTimestamptzToTimestamptzScanner{location: .ScanLocation}
		}
	}

	return nil
}

type scanPlanBinaryTimestamptzToTimestamptzScanner struct{ location *time.Location }

func ( *scanPlanBinaryTimestamptzToTimestamptzScanner) ( []byte,  any) error {
	 := .(TimestamptzScanner)

	if  == nil {
		return .ScanTimestamptz(Timestamptz{})
	}

	,  := pgio.Uint64Exact()
	if  != nil {
		return fmt.Errorf("timestamptz: %w", )
	}

	var  Timestamptz
	 := int64()

	switch  {
	case infinityMicrosecondOffset:
		 = Timestamptz{Valid: true, InfinityModifier: Infinity}
	case negativeInfinityMicrosecondOffset:
		 = Timestamptz{Valid: true, InfinityModifier: -Infinity}
	default:
		 := time.Unix(
			microsecFromUnixEpochToY2K/1_000_000+/1_000_000,
			(microsecFromUnixEpochToY2K%1_000_000*1_000)+(%1_000_000*1_000),
		)
		if .Before(minDateTime) || !.Before(endTimestamp) {
			return fmt.Errorf("timestamptz %d microseconds from 2000-01-01 is out of range", )
		}
		if .location != nil {
			 = .In(.location)
		}
		 = Timestamptz{Time: , Valid: true}
	}

	return .ScanTimestamptz()
}

type scanPlanTextTimestamptzToTimestamptzScanner struct{ location *time.Location }

func ( *scanPlanTextTimestamptzToTimestamptzScanner) ( []byte,  any) error {
	 := .(TimestamptzScanner)

	if  == nil {
		return .ScanTimestamptz(Timestamptz{})
	}

	,  := parseTextDateTime()
	if  != nil {
		return 
	}

	var  Timestamptz
	if .infinity != Finite {
		 = Timestamptz{Valid: true, InfinityModifier: .infinity}
	} else {
		if !.hasTime || !.hasOffset {
			return badDateTime()
		}

		,  := .toTime(, "timestamptz", endTimestamp)
		if  != nil {
			return 
		}

		// The binary path builds its value with time.Unix, which returns time.Local. Match
		// it, so that the same value scanned in either format produces the same time.Time.
		 := time.Local
		if .location != nil {
			 = .location
		}

		 = Timestamptz{Time: .In(), Valid: true}
	}

	return .ScanTimestamptz()
}

func ( *TimestamptzCodec) ( *Map,  uint32,  int16,  []byte) (driver.Value, error) {
	if  == nil {
		return nil, nil
	}

	var  Timestamptz
	 := codecScan(, , , , , &)
	if  != nil {
		return nil, 
	}

	if .InfinityModifier != Finite {
		return .InfinityModifier.String(), nil
	}

	return .Time, nil
}

func ( *TimestamptzCodec) ( *Map,  uint32,  int16,  []byte) (any, error) {
	if  == nil {
		return nil, nil
	}

	var  Timestamptz
	 := codecScan(, , , , , &)
	if  != nil {
		return nil, 
	}

	if .InfinityModifier != Finite {
		return .InfinityModifier, nil
	}

	return .Time, nil
}