package models

import (
	"database/sql"
	"time"
)

type Invoice struct {
	BaseModel
	BuyerID            uint           `json:"buyerId"`
	BuyerName          string         `json:"buyerName"`
	BuyerAddress       string         `json:"buyerAddress"`
	BuyerEmail         string         `json:"buyerEmail"`
	BuyerPhone         string         `json:"buyerPhone"`
	SellerID           uint           `json:"sellerId"`
	SellerName         string         `json:"sellerName"`
	SellerAddress      string         `json:"sellerAddress"`
	SellerEmail        string         `json:"sellerEmail"`
	SellerPhone        string         `json:"sellerPhone"`
	PriceCents         int64          `json:"priceCents" gorm:"column:price_cents"`
	Discount           int64          `json:"discount"`
	Tax                int64          `json:"tax"`
	TermsAndConditions string         `json:"termsAndConditions"`
	InvoiceName        string         `json:"invoiceName"`
	InvoiceDate        time.Time      `json:"invoiceDate"`
	InvoiceDueDate     time.Time      `json:"invoiceDueDate"`
	ContractID         uint           `json:"contractId"`
	InvoiceItem        []InvoiceItem `json:"invoiceItem"`
	Status             string         `json:"status"`
}

type InvoiceResponse struct {
	BaseModel
	ID                 uint           `json:"id" gorm:"primaryKey"`
	CreatedAt          time.Time      `json:"createdAt"`
	UpdatedAt          time.Time      `json:"updatedAt"`
	DeletedAt          sql.NullTime   `json:"deletedAt" gorm:"index"`
	BuyerID            uint           `json:"buyerId"`
	BuyerName          string         `json:"buyerName"`
	BuyerAddress       string         `json:"buyerAddress"`
	BuyerEmail         string         `json:"buyerEmail"`
	BuyerPhone         string         `json:"buyerPhone"`
	SellerID           uint           `json:"sellerId"`
	SellerName         string         `json:"sellerName"`
	SellerAddress      string         `json:"sellerAddress"`
	SellerEmail        string         `json:"sellerEmail"`
	SellerPhone        string         `json:"sellerPhone"`
	PriceCents         int64          `json:"priceCents" gorm:"column:price_cents"`
	Discount           int64          `json:"discount"`
	Tax                int64          `json:"tax"`
	TermsAndConditions string         `json:"termsAndConditions"`
	InvoiceName        string         `json:"invoiceName"`
	InvoiceDate        time.Time      `json:"invoiceDate"`
	InvoiceDueDate     time.Time      `json:"invoiceDueDate"`
	ContractID         uint           `json:"contractId"`
	InvoiceItem        []InvoiceItem `json:"invoiceItem"`
	Status             string         `json:"status"`
}

func ConvertInvoiceToResponse(invoices []Invoice) []InvoiceResponse {
	invoiceResponses := []InvoiceResponse{}
	for _, invoice := range invoices {
		if invoice.InvoiceItem == nil {
			emptySlice := make([]InvoiceItem, 0)
			invoice.InvoiceItem = emptySlice
		}

		invoiceResponse := InvoiceResponse{
			BaseModel: BaseModel{
				ID:        invoice.ID,
				CreatedAt: invoice.CreatedAt,
				UpdatedAt: invoice.UpdatedAt,
			},
			BuyerID:            invoice.BuyerID,
			BuyerName:          invoice.BuyerName,
			BuyerAddress:       invoice.BuyerAddress,
			BuyerEmail:         invoice.BuyerEmail,
			BuyerPhone:         invoice.BuyerPhone,
			SellerID:           invoice.SellerID,
			SellerName:         invoice.SellerName,
			SellerAddress:      invoice.SellerAddress,
			SellerEmail:        invoice.SellerEmail,
			SellerPhone:        invoice.SellerPhone,
			PriceCents:         invoice.PriceCents,
			Discount:           invoice.Discount,
			Tax:                invoice.Tax,
			TermsAndConditions: invoice.TermsAndConditions,
			InvoiceName:        invoice.InvoiceName,
			InvoiceDate:        invoice.InvoiceDate,
			InvoiceDueDate:     invoice.InvoiceDueDate,
			ContractID:         invoice.ContractID,
			InvoiceItem:        invoice.InvoiceItem,
			Status:             invoice.Status,
		}
		invoiceResponses = append(invoiceResponses, invoiceResponse)
	}
	return invoiceResponses
}

type ListInvoiceResponse struct {
	Status      KeyValue              `json:"status"`
	Buyer       CompanyShortResponse `json:"buyer"`
	ContractID  int                   `json:"contractId"`
	DateCreated time.Time             `json:"dateCreated"`
	DueDate     time.Time             `json:"dueDate"`
	Amount      string                `json:"amount"`
}
type KeyValue struct {
	Key   string `json:"key"`
	Value string `json:"value"`
}

type ActiveInvoiceResponse struct {
	Claimed int64	`json:"issued"`
	Issued int64	`json:"claimed"`
	MonthlyInvoices  map[string]map[string]int64 `json:"monthly"`
}

func GetInvoiceStatuses() []Status {
	return []Status{
		{Key: "Insurance claimed", Value: "insurance_claimed"},
		{Key: "Invoice issued", Value: "invoice_issued"},
	}
}

func (Invoice) Update() (bool, error) {
	return false, nil
}
func (Invoice) Create() (bool, error) {
	return false, nil
}
func (Invoice) Delete() (bool, error) {
	return false, nil
}
