package qris

import (
	"api-service/internal/models"
	"database/sql"
	"encoding/json"
	"time"
)

// Qris represents the data structure for the qris table
// with proper null handling and optimized JSON marshaling
type Qris struct {
	ID            string               `json:"id" db:"id"`
	Status        string               `json:"status" db:"status"`
	Sort          models.NullableInt32 `json:"sort,omitempty" db:"sort"`
	PosDevice     sql.NullString       `json:"posdevice,omitempty" db:"posdevice"`
	CreatedAt     sql.NullTime         `json:"created_at,omitempty" db:"created_at"`
	InvoiceNumber sql.NullString       `json:"invoice_number,omitempty" db:"invoice_number"`
	UpdatedAt     sql.NullTime         `json:"updated_at,omitempty" db:"updated_at"`
	DisplayName   sql.NullString       `json:"display_name,omitempty" db:"display_name"`
	DisplayAmount float64              `json:"display_amount" db:"display_amount"`
	QrValue       string               `json:"qrvalue" db:"qrvalue"`
	IPAddress     string               `json:"ip" db:"ip"`
	DisplayNoBill string               `json:"display_nobill" db:"display_nobill"`
}

// Custom JSON marshaling untuk Qris agar NULL values tidak muncul di response
func (r Qris) MarshalJSON() ([]byte, error) {
	type Alias Qris
	aux := &struct {
		Sort          *int       `json:"sort,omitempty"`
		PosDevice     *string    `json:"posdevice,omitempty"`
		InvoiceNumber *string    `json:"invoice_number,omitempty"`
		UpdatedAt     *time.Time `json:"updated_at,omitempty"`
		CreatedAt     *time.Time `json:"created_at,omitempty"`
		DisplayName   *string    `json:"display_name,omitempty"`
		*Alias
	}{
		Alias: (*Alias)(&r),
	}

	if r.Sort.Valid {
		sort := int(r.Sort.Int32)
		aux.Sort = &sort
	}
	if r.PosDevice.Valid {
		aux.PosDevice = &r.PosDevice.String
	}
	if r.InvoiceNumber.Valid {
		aux.InvoiceNumber = &r.InvoiceNumber.String
	}
	if r.UpdatedAt.Valid {
		aux.UpdatedAt = &r.UpdatedAt.Time
	}
	if r.CreatedAt.Valid {
		aux.CreatedAt = &r.CreatedAt.Time
	}
	if r.DisplayName.Valid {
		aux.DisplayName = &r.DisplayName.String
	}
	return json.Marshal(aux)
}

// Helper methods untuk mendapatkan nilai yang aman
/*func (r *Qris) GetName() string {
	if r.DisplayName.Valid {
		return r.DisplayName.String
	}
	return ""
}*/

// Response struct untuk GET by ID
type QrisGetByIDResponse struct {
	Message string `json:"message"`
	Data    *Qris  `json:"data"`
}

// Response struct untuk GET by IP
type QrisGetByIPResponse struct {
	Message string `json:"message"`
	Data    []Qris `json:"data"`
}

// Enhanced GET response dengan pagination dan aggregation
type QrisGetResponse struct {
	Message string                `json:"message"`
	Data    []Qris                `json:"data"`
	Meta    models.MetaResponse   `json:"meta"`
	Summary *models.AggregateData `json:"summary,omitempty"`
}

// Filter struct untuk query parameters
type QrisFilter struct {
	Status      *string    `json:"status,omitempty" form:"status"`
	Search      *string    `json:"search,omitempty" form:"search"`
	DateFrom    *time.Time `json:"date_from,omitempty" form:"date_from"`
	DateTo      *time.Time `json:"date_to,omitempty" form:"date_to"`
	PosDevice   *string    `json:"posdevice,omitempty" form:"posdevice"`
	DisplayName *string    `json:"display_name,omitempty" form:"display_name"`
	IPAddress   *string    `json:"ip,omitempty" form:"ip"`
}
