package application

import (
	"context"
	"crypto/rand"
	"encoding/base64"
	"fmt"
	"io"
	"net/http"
	"sort"
	"sync"
	"time"

	"git.coopgo.io/coopgo-apps/parcoursmob/utils/identification"
	"git.coopgo.io/coopgo-apps/parcoursmob/utils/sorting"
	agenda "git.coopgo.io/coopgo-platform/agenda/grpcapi"
	agendastorage "git.coopgo.io/coopgo-platform/agenda/storage"
	fleets "git.coopgo.io/coopgo-platform/fleets/grpcapi"
	fleetsstorage "git.coopgo.io/coopgo-platform/fleets/storage"
	groupsmanagement "git.coopgo.io/coopgo-platform/groups-management/grpcapi"
	"git.coopgo.io/coopgo-platform/groups-management/storage"
	groupstorage "git.coopgo.io/coopgo-platform/groups-management/storage"
	accounts "git.coopgo.io/coopgo-platform/mobility-accounts/grpcapi"
	mobilityaccountsstorage "git.coopgo.io/coopgo-platform/mobility-accounts/storage"
	"github.com/google/uuid"
	"github.com/gorilla/mux"
	"google.golang.org/protobuf/types/known/structpb"
)

func (h *ApplicationHandler) Administration(w http.ResponseWriter, r *http.Request) {
	var (
		wg                                                                                               sync.WaitGroup
		accounts, beneficiaries                                                                          []mobilityaccountsstorage.Account
		bookings                                                                                         []fleetsstorage.Booking
		accountsErr, beneficiariesErr, bookingsErr, groupsResponseErr, eventsResponseErr, groupsBatchErr error
		groups                                                                                           = []groupstorage.Group{}
		responses                                                                                        = []agendastorage.Event{}
		groupsResponse                                                                                   *groupsmanagement.GetGroupsResponse
		eventsResponse                                                                                   *agenda.GetEventsResponse
		groupids                                                                                         = []string{}
		groupsBatchResponse                                                                              *groupsmanagement.GetGroupsBatchResponse
	)
	// Retrieve accounts in a goroutine
	wg.Add(1)
	go func() {
		defer wg.Done()
		accounts, accountsErr = h.services.GetAccounts()
	}()
	// Retrieve beneficiaries in a goroutine
	wg.Add(1)
	go func() {
		defer wg.Done()
		beneficiaries, beneficiariesErr = h.services.GetBeneficiaries()
	}()
	// Retrieve bookings in a goroutine
	wg.Add(1)
	go func() {
		defer wg.Done()
		bookings, bookingsErr = h.services.GetBookings()
	}()
	// Retrieve groupsRequest in a goroutine
	wg.Add(1)
	go func() {
		defer wg.Done()
		request := &groupsmanagement.GetGroupsRequest{
			Namespaces: []string{"parcoursmob_organizations"},
		}
		groupsResponse, groupsResponseErr = h.services.GRPC.GroupsManagement.GetGroups(context.TODO(), request)
		for _, group := range groupsResponse.Groups {
			g := group.ToStorageType()
			groups = append(groups, g)
		}
		sort.Sort(sorting.GroupsByName(groups))
	}()
	// Retrieve Events in a goroutine
	wg.Add(1)
	go func() {
		defer wg.Done()
		eventsResponse, eventsResponseErr = h.services.GRPC.Agenda.GetEvents(context.TODO(), &agenda.GetEventsRequest{
			Namespaces: []string{"parcoursmob_dispositifs"},
		})
		for _, e := range eventsResponse.Events {
			groupids = append(groupids, e.Owners...)
			responses = append(responses, e.ToStorageType())
		}
		sort.Sort(sorting.EventsByStartdate(responses))
	}()
	wg.Add(1)
	// Retrieve groupsBatch in a goroutine
	go func() {
		defer wg.Done()
		groupsBatchResponse, groupsBatchErr = h.services.GRPC.GroupsManagement.GetGroupsBatch(context.TODO(), &groupsmanagement.GetGroupsBatchRequest{
			Groupids: groupids,
		})
		groupps := map[string]any{}
		if groupsBatchErr == nil {
			for _, g := range groupsBatchResponse.Groups {
				groupps[g.Id] = g.ToStorageType()
			}
		}
	}()
	wg.Wait()
	if accountsErr != nil || beneficiariesErr != nil || bookingsErr != nil || groupsResponseErr != nil || eventsResponseErr != nil {
		fmt.Println(accountsErr, beneficiariesErr, bookingsErr, groupsResponseErr, eventsResponseErr, groupsBatchErr)
		w.WriteHeader(http.StatusInternalServerError)
		return
	}
	h.Renderer.Administration(w, r, accounts, beneficiaries, groups, bookings, responses)
}

func (h *ApplicationHandler) AdministrationCreateGroup(w http.ResponseWriter, r *http.Request) {
	if r.Method == "POST" {
		r.ParseForm()

		if r.FormValue("name") == "" {

			fmt.Println("invalid name")
			w.WriteHeader(http.StatusBadRequest)
			return
		}

		modules := map[string]any{
			"beneficiaries":       r.FormValue("modules.beneficiaries") == "on",
			"journeys":            r.FormValue("modules.journeys") == "on",
			"vehicles":            r.FormValue("modules.vehicles") == "on",
			"vehicles_management": r.FormValue("modules.vehicles_management") == "on",
			"events":              r.FormValue("modules.events") == "on",
			"agenda":              r.FormValue("modules.agenda") == "on",
			"groups":              r.FormValue("modules.groups") == "on",
			"administration":      r.FormValue("modules.administration") == "on",
			"support":             r.FormValue("modules.support") == "on",
			"group_module":        r.FormValue("modules.group_module") == "on",
		}

		groupid := uuid.NewString()

		dataMap := map[string]any{
			"name":    r.FormValue("name"),
			"modules": modules,
		}

		data, err := structpb.NewValue(dataMap)
		if err != nil {
			fmt.Println(err)
			w.WriteHeader(http.StatusInternalServerError)
			return
		}

		request_organization := &groupsmanagement.AddGroupRequest{
			Group: &groupsmanagement.Group{
				Id:        groupid,
				Namespace: "parcoursmob_organizations",
				Data:      data.GetStructValue(),
			},
		}

		request_role := &groupsmanagement.AddGroupRequest{
			Group: &groupsmanagement.Group{
				Id:        groupid + ":admin",
				Namespace: "parcoursmob_roles",
			},
		}
		go func() {
			_, err = h.services.GRPC.GroupsManagement.AddGroup(context.TODO(), request_organization)
			if err != nil {
				fmt.Println(err)
				w.WriteHeader(http.StatusInternalServerError)
				return
			}
		}()
		// Create the admin role for the organization
		go func() {
			_, err = h.services.GRPC.GroupsManagement.AddGroup(context.TODO(), request_role)
			if err != nil {
				fmt.Println(err)
				w.WriteHeader(http.StatusInternalServerError)
				return
			}
		}()
		http.Redirect(w, r, fmt.Sprintf("/app/administration/groups/%s", groupid), http.StatusFound)
		return
	}
	h.Renderer.AdministrationCreateGroup(w, r)
}

func (h *ApplicationHandler) AdministrationGroupDisplay(w http.ResponseWriter, r *http.Request) {
	vars := mux.Vars(r)
	groupid := vars["groupid"]

	request := &groupsmanagement.GetGroupRequest{
		Id: groupid,
	}

	resp, err := h.services.GRPC.GroupsManagement.GetGroup(context.TODO(), request)
	if err != nil {
		fmt.Println(err)
		w.WriteHeader(http.StatusInternalServerError)
		return
	}

	groupmembers, admins, err := h.groupmembers(groupid)
	if err != nil {
		fmt.Println(err)
		w.WriteHeader(http.StatusInternalServerError)
		return
	}

	h.Renderer.AdministrationGroupDisplay(w, r, resp.Group.ToStorageType(), groupmembers, admins)
}

func (h *ApplicationHandler) AdministrationGroupInviteAdmin(w http.ResponseWriter, r *http.Request) {
	vars := mux.Vars(r)
	groupid := vars["groupid"]
	var (
		groupresp   *groupsmanagement.GetGroupResponse
		accountresp *accounts.GetAccountUsernameResponse
		err         error
	)
	go func() {
		groupresp, err = h.services.GRPC.GroupsManagement.GetGroup(context.TODO(), &groupsmanagement.GetGroupRequest{
			Id:        groupid,
			Namespace: "parcoursmob_organizations",
		})

		if err != nil {
			fmt.Println(err)
			w.WriteHeader(http.StatusInternalServerError)
			return
		}
	}()
	r.ParseForm()

	go func() {
		accountresp, err = h.services.GRPC.MobilityAccounts.GetAccountUsername(context.TODO(), &accounts.GetAccountUsernameRequest{
			Username:  r.FormValue("username"),
			Namespace: "parcoursmob",
		})
		if err == nil {
			// Account already exists : adding the existing account to admin list
			account := accountresp.Account.ToStorageType()
			account.Data["groups"] = append(account.Data["groups"].([]any), groupid, groupid)
			account.Data["groups"] = append(account.Data["groups"].([]any), groupid, groupid+":admin")

			as, _ := accounts.AccountFromStorageType(&account)

			_, err = h.services.GRPC.MobilityAccounts.UpdateData(
				context.TODO(),
				&accounts.UpdateDataRequest{
					Account: as,
				},
			)

			fmt.Println(err)

			data := map[string]any{
				"group": groupresp.Group.ToStorageType().Data["name"],
			}

			if err := h.emailing.Send("onboarding.existing_administrator", r.FormValue("username"), data); err != nil {
				fmt.Println(err)
			}

			http.Redirect(w, r, fmt.Sprintf("/app/administration/groups/%s", groupid), http.StatusFound)
			return
		} else {
			// Onboard now administrator
			onboarding := map[string]any{
				"username": r.FormValue("username"),
				"group":    groupid,
				"admin":    true,
			}

			b := make([]byte, 16)
			if _, err := io.ReadFull(rand.Reader, b); err != nil {
				fmt.Println(err)
				w.WriteHeader(http.StatusInternalServerError)
				return
			}
			key := base64.RawURLEncoding.EncodeToString(b)

			h.cache.PutWithTTL("onboarding/"+key, onboarding, 168*time.Hour) // 1 week TTL

			data := map[string]any{
				"group": groupresp.Group.ToStorageType().Data["name"],
				"key":   key,
			}

			if err := h.emailing.Send("onboarding.new_administrator", r.FormValue("username"), data); err != nil {
				fmt.Println(err)
				w.WriteHeader(http.StatusInternalServerError)
				return
			}
		}

		http.Redirect(w, r, fmt.Sprintf("/app/administration/groups/%s", groupid), http.StatusFound)
		return
	}()
}
func (h *ApplicationHandler) AdministrationGroupInviteMember(w http.ResponseWriter, r *http.Request) {
	vars := mux.Vars(r)
	groupid := vars["groupid"]
	var (
		group storage.Group
	)
	groupCh := make(chan storage.Group)
	go func() {
		groupresp, err := h.services.GRPC.GroupsManagement.GetGroup(context.TODO(), &groupsmanagement.GetGroupRequest{
			Id:        groupid,
			Namespace: "parcoursmob_organizations",
		})

		if err != nil {
			fmt.Println(err)
			w.WriteHeader(http.StatusInternalServerError)
			return
		}
		group := groupresp.Group.ToStorageType()
		groupCh <- group
	}()
	r.ParseForm()
	go func() {
		group = <-groupCh
		accountresp, err := h.services.GRPC.MobilityAccounts.GetAccountUsername(context.TODO(), &accounts.GetAccountUsernameRequest{
			Username:  r.FormValue("username"),
			Namespace: "parcoursmob",
		})
		if err == nil {
			account := accountresp.Account.ToStorageType()
			account.Data["groups"] = append(account.Data["groups"].([]any), group.ID)

			as, _ := accounts.AccountFromStorageType(&account)

			_, err = h.services.GRPC.MobilityAccounts.UpdateData(
				context.TODO(),
				&accounts.UpdateDataRequest{
					Account: as,
				},
			)
			data := map[string]any{
				"group": group.Data["name"],
			}

			if err := h.emailing.Send("onboarding.existing_member", r.FormValue("username"), data); err != nil {
			}

			http.Redirect(w, r, "/app/group/settings", http.StatusFound)
			return
		} else {
			// Onboard now administrator
			onboarding := map[string]any{
				"username": r.FormValue("username"),
				"group":    group.ID,
				"admin":    false,
			}

			b := make([]byte, 16)
			if _, err := io.ReadFull(rand.Reader, b); err != nil {
				w.WriteHeader(http.StatusInternalServerError)
				return
			}
			key := base64.RawURLEncoding.EncodeToString(b)

			h.cache.PutWithTTL("onboarding/"+key, onboarding, 168*time.Hour) // 1 week TTL
			data := map[string]any{
				"group": group.Data["name"],
				"key":   key,
			}

			if err := h.emailing.Send("onboarding.new_member", r.FormValue("username"), data); err != nil {
				w.WriteHeader(http.StatusInternalServerError)
				return
			}
		}
	}()
	http.Redirect(w, r, "/app/administration/groups/"+group.ID, http.StatusFound)
	return
}

func filteVehicle(r *http.Request, v *fleets.Vehicle) bool {
	g := r.Context().Value(identification.GroupKey)
	if g == nil {
		return false
	}

	group := g.(storage.Group)

	for _, n := range v.Administrators {
		if n == group.ID {
			return true
		}
	}

	return false
}

func (h ApplicationHandler) AdminStatVehicles(w http.ResponseWriter, r *http.Request) {

	bookings := []fleetsstorage.Booking{}
	administrators := []string{}
	reequest := &fleets.GetVehiclesRequest{
		Namespaces: []string{"parcoursmob"},
	}
	reesp, err := h.services.GRPC.Fleets.GetVehicles(context.TODO(), reequest)
	if err != nil {
		fmt.Println(err)
		w.WriteHeader(http.StatusInternalServerError)
	}

	vehicles := []fleetsstorage.Vehicle{}
	for _, vehiicle := range reesp.Vehicles {

		v := vehiicle.ToStorageType()
		adminfound := false
		for _, a := range administrators {
			if a == v.Administrators[0] {
				adminfound = true
				break
			}
		}
		if !adminfound {
			administrators = append(administrators, v.Administrators[0])
		}

		vehicleBookings := []fleetsstorage.Booking{}
		for _, b := range v.Bookings {
			if b.Unavailableto.After(time.Now()) {
				vehicleBookings = append(vehicleBookings, b)
			}
		}

		v.Bookings = vehicleBookings

		vehicles = append(vehicles, v)

	}
	groups := map[string]any{}

	if len(administrators) > 0 {
		admingroups, err := h.services.GRPC.GroupsManagement.GetGroupsBatch(context.TODO(), &groupsmanagement.GetGroupsBatchRequest{
			Groupids: administrators,
		})
		if err != nil {
			fmt.Println(err)
			w.WriteHeader(http.StatusInternalServerError)
			return
		}

		for _, g := range admingroups.Groups {
			groups[g.Id] = g.ToStorageType()
		}

	}
	sort.Sort(sorting.VehiclesByLicencePlate(vehicles))
	sort.Sort(sorting.BookingsByStartdate(bookings))
	h.Renderer.AdminStatVehicles(w, r, vehicles, bookings, groups)
}

func (h ApplicationHandler) AdminStatBookings(w http.ResponseWriter, r *http.Request) {

	vehicles := map[string]fleetsstorage.Vehicle{}
	bookings := []fleetsstorage.Booking{}

	reequest := &fleets.GetVehiclesRequest{
		Namespaces: []string{"parcoursmob"},
	}
	reesp, err := h.services.GRPC.Fleets.GetVehicles(context.TODO(), reequest)
	if err != nil {
		fmt.Println(err)
		w.WriteHeader(http.StatusInternalServerError)
		return
	}

	beneficiaries_ids := []string{}

	for _, vehicle := range reesp.Vehicles {

		v := vehicle.ToStorageType()

		for _, b := range v.Bookings {
			bookings = append(bookings, b)
			beneficiaries_ids = append(beneficiaries_ids, b.Driver)
		}

		vehicles[v.ID] = v

	}

	groups := map[string]any{}

	admingroups, err := h.services.GRPC.GroupsManagement.GetGroups(context.TODO(), &groupsmanagement.GetGroupsRequest{
		Namespaces: []string{"parcoursmob_organizations"},
	})
	if err != nil {
		fmt.Println(err)
		w.WriteHeader(http.StatusInternalServerError)
		return
	}

	for _, g := range admingroups.Groups {
		groups[g.Id] = g.ToStorageType()
	}

	beneficiaries, err := h.services.GRPC.MobilityAccounts.GetAccountsBatch(context.TODO(), &accounts.GetAccountsBatchRequest{
		Accountids: beneficiaries_ids,
	})
	if err != nil {
		fmt.Println(err)
		w.WriteHeader(http.StatusInternalServerError)
		return
	}

	beneficiaries_map := map[string]any{}
	for _, ben := range beneficiaries.Accounts {
		beneficiaries_map[ben.Id] = ben.ToStorageType()
	}

	sort.Sort(sorting.BookingsByStartdate(bookings))
	h.Renderer.AdminStatBookings(w, r, vehicles, bookings, groups, beneficiaries_map)
}

func (h *ApplicationHandler) members() ([]*accounts.Account, error) {
	resp, err := h.services.GRPC.MobilityAccounts.GetAccounts(context.TODO(), &accounts.GetAccountsRequest{
		Namespaces: []string{"parcoursmob"},
	})
	if err != nil {
		return nil, err
	}

	return resp.Accounts, nil
}

func (h *ApplicationHandler) groupmembers(groupid string) (groupmembers []mobilityaccountsstorage.Account, admins []mobilityaccountsstorage.Account, err error) {
	members, err := h.members()
	if err != nil {
		if err != nil {
			fmt.Println(err)
			return nil, nil, err
		}
	}

	groupmembers = []mobilityaccountsstorage.Account{}
	admins = []mobilityaccountsstorage.Account{}

	for _, m := range members {
		mm := m.ToStorageType()
		for _, g := range mm.Data["groups"].([]any) {
			if g.(string) == groupid {
				groupmembers = append(groupmembers, mm)
			}
			if g.(string) == groupid+":admin" {
				admins = append(admins, mm)
			}
		}
	}

	return groupmembers, admins, err
}

func (h ApplicationHandler) AdminStatBeneficaires(w http.ResponseWriter, r *http.Request) {
	beneficiaries, err := h.services.GetBeneficiaries()
	if err != nil {
		fmt.Println(err)
		w.WriteHeader(http.StatusInternalServerError)
		return
	}
	cacheid := uuid.NewString()
	h.cache.PutWithTTL(cacheid, beneficiaries, 1*time.Hour)
	h.Renderer.AdminStatBeneficaires(w, r, beneficiaries, cacheid)
}

func (h ApplicationHandler) AdminStatEvents(w http.ResponseWriter, r *http.Request) {
	resp, err := h.services.GRPC.Agenda.GetEvents(context.TODO(), &agenda.GetEventsRequest{
		Namespaces: []string{"parcoursmob_dispositifs"},
	})

	if err != nil {
		fmt.Println(err)
		w.WriteHeader(http.StatusInternalServerError)
		return
	}

	responses := []agendastorage.Event{}

	groupids := []string{}
	for _, e := range resp.Events {
		groupids = append(groupids, e.Owners...)
		responses = append(responses, e.ToStorageType())
	}

	sort.Sort(sorting.EventsByStartdate(responses))

	groupsresp, err := h.services.GRPC.GroupsManagement.GetGroupsBatch(context.TODO(), &groupsmanagement.GetGroupsBatchRequest{
		Groupids: groupids,
	})
	groups := map[string]any{}

	if err == nil {
		for _, g := range groupsresp.Groups {
			groups[g.Id] = g.ToStorageType()
		}
	}
	h.Renderer.AdminStatEvents(w, r, responses, groups)
}
