package model

import (
	"database/sql"
	"time"

	"cloud.google.com/go/bigquery"
	"gorm.io/gorm"
)

type OrgBq struct {
	BillMonth                  bigquery.NullString  `bigquery:"bill_month" json:"bill_month"`
	StartDate                  bigquery.NullDate    `bigquery:"start_date" json:"start_date"`
	EndDate                    bigquery.NullDate    `bigquery:"end_date" json:"end_date"`
	AdjustTenantID             bigquery.NullString  `bigquery:"adjust_tenant_id" json:"adjust_tenant_id"`
	OriginalTenantID           bigquery.NullString  `bigquery:"original_tenant_id" json:"original_tenant_id"`
	AccountID                  bigquery.NullString  `bigquery:"account_id" json:"account_id"`
	AccountName                bigquery.NullString  `bigquery:"account_name" json:"account_name"`
	MarketplaceAmount          bigquery.NullFloat64 `bigquery:"marketplace_amount" json:"marketplace_amount"`
	StripeAmount               bigquery.NullString  `bigquery:"stripe_amount" json:"stripe_amount"`
	WireTransferAmount         bigquery.NullFloat64 `bigquery:"wire_transfer_amount" json:"wire_transfer_amount"`
	TotalAmount                bigquery.NullFloat64 `bigquery:"total_amount" json:"total_amount"`
	StripeTaxRate              bigquery.NullString  `bigquery:"stripe_tax_rate" json:"stripe_tax_rate"`
	StripeAmountIncludingTax   bigquery.NullString  `bigquery:"stripe_amount_including_tax" json:"stripe_amount_including_tax"`
	StripeInvoiceNumber        bigquery.NullString  `bigquery:"stripe_invoice_number" json:"stripe_invoice_number"`
	StripeInvoice              bigquery.NullString  `bigquery:"stripe_invoice" json:"stripe_invoice"`
	StripeInvoicePaid          bigquery.NullString  `bigquery:"stripe_invoice_paid" json:"stripe_invoice_paid"`
	StripeInvoiceFinalizedDate bigquery.NullString  `bigquery:"stripe_invoice_finalized_date" json:"stripe_invoice_finalized_date"`
	StripeInvoicePaidDate      bigquery.NullString  `bigquery:"stripe_invoice_paid_date" json:"stripe_invoice_paid_date"`
	InvoiceAmount              bigquery.NullFloat64 `bigquery:"invoice_amount" json:"invoice_amount"`
	NeedNewOrder               bigquery.NullString  `bigquery:"need_new_order" json:"need_new_order"`
	NewOrderStartDate          bigquery.NullDate    `bigquery:"new_order_start_date" json:"new_order_start_date"`
	NewOrderEndDate            bigquery.NullDate    `bigquery:"new_order_end_date" json:"new_order_end_date"`
	NewOrderAmount             bigquery.NullFloat64 `bigquery:"new_order_amount" json:"new_order_amount"`
	OrderNumber                bigquery.NullString  `bigquery:"order_number" json:"order_number"`
	OrderStartDate             bigquery.NullDate    `bigquery:"order_start_date" json:"order_start_date"`
	OrderEndDate               bigquery.NullDate    `bigquery:"order_end_date" json:"order_end_date"`
	RevenueAmount              bigquery.NullFloat64 `bigquery:"revenue_amount" json:"revenue_amount"`
	OrderNumber2               bigquery.NullString  `bigquery:"order_number2" json:"order_number2"`
	OrderStartDate2            bigquery.NullDate    `bigquery:"order_start_date2" json:"order_start_date2"`
	OrderEndDate2              bigquery.NullDate    `bigquery:"order_end_date2" json:"order_end_date2"`
	RevenueAmount2             bigquery.NullFloat64 `bigquery:"revenue_amount2" json:"revenue_amount2"`
	CollectionsFrom            bigquery.NullString  `bigquery:"collections_from" json:"collections_from"`
	NewOrderNumber             bigquery.NullString  `bigquery:"new_order_number" json:"new_order_number"`
	NewOrderRevrecNumber       bigquery.NullString  `bigquery:"new_order_revrec_number" json:"new_order_revrec_number"`
	OrderFormRevrecNumber      bigquery.NullString  `bigquery:"order_form_revrec_number" json:"order_form_revrec_number"`
	OrderForm2RevrecNumber     bigquery.NullString  `bigquery:"order_form2_revrec_number" json:"order_form2_revrec_number"`
	CollectionsNumber          bigquery.NullString  `bigquery:"collections_number" json:"collections_number"`
	AutoMateType               bigquery.NullString  `bigquery:"automate_type" json:"automate_type"`

	RawServerlessAmount bigquery.NullFloat64 `bigquery:"raw_serverless_amount" json:"raw_serverless_amount"`
	RawDedicatedAmount  bigquery.NullFloat64 `bigquery:"raw_dedicated_amount" json:"raw_dedicated_amount"`
	RawSupportAmount    bigquery.NullFloat64 `bigquery:"raw_support_amount" json:"raw_support_amount"`

	// new_order_serverless_amount
	NewOrderServerlessAmount bigquery.NullFloat64 `bigquery:"new_order_serverless_amount" json:"new_order_serverless_amount"`
	// new_order_dedicated_amount
	NewOrderDedicatedAmount bigquery.NullFloat64 `bigquery:"new_order_dedicated_amount" json:"new_order_dedicated_amount"`
	// new_order_support_amount
	NewOrderSupportAmount bigquery.NullFloat64 `bigquery:"new_order_support_amount" json:"new_order_support_amount"`

	ServerlessRevenueAmount bigquery.NullFloat64 `bigquery:"serverless_revenue_amount" json:"serverless_revenue_amount"`
	DedicatedRevenueAmount  bigquery.NullFloat64 `bigquery:"dedicated_revenue_amount" json:"dedicated_revenue_amount"`
	SupportRevenueAmount    bigquery.NullFloat64 `bigquery:"support_revenue_amount" json:"support_revenue_amount"`

	ServerlessRevenueAmount2 bigquery.NullFloat64 `bigquery:"serverless_revenue_amount2" json:"serverless_revenue_amount2"`
	DedicatedRevenueAmount2  bigquery.NullFloat64 `bigquery:"dedicated_revenue_amount2" json:"dedicated_revenue_amount2"`
	SupportRevenueAmount2    bigquery.NullFloat64 `bigquery:"support_revenue_amount2" json:"support_revenue_amount2"`
}

// Save 实现 bigquery.ValueSaver 接口
func (o *OrgBq) Save() (map[string]bigquery.Value, string, error) {
	return map[string]bigquery.Value{
		"bill_month":                    o.BillMonth,
		"adjust_tenant_id":              o.AdjustTenantID,
		"original_tenant_id":            o.OriginalTenantID,
		"start_date":                    o.StartDate,
		"end_date":                      o.EndDate,
		"account_id":                    o.AccountID,
		"account_name":                  o.AccountName,
		"marketplace_amount":            o.MarketplaceAmount,
		"stripe_amount":                 o.StripeAmount,
		"wire_transfer_amount":          o.WireTransferAmount,
		"total_amount":                  o.TotalAmount,
		"stripe_tax_rate":               o.StripeTaxRate,
		"stripe_amount_including_tax":   o.StripeAmountIncludingTax,
		"stripe_invoice_number":         o.StripeInvoiceNumber,
		"invoice_amount":                o.InvoiceAmount,
		"need_new_order":                o.NeedNewOrder,
		"new_order_start_date":          o.NewOrderStartDate,
		"new_order_end_date":            o.NewOrderEndDate,
		"new_order_amount":              o.NewOrderAmount,
		"order_number":                  o.OrderNumber,
		"order_start_date":              o.OrderStartDate,
		"order_end_date":                o.OrderEndDate,
		"revenue_amount":                o.RevenueAmount,
		"order_number2":                 o.OrderNumber2,
		"order_start_date2":             o.OrderStartDate2,
		"order_end_date2":               o.OrderEndDate2,
		"revenue_amount2":               o.RevenueAmount2,
		"stripe_invoice":                o.StripeInvoice,
		"stripe_invoice_paid":           o.StripeInvoicePaid,
		"stripe_invoice_finalized_date": o.StripeInvoiceFinalizedDate,
		"stripe_invoice_paid_date":      o.StripeInvoicePaidDate,
		"new_order_number":              o.NewOrderNumber,
		"new_order_revrec_number":       o.NewOrderRevrecNumber,
		"order_form_revrec_number":      o.OrderFormRevrecNumber,
		"order_form2_revrec_number":     o.OrderForm2RevrecNumber,
		"collections_number":            o.CollectionsNumber,
		"automate_type":                 o.AutoMateType,
	}, "", nil
}

type OrderBq struct {
	ID        bigquery.NullString `bigquery:"id" json:"id"`
	Name      bigquery.NullString `bigquery:"name" json:"name"`
	IsDeleted bigquery.NullBool   `bigquery:"is_deleted" json:"is_deleted"`
}

type AccountBq struct {
	ID          bigquery.NullString `bigquery:"id" json:"id"`
	AccountName bigquery.NullString `bigquery:"account_id_c" json:"account_id_c"`
}

type CreatedOrder struct {
	ID               uint64         `gorm:"primaryKey;autoIncrement:true" json:"id"`
	CreatedAt        time.Time      `gorm:"not null;default:CURRENT_TIMESTAMP" json:"created_at"`
	UpdatedAt        sql.NullTime   `gorm:"default:null" json:"updated_at,omitempty"`
	DeletedAt        gorm.DeletedAt `gorm:"default:null" json:"deleted_at,omitempty"`
	OrderStartDate   string         `gorm:"type:varchar(64);not null" json:"order_start_date"`
	OrderEndDate     string         `gorm:"type:varchar(64);not null" json:"order_end_date"`
	Status           string         `gorm:"type:varchar(20)" json:"status"`
	AccountID        string         `gorm:"type:varchar(255)" json:"account_id"`
	Tax              string         `gorm:"type:varchar(20)" json:"tax"`
	NetFee           string         `gorm:"type:varchar(64)" json:"net_fee"`
	BillMonth        string         `gorm:"type:varchar(20)" json:"bill_month"`
	OriginalTenantID string         `gorm:"type:varchar(128)" json:"original_tenant_id"`
	AdjustTenantID   string         `gorm:"type:varchar(128)" json:"adjust_tenant_id"`
	AccountNumber    string         `gorm:"type:varchar(128)" json:"account_number"`
	AccountName      string         `gorm:"type:varchar(64)" json:"account_name"`
	NewOrderID       string         `gorm:"type:varchar(128)" json:"new_order_id"`
	NewOrderNumber   string         `gorm:"type:varchar(128)" json:"new_order_number"`
	Sync             int            `gorm:"type:tinyint" json:"sync"`
	FakeNumber       string         `gorm:"type:varchar(128)" json:"fake_number"`
}

func (CreatedOrder) TableName() string {
	return "created_order"
}

type BiOrder struct {
	OrderStartDate   string ` json:"OrderStartDate"`
	OrderEndDate     string ` json:"OrderEndDate"`
	ServiceStartDate string ` json:"ServiceStartDate"`
	ServiceEndDate   string ` json:"ServiceEndDate"`
	Tax              string ` json:"Tax"`
	Status           string ` json:"Status"`
	NetFees          string ` json:"NetFees"`
	BillMonth        string ` json:"BillMonth"`
	OriginalTenantID string ` json:"OriginalTenantID"`
	AdjustTenantID   string ` json:"AdjustTenantID"`
	AccountNumber    string ` json:"AccountNumber"`
	AccountName      string ` json:"AccountName"`
	OrderNumber      string ` json:"OrderNumber"`
	CreatedAt        string ` json:"CreatedAt"`
}

type CreatedRevrec struct {
	ID                       uint64         `gorm:"primaryKey;autoIncrement:true" json:"id"`
	CreatedAt                time.Time      `gorm:"not null;default:CURRENT_TIMESTAMP" json:"created_at"`
	UpdatedAt                sql.NullTime   `gorm:"default:null" json:"updated_at,omitempty"`
	DeletedAt                gorm.DeletedAt `gorm:"default:null" json:"deleted_at,omitempty"`
	Date                     string         `gorm:"type:varchar(64);not null;default:0" json:"date"`
	BillingEndDate           string         `gorm:"type:varchar(64);not null" json:"billing_end_date"`
	Status                   string         `gorm:"type:varchar(64);not null" json:"status"`
	Amount                   string         `gorm:"type:varchar(64)" json:"amount"`
	Order                    string         `gorm:"type:varchar(128)" json:"order"`
	OrderNumber              string         `gorm:"type:varchar(128)" json:"order_number"`
	Type                     int            `gorm:"type:tinyint" json:"type"`
	BillMonth                string         `gorm:"type:varchar(20)" json:"bill_month"`
	OriginalTenantID         string         `gorm:"type:varchar(128)" json:"original_tenant_id"`
	AdjustTenantID           string         `gorm:"type:varchar(128)" json:"adjust_tenant_id"`
	AccountNumber            string         `gorm:"type:varchar(128)" json:"account_number"`
	AccountName              string         `gorm:"type:varchar(64)" json:"account_name"`
	NewOrderServerlessAmount string         `json:"new_order_serverless_amount"`
	NewOrderDedicatedAmount  string         `json:"new_order_dedicated_amount"`
	NewOrderSupportAmount    string         `json:"new_order_support_amount"`

	RevrecID     string `gorm:"type:varchar(128)" json:"revrec_id"`
	RevrecNumber string `gorm:"type:varchar(128)" json:"revrec_number"`
	Sync         int    `gorm:"type:tinyint" json:"sync"`
}

func (CreatedRevrec) TableName() string {
	return "created_revrec"
}

type BiRevrec struct {
	BillMonth               string `json:"BillMonth"`
	OriginalTenantID        string `json:"OriginalTenantID"`
	AdjustTenantID          string ` json:"AdjustTenantID"`
	AccountNumber           string ` json:"AccountNumber"`
	AccountName             string ` json:"AccountName"`
	OrderNumber             string `json:"OrderNumber"`
	RevrecNumber            string `json:"RevrecNumber"`
	Date                    string `json:"Date"`
	TiDBCloudBillingEndDate string `json:"TiDBCloudBillingEndDate"`
	Status                  string `json:"Status"`
	Amount                  string `json:"Amount"`
	CreatedAt               string ` json:"CreatedAt"`
	SupportAmount           string `json:"SupportAmount"`
	ServerlessAmount        string `json:"ServerlessAmount"`
	DedicatedAmount         string `json:"DedicatedAmount"`
}

type CreatedCollection struct {
	ID                uint64         `gorm:"primaryKey;autoIncrement:true" json:"id"`
	CreatedAt         time.Time      `gorm:"not null;default:CURRENT_TIMESTAMP" json:"created_at"`
	UpdatedAt         sql.NullTime   `gorm:"default:null" json:"updated_at,omitempty"`
	DeletedAt         gorm.DeletedAt `gorm:"default:null" json:"deleted_at,omitempty"`
	PayableAmount     string         `gorm:"type:varchar(64);not null;default:'0'" json:"payable_amount"`
	InvoiceNumber     string         `gorm:"type:varchar(128);not null" json:"invoice_number"`
	PayableDate       string         `gorm:"type:varchar(64)" json:"payable_date"`
	ActualBillingDate string         `gorm:"type:varchar(64)" json:"actual_billing_date"`
	CollectionsDate   string         `gorm:"type:varchar(20)" json:"collections_date"`
	CollectionStatus  string         `gorm:"type:varchar(20)" json:"collection_status"`
	CollectionsAmount string         `gorm:"type:varchar(20)" json:"collections_amount"`
	Files             string         `gorm:"type:varchar(255)" json:"files"`
	InvoiceStatus     string         `gorm:"type:varchar(20)" json:"invoice_status"`
	Order             string         `gorm:"type:varchar(128)" json:"order"`
	OrderNumber       string         `gorm:"type:varchar(128)" json:"order_number"`
	BillMonth         string         `gorm:"type:varchar(20)" json:"bill_month"`
	OriginalTenantID  string         `gorm:"type:varchar(255)" json:"original_tenant_id"`
	AdjustTenantID    string         `gorm:"type:varchar(128)" json:"adjust_tenant_id"`
	AccountNumber     string         `gorm:"type:varchar(128)" json:"account_number"`
	AccountName       string         `gorm:"type:varchar(64)" json:"account_name"`
	CollectionNumber  string         `gorm:"type:varchar(255)" json:"collection_number"`
	Sync              int            `gorm:"type:tinyint" json:"sync"`
	StripePaid        string         `gorm:"type:varchar(20)" json:"stripe_paid"`
	Type              int            `gorm:"type:tinyint" json:"type"`
}

func (CreatedCollection) TableName() string {
	return "created_collection"
}

type BiCollection struct {
	BillMonth         string `json:"BillMonth"`
	OriginalTenantID  string `json:"OriginalTenantID"`
	AdjustTenantID    string ` json:"AdjustTenantID"`
	AccountNumber     string ` json:"AccountNumber"`
	AccountName       string ` json:"AccountName"`
	OrderNumber       string `json:"OrderNumber"`
	CollectionNumber  string `json:"CollectionNumber"`
	InvoiceNumber     string `json:"InvoiceNumber"`
	PayableAmount     string `json:"PayableAmount"`
	PayableDate       string `json:"PayableDate"`
	ActualBillingDate string `json:"ActualBillingDate"`
	CollectionsDate   string `json:"CollectionDate"`
	CollectionStatus  string `json:"CollectionStatus"`
	CollectionsAmount string `json:"CollectionAmount"`
	Files             string `json:"Files"`
	InvoiceStatus     string `json:"InvoiceStatus"`
	StripePaid        string `json:"StripePaid"`
	CreatedAt         string ` json:"CreatedAt"`
}

type Approval struct {
	ID               uint64         `gorm:"primaryKey;autoIncrement:true" json:"id"`
	CreatedAt        time.Time      `gorm:"not null;default:CURRENT_TIMESTAMP" json:"created_at"`
	UpdatedAt        sql.NullTime   `gorm:"default:null" json:"updated_at,omitempty"`
	DeletedAt        gorm.DeletedAt `gorm:"default:null" json:"deleted_at,omitempty"`
	ApprovalInstance string         `gorm:"type:varchar(256);not null" json:"approval_instance"`
	Status           int            `gorm:"type:tinyint" json:"status"`
}

func (Approval) TableName() string {
	return "approval"
}

type BqOrder struct {
	ID        uint64         `gorm:"primaryKey;autoIncrement:true" json:"id"`
	CreatedAt time.Time      `gorm:"not null;default:CURRENT_TIMESTAMP" json:"created_at"`
	UpdatedAt sql.NullTime   `gorm:"default:null" json:"updated_at,omitempty"`
	DeletedAt gorm.DeletedAt `gorm:"default:null" json:"deleted_at,omitempty"`
	OrderID   string         `gorm:"type:varchar(255)" json:"order_id"`
	OrderName string         `gorm:"type:varchar(64)" json:"order_name"`
}

func (BqOrder) TableName() string {
	return "bq_order"
}

type BqAccount struct {
	ID          uint64         `gorm:"primaryKey;autoIncrement:true" json:"id"`
	CreatedAt   time.Time      `gorm:"not null;default:CURRENT_TIMESTAMP" json:"created_at"`
	UpdatedAt   sql.NullTime   `gorm:"default:null" json:"updated_at,omitempty"`
	DeletedAt   gorm.DeletedAt `gorm:"default:null" json:"deleted_at,omitempty"`
	AccountID   string         `gorm:"type:varchar(255)" json:"account_id"`
	AccountName string         `gorm:"type:varchar(64)" json:"account_name"`
}

func (BqAccount) TableName() string {
	return "bq_account"
}

type CreatedOrderItem struct {
	Attributes struct {
		Type string `json:"type"`
	} `json:"attributes"`
	PricebookEntryID string `json:"PricebookEntryId"`
	Quantity         string `json:"quantity"`
	UnitPrice        string `json:"UnitPrice"`
}

type Attributes struct {
	Type string `json:"type"`
	Url  string `json:"url"`
}
type CreatedOrderReq struct {
	OrderStartDate         string `json:"EffectiveDate"`
	OrderEndDate           string `json:"Order_End_Date__c"`
	Status                 string `json:"Status"`
	AccountID              string `json:"accountId"`
	Tax                    string `json:"Tax__c"`
	PriceBook2ID           string `json:"Pricebook2Id"`
	ProductFamily          string `json:"Product_Family__c"`
	AccumulateRevenueToOrg string `json:"Accumulate_revenue_to_Org__c"`
}

type CreatedOrders struct {
	Order []CreatedOrderReq `json:"order"`
}

type CreatedOrderResp struct {
	ID      string        `json:"id"`
	Success bool          `json:"success"`
	Errors  []interface{} `json:"errors"`
}

type CreatedOrderProductReq struct {
	ServiceStartDate string `json:"Service_Start_Date__c"`
	ServiceEndDate   string `json:"Service_End_Date__c"`
	NetFee           string `json:"Net_fees__c"`
	Order            string `json:"Order__c"`
	Product          string `json:"Product__c"`
	Quantity         string `json:"Quantity__c"`
}

type CreatedRevrecReq struct {
	Order                    string `json:"Order__c,omitempty"`
	Date                     string `json:"Date__c,omitempty"`
	Status                   string `json:"Status__c,omitempty"`
	Amount                   string `json:"Amount__c,omitempty"`
	TiDBCloudBillingEndDate  string `json:"Billing_End_Date__c,omitempty"`
	Product                  string `json:"Product__c,omitempty"`
	NewOrderServerlessAmount string `json:"TiDB_Cloud_Serverless_Amount_Raw__c,omitempty"`
	NewOrderDedicatedAmount  string `json:"TiDB_Cloud_Dedicated_Amount_Raw__c,omitempty"`
	NewOrderSupportAmount    string `json:"TiDB_Cloud_Support_Amount__c,omitempty"`
}

type CreatedCollectionReq struct {
	Order             string `json:"Order__c" bigquery:"Order"`
	PayableAmount     string `json:"Payable_Amount__c" bigquery:"Payable_Amount"`
	InvoiceNumber     string `json:"Invoice_Number__c" bigquery:"Invoice_Number"`
	PayableDate       string `json:"Payable_Date__c,omitempty" bigquery:"Payable_Date"`
	ActualBillingDate string `json:"Actual_Billing_Date__c,omitempty" bigquery:"Actual_Billing_Date"`
	CollectionsDate   string `json:"Collections_Date__c,omitempty" bigquery:"Collections_Date"`
	CollectionStatus  string `json:"CollectionsStatus__c" bigquery:"CollectionsStatus__c"`
	CollectionsAmount string `json:"Collections_Amount__c,omitempty" bigquery:"Collections_Amount"`
	StripeInvoicePDF  string `json:"Stripe_Invoice_PDF__c" bigquery:"Stripe_Invoice_PDF"`
	InvoiceStatus     string `json:"Invoice_Status__c" bigquery:"Invoice_Status"`
}

type CommonSalesResp struct {
	ID      string        `json:"id"`
	Success bool          `json:"success"`
	Errors  []interface{} `json:"errors"`
}

type CommonGetResp struct {
	Name string `json:"Name"`
}

type CommonQueryResp struct {
	TotalSize int  `json:"totalSize"`
	Done      bool `json:"done"`
	Records   []struct {
		attributes       Attributes
		ID               string `json:"Id"`
		LastModifiedDate string `json:"LastModifiedDate"`
	}
}

type RunStatus struct {
	RunStatus string `json:"RunStatus"`
	RunTime   string `json:"RunTime"`
	Message   string `json:"Message"`
}
