package handlers import ( "encoding/json" "net/http" "aggios-app/backend/internal/repository" "github.com/google/uuid" ) type AgencyHandler struct { tenantRepo *repository.TenantRepository } func NewAgencyHandler(tenantRepo *repository.TenantRepository) *AgencyHandler { return &AgencyHandler{ tenantRepo: tenantRepo, } } type AgencyProfileResponse struct { ID string `json:"id"` Name string `json:"name"` CNPJ string `json:"cnpj"` Email string `json:"email"` Phone string `json:"phone"` Website string `json:"website"` Address string `json:"address"` City string `json:"city"` State string `json:"state"` Zip string `json:"zip"` RazaoSocial string `json:"razao_social"` Description string `json:"description"` Industry string `json:"industry"` } type UpdateAgencyProfileRequest struct { Name string `json:"name"` CNPJ string `json:"cnpj"` Email string `json:"email"` Phone string `json:"phone"` Website string `json:"website"` Address string `json:"address"` City string `json:"city"` State string `json:"state"` Zip string `json:"zip"` RazaoSocial string `json:"razao_social"` Description string `json:"description"` Industry string `json:"industry"` } // GetProfile returns the current agency profile func (h *AgencyHandler) GetProfile(w http.ResponseWriter, r *http.Request) { if r.Method != http.MethodGet { http.Error(w, "Method not allowed", http.StatusMethodNotAllowed) return } // Get tenant from context (set by middleware) tenantID := r.Context().Value("tenantID") if tenantID == nil { http.Error(w, "Tenant not found", http.StatusUnauthorized) return } // Parse tenant ID tid, err := uuid.Parse(tenantID.(string)) if err != nil { http.Error(w, "Invalid tenant ID", http.StatusBadRequest) return } // Get tenant from database tenant, err := h.tenantRepo.FindByID(tid) if err != nil { http.Error(w, "Error fetching profile", http.StatusInternalServerError) return } if tenant == nil { http.Error(w, "Tenant not found", http.StatusNotFound) return } response := AgencyProfileResponse{ ID: tenant.ID.String(), Name: tenant.Name, CNPJ: tenant.CNPJ, Email: tenant.Email, Phone: tenant.Phone, Website: tenant.Website, Address: tenant.Address, City: tenant.City, State: tenant.State, Zip: tenant.Zip, RazaoSocial: tenant.RazaoSocial, Description: tenant.Description, Industry: tenant.Industry, } w.Header().Set("Content-Type", "application/json") json.NewEncoder(w).Encode(response) } // UpdateProfile updates the current agency profile func (h *AgencyHandler) UpdateProfile(w http.ResponseWriter, r *http.Request) { if r.Method != http.MethodPut && r.Method != http.MethodPatch { http.Error(w, "Method not allowed", http.StatusMethodNotAllowed) return } // Get tenant from context tenantID := r.Context().Value("tenantID") if tenantID == nil { http.Error(w, "Tenant not found", http.StatusUnauthorized) return } var req UpdateAgencyProfileRequest if err := json.NewDecoder(r.Body).Decode(&req); err != nil { http.Error(w, "Invalid request body", http.StatusBadRequest) return } // Parse tenant ID tid, err := uuid.Parse(tenantID.(string)) if err != nil { http.Error(w, "Invalid tenant ID", http.StatusBadRequest) return } // Prepare updates updates := map[string]interface{}{ "name": req.Name, "cnpj": req.CNPJ, "razao_social": req.RazaoSocial, "email": req.Email, "phone": req.Phone, "website": req.Website, "address": req.Address, "city": req.City, "state": req.State, "zip": req.Zip, "description": req.Description, "industry": req.Industry, } // Update in database if err := h.tenantRepo.UpdateProfile(tid, updates); err != nil { http.Error(w, "Error updating profile", http.StatusInternalServerError) return } // Fetch updated data tenant, err := h.tenantRepo.FindByID(tid) if err != nil { http.Error(w, "Error fetching updated profile", http.StatusInternalServerError) return } response := AgencyProfileResponse{ ID: tenant.ID.String(), Name: tenant.Name, CNPJ: tenant.CNPJ, Email: tenant.Email, Phone: tenant.Phone, Website: tenant.Website, Address: tenant.Address, City: tenant.City, State: tenant.State, Zip: tenant.Zip, RazaoSocial: tenant.RazaoSocial, Description: tenant.Description, Industry: tenant.Industry, } w.Header().Set("Content-Type", "application/json") json.NewEncoder(w).Encode(response) }