package app

import (
	"github.com/gofiber/fiber/v2"
	"lms.de/backend/modules/database"
	"lms.de/backend/modules/structs"
)

func GetApp(c *fiber.Ctx) error {
	// swagger:operation GET /app app getApp
	// ---
	// summary: Get app
	// consumes:
	// - application/json
	// produces:
	// - application/json
	// responses:
	//   '200':
	//     description: App fetched successfully
	//     schema:
	//       "$ref": "#/definitions/GetAppResponse"
	//   '400':
	//     description: Invalid request body
	//   '500':
	//     description: Failed to fetch app

	var user structs.User

	database.DB.Model(&structs.User{
		Id: c.Locals("userId").(string),
	}).Select("profile_picture_url").First(&user)

	var organization structs.Organization

	database.DB.Model(&structs.Organization{
		Id: c.Locals("organizationId").(string),
	}).Select("company_name", "primary_color", "logo_url", "banner_url").First(&organization)

	return c.JSON(structs.GetAppResponse{
		User: structs.AppUser{
			ProfilePictureUrl: user.ProfilePictureUrl,
		},
		Organization: structs.AppOrganization{
			CompanyName:  organization.CompanyName,
			PrimaryColor: organization.PrimaryColor,
			LogoUrl:      organization.LogoUrl,
			BannerUrl:    organization.BannerUrl,
		},
	})
}
