package admin

import (
	"strconv"

	"github.com/gofiber/fiber/v2"

	packageService "github.com/break/junhong_cmp_fiber/internal/service/package"
	"github.com/break/junhong_cmp_fiber/pkg/errors"
	"github.com/break/junhong_cmp_fiber/pkg/response"
)

// PackageUsageHandler 套餐使用记录 Handler
type PackageUsageHandler struct {
	dailyRecordService *packageService.DailyRecordService
}

// NewPackageUsageHandler 创建套餐使用记录 Handler
func NewPackageUsageHandler(dailyRecordService *packageService.DailyRecordService) *PackageUsageHandler {
	return &PackageUsageHandler{
		dailyRecordService: dailyRecordService,
	}
}

// GetDailyRecords 任务 16.2-16.5: 获取套餐流量详单
// GET /api/admin/package-usage/:id/daily-records
// 查询参数：start_date（开始日期，格式 YYYY-MM-DD）, end_date（结束日期，格式 YYYY-MM-DD）
func (h *PackageUsageHandler) GetDailyRecords(c *fiber.Ctx) error {
	// 解析套餐使用记录 ID
	id, err := strconv.ParseUint(c.Params("id"), 10, 64)
	if err != nil {
		return errors.New(errors.CodeInvalidParam, "无效的套餐使用记录 ID")
	}

	// 任务 16.3: 解析日期范围查询参数
	startDate := c.Query("start_date", "")
	endDate := c.Query("end_date", "")

	// 任务 16.4: 调用 DailyRecordService.GetDailyRecords 获取日记录
	records, err := h.dailyRecordService.GetDailyRecords(c.UserContext(), uint(id), startDate, endDate)
	if err != nil {
		return err
	}

	// 任务 16.5: 返回 PackageUsageDetailResponse 响应
	return response.Success(c, records)
}
