package report

import (
	"github.com/pingcap/errors"
)

type reportOverview struct {
	ReportID   string `json:"report_id"`
	ReportType string `json:"report_type"`
	Status     string `json:"status"`
	Progress   uint   `json:"progress"`
	Args       any    `json:"args"`
	Creator    string `json:"creator"`
	CreatedAt  uint   `json:"createdAt"`
}

type TouchReportRequest struct {
	// these field exist for validate, other field will also pass to compute-engine
	BeginTime uint   `json:"begin_ts"`
	EndTime   uint   `json:"end_ts"`
	PackageID string `json:"package_id"`
}

type TouchReportResponse struct {
	// Status   string `json:"status"`
	ReportID string `json:"report_id"`
}

type ListReportResponse struct {
	Total   int64            `json:"total"`
	Reports []reportOverview `json:"reports"`
}

type GetReportResponse struct {
	Creator   string `json:"creator"`
	Status    string `json:"status"`
	Progress  uint   `json:"progress"`
	Args      any    `json:"args"`
	Result    any    `json:"data"`
	CreatedAt uint   `json:"createdAt"`
}

type PutReportRequest struct {
	Status   string `json:"status"`
	Progress uint   `json:"progress"`
	Result   any    `json:"data"`
}

type ReportStatistics struct {
	Count int `json:"count"`
}

func (req *TouchReportRequest) Validate(orgType string) error {
	switch orgType {
	case "cloud":
		if req.BeginTime == 0 {
			return errors.New("begin_time is zero")
		}
		if req.EndTime == 0 {
			return errors.New("end_time is zero")
		}
		if req.EndTime-req.BeginTime > 24*60*60 || req.EndTime < req.BeginTime {
			return errors.New("time range should not be more than 24 hours")
		}
	case "op":
		if req.PackageID == "" {
			return errors.New("package_id is empty")
		}
	}

	return nil
}
