package handler

import (
	"Engine/internal/model"
	"Engine/internal/service"
	"context"
	"encoding/json"
	"net/http"
)

type ReportHandler struct {
	svc *service.ReportService
}

func NewReportHandler(svc *service.ReportService) *ReportHandler {
	return &ReportHandler{svc: svc}
}

func (report *ReportHandler) PnL(w http.ResponseWriter, r *http.Request) {
	var req model.PnLRequest
	if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
		writeError(w, http.StatusBadRequest, "invalid request body")
		return
	}

	for _, year := range req.FiscalYears {
		for _, period := range req.FiscalPeriods {
			for _, Department := range req.DeptCodes {
				report.svc.CreatePnL(context.TODO(), service.PnLFilter{
					FiscalYear:   year,
					FiscalPeriod: period,
					DeptCode:     Department,
					Version:      req.Version,
				})

			}
		}
	}

}
