package renderer

import (
	"encoding/json"
	"html/template"
	"net/http"

	fleetsstorage "git.coopgo.io/coopgo-platform/fleets/storage"
	mobilityaccountsstorage "git.coopgo.io/coopgo-platform/mobility-accounts/storage"
	solidarity_service "git.coopgo.io/sbouaram/solidarity-service/servers/grpc/proto"
)

const beneficiariesMenu = "beneficiaries"

type BeneficiariesListState struct {
	Count         int                               `json:"count"`
	CacheId       string                            `json:"cache_id"`
	Beneficiaries []mobilityaccountsstorage.Account `json:"beneficiaries"`
	BeneficiariesSolidarity *solidarity_service.GetAllPassengersResponse `json:"beneficiariessolidarity"`
}

type BeneficiarySolidarityState struct {
	Count         int                               `json:"count"`
	CacheId       string                            `json:"cache_id"`
	Beneficiaries []mobilityaccountsstorage.Account `json:"beneficiaries"`
	BeneficiarySolidarity *solidarity_service.GetPassengerResponse `json:"beneficiarysolidarity"`
	BookingSolidarity *solidarity_service.GetBookingsByStatusResponse `json:"bookingsolidarity"`

}

func (s BeneficiariesListState) JSON() template.JS {
	result, _ := json.Marshal(s)
	return template.JS(result)
}

func (s BeneficiariesListState) JSONWithLimits(a int, b int) template.JS {
	if b < len(s.Beneficiaries) { 
		s.Beneficiaries = s.Beneficiaries[a:b]
	}
	return s.JSON()
}

func (s BeneficiarySolidarityState) JSON() template.JS {
	result, _ := json.Marshal(s)
	return template.JS(result)
}

func (s BeneficiarySolidarityState) JSONWithLimits(a int, b int) template.JS {
	if b < 5 { 
		s.Beneficiaries = s.Beneficiaries[a:b]
	}
	return s.JSON()
}

func (renderer *Renderer) BeneficiariesList(w http.ResponseWriter, r *http.Request, accounts []mobilityaccountsstorage.Account,cacheid string) {
	files := renderer.ThemeConfig.GetStringSlice("views.beneficiaries.list.files")

	state := NewState(r, renderer.ThemeConfig, beneficiariesMenu)
	state.ViewState = BeneficiariesListState{
		Count:         len(accounts), 
		CacheId:       cacheid,
		Beneficiaries: accounts,
	}

	renderer.Render("beneficiaries_list", w, r, files, state)
}

func (renderer *Renderer) BeneficiaryCreate(w http.ResponseWriter, r *http.Request) {
	files := renderer.ThemeConfig.GetStringSlice("views.beneficiaries.create.files")
	state := NewState(r, renderer.ThemeConfig, beneficiariesMenu)

	renderer.Render("beneficiaries_create", w, r, files, state)
}

type BeneficiariesDisplayState struct {
	Beneficiary any
}

func (renderer *Renderer) BeneficiaryDisplay(w http.ResponseWriter, r *http.Request, beneficiary any, bookings []fleetsstorage.Booking, organizations []any, beneficiaries_file_types []string, file_types_map map[string]string, documents any, event interface{}, solidarity_bookings_list []any) {
	files := renderer.ThemeConfig.GetStringSlice("views.beneficiaries.display.files")
	state := NewState(r, renderer.ThemeConfig, beneficiariesMenu)
	state.ViewState = map[string]any{
		"beneficiary":              beneficiary,
		"bookings":                 bookings,
		"beneficiaries_file_types": beneficiaries_file_types,
		"file_types_map":           file_types_map,
		"documents":                documents,
		"organizations":            organizations,
		"event":                    event, 
		"bookingSolidarity" : solidarity_bookings_list,
	}
	renderer.Render("beneficiaries_display", w, r, files, state)
}

func (renderer *Renderer) BeneficiaryUpdate(w http.ResponseWriter, r *http.Request, beneficiary any) {
	files := renderer.ThemeConfig.GetStringSlice("views.beneficiaries.update.files")
	state := NewState(r, renderer.ThemeConfig, beneficiariesMenu)
	state.ViewState = beneficiary

	renderer.Render("beneficiaries_update", w, r, files, state)
}
