package handlers import ( "fmt" "net/http" "strconv" "time" "go-crm/internal/db" "go-crm/internal/templates" "github.com/go-chi/chi/v5" ) func (a *App) ListQuestions(w http.ResponseWriter, r *http.Request) { accountID, ok := a.requireAuth(w, r) if !ok { return } limit, _ := strconv.Atoi(r.URL.Query().Get("limit")) offset, _ := strconv.Atoi(r.URL.Query().Get("offset")) clientID, _ := strconv.ParseInt(r.URL.Query().Get("client_id"), 10, 64) if limit == 0 { limit = 20 } questions, err := db.ListQuestions(a.DB, clientID, limit, offset) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return } clients, err := db.ListClients(a.DB, accountID, limit, offset) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return } customers, err := db.ListCustomers(a.DB, accountID, clientID, limit, offset) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return } var clientOptions, customerOptions string clientNames := make(map[int64]string) customerNames := make(map[int64]string) for _, c := range clients { clientOptions += fmt.Sprintf(``, c.ClientID, htmlEscape(c.Name)) clientNames[c.ClientID] = c.Name } for _, cu := range customers { customerOptions += fmt.Sprintf(``, cu.CustomerID, htmlEscape(cu.Name)) customerNames[cu.CustomerID] = cu.Name } buf := templates.BufRender() actions := `` fmt.Fprint(buf, templates.PageHeader("Questions", "Customer inquiries and Q&A", actions)) fmt.Fprintf(buf, ` `, clientOptions, customerOptions) if len(questions) == 0 { fmt.Fprint(buf, templates.EmptyState(``, "No questions recorded yet.")) } else { fmt.Fprintf(buf, `
`) fmt.Fprint(buf, templates.TableStart([]string{"Client", "Customer", "Question", "Status", "Actions"})) for _, q := range questions { clientName := clientNames[q.ClientID] if clientName == "" { clientName = strconv.FormatInt(q.ClientID, 10) } customerName := customerNames[q.CustomerID] if customerName == "" { customerName = strconv.FormatInt(q.CustomerID, 10) } statusPill := `pending` if q.Status == "answered" { statusPill = `answered` } fmt.Fprintf(buf, ` %s %s %s %s
View
`, htmlEscape(clientName), htmlEscape(customerName), htmlEscape(q.Question), statusPill, q.QuestionID, q.QuestionID, q.QuestionID) } fmt.Fprint(buf, templates.TableEnd()) fmt.Fprintf(buf, `
`) for _, q := range questions { clientName := clientNames[q.ClientID] customerName := customerNames[q.CustomerID] fmt.Fprintf(buf, ` `, q.QuestionID, q.QuestionID, q.QuestionID, q.ClientID, htmlEscape(clientName), clientOptions, q.CustomerID, htmlEscape(customerName), customerOptions, htmlEscape(q.Question), q.Status, q.Status, q.QuestionID) } } w.Header().Set("Content-Type", "text/html; charset=utf-8") templates.WritePage(w, buf, "Questions", "questions") } func (a *App) CreateQuestion(w http.ResponseWriter, r *http.Request) { _, ok := a.requireAuth(w, r) if !ok { return } r.ParseForm() clientID, _ := strconv.ParseInt(r.FormValue("client_id"), 10, 64) customerID, _ := strconv.ParseInt(r.FormValue("customer_id"), 10, 64) question := db.Question{ ClientID: clientID, CustomerID: customerID, Question: r.FormValue("question"), Timestamp: time.Now().Unix(), Status: r.FormValue("status"), CreatedAt: time.Now().Unix(), } if err := question.Create(a.DB); err != nil { http.Error(w, err.Error(), http.StatusBadRequest) return } w.Header().Set("HX-Refresh", "true") } func (a *App) ViewQuestion(w http.ResponseWriter, r *http.Request) { _, ok := a.requireAuth(w, r) if !ok { return } id, _ := strconv.ParseInt(chi.URLParam(r, "id"), 10, 64) var q db.Question err := a.DB.QueryRow("SELECT question_id, client_id, customer_id, question, timestamp, status, created_at FROM questions WHERE question_id = ?", id).Scan(&q.QuestionID, &q.ClientID, &q.CustomerID, &q.Question, &q.Timestamp, &q.Status, &q.CreatedAt) if err != nil { http.Error(w, "Question not found", http.StatusNotFound) return } buf := templates.BufRender() fmt.Fprint(buf, templates.PageHeader("Question", "Inquiry details")) statusPill := `pending` if q.Status == "answered" { statusPill = `answered` } fmt.Fprintf(buf, `
%s
%s
Client ID: %d
Customer ID: %d
`, statusPill, htmlEscape(q.Question), q.ClientID, q.CustomerID) w.Header().Set("Content-Type", "text/html; charset=utf-8") templates.WritePage(w, buf, "Question", "questions") } func (a *App) UpdateQuestion(w http.ResponseWriter, r *http.Request) { _, ok := a.requireAuth(w, r) if !ok { return } id, _ := strconv.ParseInt(chi.URLParam(r, "id"), 10, 64) r.ParseForm() clientID, _ := strconv.ParseInt(r.FormValue("client_id"), 10, 64) customerID, _ := strconv.ParseInt(r.FormValue("customer_id"), 10, 64) _, err := a.DB.Exec("UPDATE questions SET client_id=?, customer_id=?, question=?, status=? WHERE question_id=?", clientID, customerID, r.FormValue("question"), r.FormValue("status"), id) if err != nil { http.Error(w, err.Error(), http.StatusBadRequest) return } w.Header().Set("HX-Refresh", "true") } func (a *App) DeleteQuestion(w http.ResponseWriter, r *http.Request) { _, ok := a.requireAuth(w, r) if !ok { return } id, _ := strconv.ParseInt(chi.URLParam(r, "id"), 10, 64) a.DB.Exec("DELETE FROM questions WHERE question_id = ?", id) } // --- Answers (in same file as original) --- func (a *App) ListAnswers(w http.ResponseWriter, r *http.Request) { accountID, ok := a.requireAuth(w, r) if !ok { return } limit, _ := strconv.Atoi(r.URL.Query().Get("limit")) offset, _ := strconv.Atoi(r.URL.Query().Get("offset")) questionID, _ := strconv.ParseInt(r.URL.Query().Get("question_id"), 10, 64) if limit == 0 { limit = 20 } answers, err := db.ListAnswersWithDetails(a.DB, accountID, questionID, limit, offset) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return } questions, err := db.ListQuestions(a.DB, 0, limit, offset) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return } var questionOptions string questionText := make(map[int64]string) for _, q := range questions { questionOptions += fmt.Sprintf(``, q.QuestionID, htmlEscape(q.Question)) questionText[q.QuestionID] = q.Question } buf := templates.BufRender() actions := `` fmt.Fprint(buf, templates.PageHeader("Answers", "Responses to customer questions", actions)) fmt.Fprintf(buf, ` `, questionOptions) if len(answers) == 0 { fmt.Fprint(buf, templates.EmptyState(``, "No answers recorded yet.")) } else { fmt.Fprintf(buf, `
`) fmt.Fprint(buf, templates.TableStart([]string{"Client", "Customer", "Question", "Answer", "Actions"})) for _, a2 := range answers { qText := a2.QuestionText if qText == "" { qText = questionText[a2.QuestionID] if qText == "" { qText = strconv.FormatInt(a2.QuestionID, 10) } } clientName := a2.ClientName if clientName == "" { clientName = strconv.FormatInt(a2.ClientID, 10) } customerName := a2.CustomerName if customerName == "" { customerName = "Unknown" } fmt.Fprintf(buf, ` %s %s %s %s
View
`, htmlEscape(clientName), htmlEscape(customerName), htmlEscape(qText), htmlEscape(a2.Answer), a2.AnswerID, a2.AnswerID, a2.AnswerID) } fmt.Fprint(buf, templates.TableEnd()) fmt.Fprintf(buf, `
`) for _, a2 := range answers { qText := a2.QuestionText if qText == "" { qText = questionText[a2.QuestionID] if qText == "" { qText = strconv.FormatInt(a2.QuestionID, 10) } } fmt.Fprintf(buf, ` `, a2.AnswerID, a2.AnswerID, a2.AnswerID, a2.QuestionID, htmlEscape(qText), questionOptions, htmlEscape(a2.Answer), a2.AnswerID) } } w.Header().Set("Content-Type", "text/html; charset=utf-8") templates.WritePage(w, buf, "Answers", "answers") } func (a *App) CreateAnswer(w http.ResponseWriter, r *http.Request) { accountID, ok := a.requireAuth(w, r) if !ok { return } r.ParseForm() questionID, _ := strconv.ParseInt(r.FormValue("question_id"), 10, 64) var clientID int64 err := a.DB.QueryRow("SELECT client_id FROM questions WHERE question_id = ?", questionID).Scan(&clientID) if err != nil { http.Error(w, "Question not found", http.StatusBadRequest) return } var checkID int64 err = a.DB.QueryRow("SELECT client_id FROM clients WHERE client_id = ? AND account_id = ?", clientID, accountID).Scan(&checkID) if err != nil { http.Error(w, "Invalid question", http.StatusBadRequest) return } answer := db.Answer{ ClientID: clientID, QuestionID: questionID, Answer: r.FormValue("answer"), Timestamp: time.Now().Unix(), Status: "active", CreatedAt: time.Now().Unix(), } if err := answer.Create(a.DB); err != nil { http.Error(w, err.Error(), http.StatusBadRequest) return } w.Header().Set("HX-Refresh", "true") } func (a *App) ViewAnswer(w http.ResponseWriter, r *http.Request) { _, ok := a.requireAuth(w, r) if !ok { return } id, _ := strconv.ParseInt(chi.URLParam(r, "id"), 10, 64) var ans db.Answer err := a.DB.QueryRow("SELECT answer_id, client_id, question_id, answer, timestamp, status, created_at FROM answers WHERE answer_id = ?", id).Scan(&ans.AnswerID, &ans.ClientID, &ans.QuestionID, &ans.Answer, &ans.Timestamp, &ans.Status, &ans.CreatedAt) if err != nil { http.Error(w, "Answer not found", http.StatusNotFound) return } buf := templates.BufRender() fmt.Fprint(buf, templates.PageHeader("Answer", "Response details")) fmt.Fprintf(buf, `
%s
Question ID: %d
Client ID: %d
`, htmlEscape(ans.Answer), ans.QuestionID, ans.ClientID) w.Header().Set("Content-Type", "text/html; charset=utf-8") templates.WritePage(w, buf, "Answer", "answers") } func (a *App) UpdateAnswer(w http.ResponseWriter, r *http.Request) { _, ok := a.requireAuth(w, r) if !ok { return } id, _ := strconv.ParseInt(chi.URLParam(r, "id"), 10, 64) r.ParseForm() questionID, _ := strconv.ParseInt(r.FormValue("question_id"), 10, 64) _, err := a.DB.Exec("UPDATE answers SET question_id=?, answer=? WHERE answer_id=?", questionID, r.FormValue("answer"), id) if err != nil { http.Error(w, err.Error(), http.StatusBadRequest) return } w.Header().Set("HX-Refresh", "true") } func (a *App) DeleteAnswer(w http.ResponseWriter, r *http.Request) { _, ok := a.requireAuth(w, r) if !ok { return } id, _ := strconv.ParseInt(chi.URLParam(r, "id"), 10, 64) a.DB.Exec("DELETE FROM answers WHERE answer_id = ?", id) } // --- package-level shims --- func ListQuestions(w http.ResponseWriter, r *http.Request) { (&App{DB: DB, WAConnector: WAConnector}).ListQuestions(w, r) } func CreateQuestion(w http.ResponseWriter, r *http.Request) { (&App{DB: DB, WAConnector: WAConnector}).CreateQuestion(w, r) } func ViewQuestion(w http.ResponseWriter, r *http.Request) { (&App{DB: DB, WAConnector: WAConnector}).ViewQuestion(w, r) } func UpdateQuestion(w http.ResponseWriter, r *http.Request) { (&App{DB: DB, WAConnector: WAConnector}).UpdateQuestion(w, r) } func DeleteQuestion(w http.ResponseWriter, r *http.Request) { (&App{DB: DB, WAConnector: WAConnector}).DeleteQuestion(w, r) } func ListAnswers(w http.ResponseWriter, r *http.Request) { (&App{DB: DB, WAConnector: WAConnector}).ListAnswers(w, r) } func CreateAnswer(w http.ResponseWriter, r *http.Request) { (&App{DB: DB, WAConnector: WAConnector}).CreateAnswer(w, r) } func ViewAnswer(w http.ResponseWriter, r *http.Request) { (&App{DB: DB, WAConnector: WAConnector}).ViewAnswer(w, r) } func UpdateAnswer(w http.ResponseWriter, r *http.Request) { (&App{DB: DB, WAConnector: WAConnector}).UpdateAnswer(w, r) } func DeleteAnswer(w http.ResponseWriter, r *http.Request) { (&App{DB: DB, WAConnector: WAConnector}).DeleteAnswer(w, r) }