// infrastructure/csv_exporter.go
package infrastructure

import (
	"Yimaru-Backend/internal/domain"
	"encoding/csv"
	"os"
	"strconv"
	"time"
)

type CSVExporter struct {
	ExportPath string
}

func (e *CSVExporter) Export(report *domain.Report) error {
	filename := e.ExportPath + "/report_" + string(report.TimeFrame) + "_" +
		time.Now().Format("20060102") + ".csv"

	file, err := os.Create(filename)
	if err != nil {
		return err
	}
	defer file.Close()

	writer := csv.NewWriter(file)
	defer writer.Flush()

	// Write header
	_ = writer.Write([]string{
		"Time Frame", "Period Start", "Period End",
		"Total Bets", "Total Cash In", "Total Cash Out", "Total Cash Back",
		"Generated At",
	})

	// Write data
	return writer.Write([]string{
		string(report.TimeFrame),
		report.PeriodStart.Format(time.RFC3339),
		report.PeriodEnd.Format(time.RFC3339),
		strconv.Itoa(report.TotalBets),
		strconv.FormatFloat(report.TotalCashIn, 'f', 2, 64),
		strconv.FormatFloat(report.TotalCashOut, 'f', 2, 64),
		strconv.FormatFloat(report.TotalCashBack, 'f', 2, 64),
		report.GeneratedAt.Format(time.RFC3339),
	})
}
