package mysql

import (
	"gorm.io/gorm"

	"github.com/pingcap-inc/data-ingestion-tools/model"
)

type cloudBilling struct {
	db *gorm.DB
}

func newCloudBilling(ds *datastore) *cloudBilling {
	return &cloudBilling{db: ds.db}
}

func (a *cloudBilling) ListOverSeaByMonth(cloudProvider, yearMonth string, offset, pageSize int) (records []model.CloudBillingOverSea, total int64, err error) {
	tx := a.db.Model(&model.CloudBillingOverSea{})
	if yearMonth != "" {
		tx = tx.Where("billing_month = ?", yearMonth)
	}
	if cloudProvider != "" {
		tx = tx.Where("cloud_provider = ?", cloudProvider)
	}
	err = tx.Count(&total).Error
	if err != nil {
		return
	}

	if offset != 0 {
		tx = tx.Offset(offset)
	}
	if pageSize != 0 {
		tx = tx.Limit(pageSize)
	}

	err = tx.Find(&records).Error

	return
}

func (a *cloudBilling) ListDomesticByMonth(cloudProvider, yearMonth string, offset, pageSize int) (records []model.CloudBillingDomestic, total int64, err error) {
	tx := a.db.Model(&model.CloudBillingDomestic{})
	if yearMonth != "" {
		tx = tx.Where("billing_month = ?", yearMonth)
	}
	if cloudProvider != "" {
		tx = tx.Where("cloud_provider = ?", cloudProvider)
	}
	err = tx.Count(&total).Error
	if err != nil {
		return
	}

	if offset != 0 {
		tx = tx.Offset(offset)
	}
	if pageSize != 0 {
		tx = tx.Limit(pageSize)
	}

	err = tx.Find(&records).Error

	return
}

func (a *cloudBilling) ListAccountByMonth(cloudProvider, yearMonth string, offset, pageSize int) (records []model.CloudRevrecAccount, total int64, err error) {
	tx := a.db.Model(&model.CloudRevrecAccount{})
	if yearMonth != "" {
		tx = tx.Where("billing_month = ?", yearMonth)
	}
	if cloudProvider != "" {
		tx = tx.Where("cloud_provider = ?", cloudProvider)
	}
	err = tx.Count(&total).Error
	if err != nil {
		return
	}

	if offset != 0 {
		tx = tx.Offset(offset)
	}
	if pageSize != 0 {
		tx = tx.Limit(pageSize)
	}

	err = tx.Find(&records).Error

	return
}

func (a *cloudBilling) SaveOverSeaByMonth(records []*model.CloudBillingOverSea, yearMonth string, cloudProvider string) error {
	// del current month records
	err := a.db.Unscoped().Where("billing_month = ? and cloud_provider = ?", yearMonth, cloudProvider).Delete(&model.CloudBillingOverSea{}).Error
	if err != nil {
		return err
	}
	// add new records
	err = a.db.Create(&records).Error

	return err
}

func (a *cloudBilling) SaveDomesticByMonth(records []*model.CloudBillingDomestic, yearMonth string, cloudProvider string) error {
	// del current month records
	err := a.db.Unscoped().Where("billing_month = ? and cloud_provider = ?", yearMonth, cloudProvider).Delete(&model.CloudBillingDomestic{}).Error
	if err != nil {
		return err
	}
	// add new records
	err = a.db.Create(&records).Error

	return err
}

func (a *cloudBilling) SaveAccountByMonth(records []*model.CloudRevrecAccount, yearMonth string) error {
	// del current month records
	err := a.db.Unscoped().Where("billing_month = ?", yearMonth).Delete(&model.CloudRevrecAccount{}).Error
	if err != nil {
		return err
	}
	// add new records
	err = a.db.Create(&records).Error

	return err
}
