// @ts-nocheck
import { LauncherChipSelector, CheckIcon, FilterIcon, LogsIcon, PlayIcon, SaveIcon, appendUniqueString, formatRequestTimestamp, getRequestDisplayStateClassName, getRequestDisplayStateLabel, removeStringValue } from "../utils";

export function LauncherAdminPanel({
  adminAccessGranted,
  adminPasswordDraft,
  setAdminPasswordDraft,
  handleAdminUnlock,
  adminAuthSubmitting,
  adminPasswordError,
  handleProcessPendingQueue,
  queueTriggering,
  refreshAdminData,
  logsLoading,
  setLogsModalOpen,
  loadRecentSaveEvents,
  queuedPendingRequestCount,
  needsReviewRequestCount,
  pendingRequestCount,
  activeRequestCount,
  implementedRequestCount,
  adminNotice,
  logsError,
  adminSearchText,
  setAdminSearchText,
  adminFilterMenuOpen,
  setAdminFilterMenuOpen,
  requestStatusFilterOptions,
  adminStatusFilters,
  setAdminStatusFilters,
  requestTagFilterOptions,
  adminTagFilters,
  setAdminTagFilters,
  toggleStringSelection,
  requestsLoading,
  adminFilteredRequests,
  requestMutatingId,
  selectedAdminRequestId,
  handleSelectAdminRequest,
  handleDeleteRequest,
  adminEditorDraft,
  adminDetailTab,
  setAdminDetailTab,
  selectedAnalysisItem,
  selectedAdminAnalysisIndex,
  setSelectedAdminAnalysisIndex,
  updateAdminDraft,
  updateAdminDraftItem,
  standardizedTagOptions,
  categoryOptions,
  handleSaveAdminRequest,
  adminSaving,
  requeueingMode,
  handleApproveAdminRequest,
  handleRequeueAnalysis,
}) {
  return (
    <section className="launcher-request-admin-panel">
      {!adminAccessGranted ? (
        <div className="launcher-request-admin-unlock">
          <div className="launcher-request-admin-kicker">Protected Tools</div>
          <h3 className="launcher-request-admin-title">Admin Access Required</h3>
          <p className="launcher-request-admin-copy">
            Enter the admin password to manage deletions, run the queue worker, and read request logs.
          </p>
          <label className="launcher-request-admin-field">
            <span className="launcher-request-filter-label">Password</span>
            <input
              type="password"
              className="launcher-request-filter-select"
              value={adminPasswordDraft}
              onChange={(event) => setAdminPasswordDraft(event.target.value)}
              onKeyDown={(event) => {
                if (event.key === "Enter") {
                  event.preventDefault();
                  void handleAdminUnlock();
                }
              }}
              placeholder="Enter admin password"
            />
          </label>
          <div className="launcher-request-admin-actions">
            <button
              type="button"
              className="launcher-primary-btn"
              onClick={() => void handleAdminUnlock()}
              disabled={adminAuthSubmitting}
            >
              {adminAuthSubmitting ? "Unlocking..." : "Unlock Admin Panel"}
            </button>
          </div>
          {adminPasswordError ? <p className="launcher-request-error">{adminPasswordError}</p> : null}
        </div>
      ) : (
        <>
          <div className="launcher-request-admin-toolbar">
            <div className="launcher-request-admin-actions">
              <button
                type="button"
                className="launcher-primary-btn"
                onClick={() => void handleProcessPendingQueue()}
                disabled={queueTriggering}
              >
                {queueTriggering ? "Starting Queue..." : "Run Pending Queue"}
              </button>
              <button
                type="button"
                className="launcher-secondary-btn"
                onClick={() => void refreshAdminData({ includeLogs: true, silentRequests: true })}
                disabled={logsLoading}
              >
                {logsLoading ? "Refreshing..." : "Refresh Admin Data"}
              </button>
              <button
                type="button"
                className="launcher-secondary-btn"
                onClick={() => {
                  setLogsModalOpen(true);
                  void loadRecentSaveEvents();
                }}
                disabled={logsLoading}
              >
                <LogsIcon /> View Logs
              </button>
            </div>
            <div className="launcher-request-admin-stats">
              <span>{queuedPendingRequestCount} queued</span>
              <span>{needsReviewRequestCount} review</span>
              <span>{pendingRequestCount} pending</span>
              <span>{activeRequestCount} active</span>
              <span>{implementedRequestCount} implemented</span>
            </div>
          </div>
          {adminNotice ? <p className="launcher-request-admin-notice">{adminNotice}</p> : null}
          {adminPasswordError ? <p className="launcher-request-error">{adminPasswordError}</p> : null}
          {logsError ? <p className="launcher-request-error">{logsError}</p> : null}
          <div className="launcher-request-admin-grid">
            <div className="launcher-request-admin-sidebar">
              <section className="launcher-request-admin-card launcher-request-admin-list-card">
                <div className="launcher-request-admin-card-head">
                  <h4 className="launcher-request-admin-card-title">Request Management</h4>
                  <div className="launcher-request-admin-card-hint">Select a request to load it on the right.</div>
                </div>
                <div className="launcher-request-filter-bar launcher-request-filter-bar-admin">
                  <input
                    type="text"
                    className="launcher-request-search-input"
                    value={adminSearchText}
                    onChange={(event) => setAdminSearchText(event.target.value)}
                    placeholder="Search requests..."
                  />
                  <div className="launcher-request-filter-menu-wrap">
                    <button
                      type="button"
                      className={`launcher-secondary-btn launcher-request-filter-icon-btn ${adminFilterMenuOpen ? "is-active" : ""}`}
                      onClick={() => setAdminFilterMenuOpen((current) => !current)}
                      title="Open request filters"
                      aria-label="Open request filters"
                    >
                      <FilterIcon />
                    </button>
                    {adminFilterMenuOpen ? (
                      <div className="launcher-request-filter-menu launcher-request-filter-menu-admin">
                        {requestStatusFilterOptions.length > 0 ? (
                          <div className="launcher-request-filter-group">
                            <div className="launcher-request-filter-group-title">Status</div>
                            {requestStatusFilterOptions.map((option) => (
                              <label key={`admin-status-${option.id}`} className="launcher-request-filter-option">
                                <input
                                  type="checkbox"
                                  checked={adminStatusFilters.includes(option.id)}
                                  onChange={() => setAdminStatusFilters((current) => toggleStringSelection(current, option.id))}
                                />
                                <span>({option.count}) {option.label}</span>
                              </label>
                            ))}
                          </div>
                        ) : null}
                        {requestTagFilterOptions.length > 0 ? (
                          <div className="launcher-request-filter-group">
                            <div className="launcher-request-filter-group-title">Tags</div>
                            {requestTagFilterOptions.map(({ tag, count }) => (
                              <label key={`admin-tag-${tag}`} className="launcher-request-filter-option">
                                <input
                                  type="checkbox"
                                  checked={adminTagFilters.includes(tag)}
                                  onChange={() => setAdminTagFilters((current) => toggleStringSelection(current, tag))}
                                />
                                <span>({count}) {tag}</span>
                              </label>
                            ))}
                          </div>
                        ) : null}
                      </div>
                    ) : null}
                  </div>
                </div>
                <div className="launcher-request-admin-request-list">
                  {!requestsLoading && adminFilteredRequests.length === 0 ? (
                    <div className="launcher-request-empty">No requests match the current search or filters.</div>
                  ) : null}
                  {adminFilteredRequests.map((requestEntry) => {
                    const isMutating = requestMutatingId === requestEntry.id;
                    const isSelected = requestEntry.id === selectedAdminRequestId;
                    const requestDisplayState = getRequestDisplayStateLabel(requestEntry);
                    const requestDisplayStateClassName = getRequestDisplayStateClassName(requestEntry);
                    return (
                      <article
                        key={`admin-${requestEntry.id}`}
                        className={`launcher-request-admin-request-row ${isSelected ? "is-selected" : ""}`}
                        onClick={() => handleSelectAdminRequest(requestEntry.id)}
                      >
                        <div className="launcher-request-admin-request-copy">
                          <div className="launcher-request-admin-request-headline">
                            <div className="launcher-request-admin-request-title">{requestEntry.title}</div>
                            <div className={`launcher-request-status-pill is-${requestDisplayStateClassName}`}>
                              {requestDisplayState}
                            </div>
                          </div>
                          <div className="launcher-request-admin-request-meta">
                            <span>{formatRequestTimestamp(requestEntry.updatedAt)}</span>
                          </div>
                          {requestEntry.tags.length > 0 ? (
                            <div className="launcher-request-tags">
                              {requestEntry.tags.slice(0, 3).map((tag) => (
                                <span key={`${requestEntry.id}-${tag}`} className="launcher-request-tag">{tag}</span>
                              ))}
                            </div>
                          ) : null}
                        </div>
                        <button
                          type="button"
                          className="launcher-request-delete-btn"
                          onClick={(event) => {
                            event.stopPropagation();
                            void handleDeleteRequest(requestEntry);
                          }}
                          disabled={isMutating}
                          aria-label={`Delete ${requestEntry.title}`}
                          title="Delete request"
                        >
                          X
                        </button>
                      </article>
                    );
                  })}
                </div>
              </section>
            </div>
            <section className="launcher-request-admin-card launcher-request-admin-detail-card">
              {!adminEditorDraft ? (
                <div className="launcher-request-empty">Select a request from the list to review it.</div>
              ) : (
                <div className="launcher-request-admin-detail">
                  <div className="launcher-request-admin-detail-top">
                    <div className="launcher-request-admin-detail-copy">
                      <div className="launcher-request-admin-kicker">Selected Request</div>
                      <h4 className="launcher-request-admin-title">{adminEditorDraft.title}</h4>
                    </div>
                    <div className="launcher-request-admin-detail-tabs" role="tablist" aria-label="Request detail views">
                      <button
                        type="button"
                        role="tab"
                        aria-selected={adminDetailTab === "routing"}
                        className={`launcher-request-admin-tab ${adminDetailTab === "routing" ? "is-active" : ""}`}
                        onClick={() => setAdminDetailTab("routing")}
                      >
                        Routing
                      </button>
                      <button
                        type="button"
                        role="tab"
                        aria-selected={adminDetailTab === "analysis"}
                        className={`launcher-request-admin-tab ${adminDetailTab === "analysis" ? "is-active" : ""}`}
                        onClick={() => setAdminDetailTab("analysis")}
                      >
                        Analysis
                      </button>
                    </div>
                    <div className="launcher-request-admin-detail-controls">
                      <label className="launcher-request-admin-field launcher-request-admin-field-inline">
                        <span className="launcher-request-filter-label">Request State</span>
                        <select
                          className="launcher-request-filter-select"
                          value={adminEditorDraft.status}
                          onChange={(event) => updateAdminDraft((current) => ({ ...current, status: event.target.value }))}
                        >
                          <option value="pending">Pending</option>
                          <option value="active">Active</option>
                          <option value="implemented">Implemented</option>
                        </select>
                      </label>
                      {(adminEditorDraft.analysis?.items?.length || 0) > 1 ? (
                        <label className="launcher-request-admin-field launcher-request-admin-field-inline">
                          <span className="launcher-request-filter-label">Analysis Item</span>
                          <select
                            className="launcher-request-filter-select"
                            value={String(selectedAdminAnalysisIndex)}
                            onChange={(event) => setSelectedAdminAnalysisIndex(Number(event.target.value) || 0)}
                          >
                            {(adminEditorDraft.analysis?.items || []).map((item, itemIndex) => (
                              <option key={`analysis-tab-${itemIndex}`} value={itemIndex}>
                                {item.title || `Request ${itemIndex + 1}`}
                              </option>
                            ))}
                          </select>
                        </label>
                      ) : null}
                      <div className="launcher-request-admin-icon-actions">
                        <button
                          type="button"
                          className="launcher-request-admin-icon-btn"
                          onClick={() => void handleSaveAdminRequest()}
                          disabled={adminSaving || requeueingMode !== ""}
                          title="Save all edits to the selected request without changing its review state."
                          aria-label="Save request"
                        >
                          <SaveIcon />
                        </button>
                        <button
                          type="button"
                          className="launcher-request-admin-icon-btn is-success"
                          onClick={() => void handleApproveAdminRequest()}
                          disabled={adminSaving || requeueingMode !== ""}
                          title="Approve the current reviewed item and promote it into the active request list."
                          aria-label="Approve request"
                        >
                          <CheckIcon />
                        </button>
                        <button
                          type="button"
                          className="launcher-request-admin-icon-btn is-success"
                          onClick={() => void handleRequeueAnalysis("draft")}
                          disabled={adminSaving || requeueingMode !== ""}
                          title="Submit the current edited draft back through the analyzer for a fresh manual review pass."
                          aria-label="Manual submission"
                        >
                          <PlayIcon />
                        </button>
                      </div>
                    </div>
                  </div>
                  <section className="launcher-request-admin-analysis-item launcher-request-admin-tab-panel">
                    {adminDetailTab === "routing" ? (
                      <>
                        <div className="launcher-request-admin-analysis-head">
                          <div>
                            <div className="launcher-request-admin-kicker">Routing Pass</div>
                            <div className="launcher-request-admin-request-title">KB Routing Summary</div>
                          </div>
                        </div>
                        <div className="launcher-request-admin-editor-grid launcher-request-admin-editor-grid-wide">
                          <label className="launcher-request-admin-field">
                            <span className="launcher-request-filter-label">Category</span>
                            <select
                              className="launcher-request-filter-select"
                              value={String(adminEditorDraft.category || "")}
                              onChange={(event) => updateAdminDraft((current) => ({ ...current, category: event.target.value }))}
                            >
                              <option value="">Select category</option>
                              {categoryOptions.map((option) => (
                                <option key={`request-category-${option}`} value={option}>{option}</option>
                              ))}
                            </select>
                          </label>
                          <label className="launcher-request-admin-field">
                            <span className="launcher-request-filter-label">Ambiguity</span>
                            <select
                              className="launcher-request-filter-select"
                              value={String(adminEditorDraft.analysis?.routing?.ambiguity || "medium")}
                              onChange={(event) => updateAdminDraft((current) => ({
                                ...current,
                                analysis: {
                                  ...(current.analysis || {}),
                                  routing: {
                                    ...(current.analysis?.routing || {}),
                                    ambiguity: event.target.value,
                                  },
                                },
                              }))}
                            >
                              <option value="low">Low</option>
                              <option value="medium">Medium</option>
                              <option value="high">High</option>
                            </select>
                          </label>
                        </div>
                        <div className="launcher-request-admin-editor-grid">
                          <LauncherChipSelector
                            label="Request Tags"
                            values={adminEditorDraft.tags}
                            options={standardizedTagOptions}
                            placeholder="Add request tag"
                            onAdd={(value) => updateAdminDraft((current) => ({ ...current, tags: appendUniqueString(current.tags, value) }))}
                            onRemove={(value) => updateAdminDraft((current) => ({ ...current, tags: removeStringValue(current.tags, value) }))}
                          />
                          <LauncherChipSelector
                            label="Suggested Tags"
                            values={Array.isArray(adminEditorDraft.analysis?.routing?.suggestedTags) ? adminEditorDraft.analysis.routing.suggestedTags : []}
                            options={standardizedTagOptions}
                            placeholder="Add suggested tag"
                            onAdd={(value) => updateAdminDraft((current) => ({
                              ...current,
                              analysis: {
                                ...(current.analysis || {}),
                                routing: {
                                  ...(current.analysis?.routing || {}),
                                  suggestedTags: appendUniqueString(
                                    Array.isArray(current.analysis?.routing?.suggestedTags) ? current.analysis.routing.suggestedTags : [],
                                    value,
                                  ),
                                },
                              },
                            }))}
                            onRemove={(value) => updateAdminDraft((current) => ({
                              ...current,
                              analysis: {
                                ...(current.analysis || {}),
                                routing: {
                                  ...(current.analysis?.routing || {}),
                                  suggestedTags: removeStringValue(
                                    Array.isArray(current.analysis?.routing?.suggestedTags) ? current.analysis.routing.suggestedTags : [],
                                    value,
                                  ),
                                },
                              },
                            }))}
                          />
                        </div>
                        <label className="launcher-request-admin-field">
                          <span className="launcher-request-filter-label">Routing Summary</span>
                          <textarea
                            className="launcher-request-textarea launcher-request-admin-textarea-sm"
                            value={String(adminEditorDraft.analysis?.routing?.summary || "")}
                            onChange={(event) => updateAdminDraft((current) => ({
                              ...current,
                              analysis: {
                                ...(current.analysis || {}),
                                routing: {
                                  ...(current.analysis?.routing || {}),
                                  summary: event.target.value,
                                },
                              },
                            }))}
                          />
                        </label>
                        <label className="launcher-request-admin-field">
                          <span className="launcher-request-filter-label">Routing Rationale</span>
                          <textarea
                            className="launcher-request-textarea launcher-request-admin-textarea-sm"
                            value={String(adminEditorDraft.analysis?.routing?.rationale || "")}
                            onChange={(event) => updateAdminDraft((current) => ({
                              ...current,
                              analysis: {
                                ...(current.analysis || {}),
                                routing: {
                                  ...(current.analysis?.routing || {}),
                                  rationale: event.target.value,
                                },
                              },
                            }))}
                          />
                        </label>
                        <div className="launcher-request-admin-editor-grid">
                          <label className="launcher-request-admin-field">
                            <span className="launcher-request-filter-label">Matched Terms</span>
                            <textarea
                              className="launcher-request-textarea launcher-request-admin-textarea-xs"
                              value={Array.isArray(adminEditorDraft.analysis?.routing?.matchedTerms) ? adminEditorDraft.analysis.routing.matchedTerms.join("\n") : ""}
                              onChange={(event) => updateAdminDraft((current) => ({
                                ...current,
                                analysis: {
                                  ...(current.analysis || {}),
                                  routing: {
                                    ...(current.analysis?.routing || {}),
                                    matchedTerms: event.target.value.split(/\r?\n/).map((entry) => entry.trim()).filter(Boolean),
                                  },
                                },
                              }))}
                            />
                          </label>
                          <label className="launcher-request-admin-field">
                            <span className="launcher-request-filter-label">Likely Systems</span>
                            <textarea
                              className="launcher-request-textarea launcher-request-admin-textarea-xs"
                              value={Array.isArray(adminEditorDraft.analysis?.routing?.suggestedSystems) ? adminEditorDraft.analysis.routing.suggestedSystems.join("\n") : ""}
                              onChange={(event) => updateAdminDraft((current) => ({
                                ...current,
                                analysis: {
                                  ...(current.analysis || {}),
                                  routing: {
                                    ...(current.analysis?.routing || {}),
                                    suggestedSystems: event.target.value.split(/\r?\n/).map((entry) => entry.trim()).filter(Boolean),
                                  },
                                },
                              }))}
                            />
                          </label>
                        </div>
                        <label className="launcher-request-admin-field">
                          <span className="launcher-request-filter-label">Possible Directions</span>
                          <textarea
                            className="launcher-request-textarea launcher-request-admin-textarea-sm"
                            value={Array.isArray(adminEditorDraft.analysis?.routing?.possibleDirections) ? adminEditorDraft.analysis.routing.possibleDirections.join("\n") : ""}
                            onChange={(event) => updateAdminDraft((current) => ({
                              ...current,
                              analysis: {
                                ...(current.analysis || {}),
                                routing: {
                                  ...(current.analysis?.routing || {}),
                                  possibleDirections: event.target.value.split(/\r?\n/).map((entry) => entry.trim()).filter(Boolean),
                                },
                              },
                            }))}
                          />
                        </label>
                      </>
                    ) : selectedAnalysisItem ? (
                      <>
                        <div className="launcher-request-admin-analysis-head">
                          <div>
                            <div className="launcher-request-admin-kicker">Analysis</div>
                            <div className="launcher-request-admin-request-title">{selectedAnalysisItem.title || "Structured analysis item"}</div>
                          </div>
                        </div>
                        <div className="launcher-request-admin-editor-grid launcher-request-admin-editor-grid-wide">
                          <label className="launcher-request-admin-field">
                            <span className="launcher-request-filter-label">Request Title</span>
                            <input
                              type="text"
                              className="launcher-request-filter-select"
                              value={adminEditorDraft.title}
                              onChange={(event) => updateAdminDraft((current) => ({ ...current, title: event.target.value }))}
                            />
                          </label>
                          <label className="launcher-request-admin-field">
                            <span className="launcher-request-filter-label">Item Title</span>
                            <input
                              type="text"
                              className="launcher-request-filter-select"
                              value={String(selectedAnalysisItem.title || "")}
                              onChange={(event) => updateAdminDraftItem(selectedAdminAnalysisIndex, (current) => ({ ...current, title: event.target.value }))}
                            />
                          </label>
                        </div>
                        <div className="launcher-request-admin-editor-grid launcher-request-admin-editor-grid-wide">
                          <label className="launcher-request-admin-field">
                            <span className="launcher-request-filter-label">Primary Category</span>
                            <select
                              className="launcher-request-filter-select"
                              value={String(selectedAnalysisItem.primaryCategory || "")}
                              onChange={(event) => updateAdminDraftItem(selectedAdminAnalysisIndex, (current) => ({ ...current, primaryCategory: event.target.value }))}
                            >
                              <option value="">Select category</option>
                              {categoryOptions.map((option) => (
                                <option key={`analysis-category-${option}`} value={option}>{option}</option>
                              ))}
                            </select>
                          </label>
                          <label className="launcher-request-admin-field">
                            <span className="launcher-request-filter-label">Problem Type</span>
                            <select
                              className="launcher-request-filter-select"
                              value={String(selectedAnalysisItem.problemType || "unknown")}
                              onChange={(event) => updateAdminDraftItem(selectedAdminAnalysisIndex, (current) => ({ ...current, problemType: event.target.value }))}
                            >
                              <option value="feature">Feature</option>
                              <option value="bug">Bug</option>
                              <option value="workflow">Workflow</option>
                              <option value="performance">Performance</option>
                              <option value="ux">UX</option>
                              <option value="content">Content</option>
                              <option value="unknown">Unknown</option>
                            </select>
                          </label>
                        </div>
                        <div className="launcher-request-admin-editor-grid launcher-request-admin-editor-grid-wide">
                          <label className="launcher-request-admin-field">
                            <span className="launcher-request-filter-label">Recommendation</span>
                            <select
                              className="launcher-request-filter-select"
                              value={String(selectedAnalysisItem.statusRecommendation || "needs_review")}
                              onChange={(event) => updateAdminDraftItem(selectedAdminAnalysisIndex, (current) => ({ ...current, statusRecommendation: event.target.value }))}
                            >
                              <option value="needs_review">Needs Review</option>
                              <option value="active">Active</option>
                              <option value="blocked">Blocked</option>
                              <option value="duplicate">Duplicate</option>
                            </select>
                          </label>
                          <label className="launcher-request-admin-field">
                            <span className="launcher-request-filter-label">Confidence</span>
                            <input
                              type="number"
                              min="0"
                              max="1"
                              step="0.01"
                              className="launcher-request-filter-select"
                              value={Number.isFinite(Number(selectedAnalysisItem.confidence)) ? String(selectedAnalysisItem.confidence) : ""}
                              onChange={(event) => updateAdminDraftItem(selectedAdminAnalysisIndex, (current) => ({
                                ...current,
                                confidence: event.target.value === "" ? null : Number(event.target.value),
                              }))}
                            />
                          </label>
                        </div>
                        <LauncherChipSelector
                          label="Standardized Tags"
                          values={Array.isArray(selectedAnalysisItem.tags) ? selectedAnalysisItem.tags : []}
                          options={standardizedTagOptions}
                          placeholder="Add standardized tag"
                          onAdd={(value) => updateAdminDraftItem(selectedAdminAnalysisIndex, (current) => ({
                            ...current,
                            tags: appendUniqueString(Array.isArray(current.tags) ? current.tags : [], value),
                          }))}
                          onRemove={(value) => updateAdminDraftItem(selectedAdminAnalysisIndex, (current) => ({
                            ...current,
                            tags: removeStringValue(Array.isArray(current.tags) ? current.tags : [], value),
                          }))}
                        />
                        <label className="launcher-request-admin-field">
                          <span className="launcher-request-filter-label">Original Submission</span>
                          <textarea
                            className="launcher-request-textarea launcher-request-admin-textarea-sm"
                            value={adminEditorDraft.sourceText}
                            onChange={(event) => updateAdminDraft((current) => ({ ...current, sourceText: event.target.value }))}
                          />
                        </label>
                        <label className="launcher-request-admin-field">
                          <span className="launcher-request-filter-label">Parsed Interpretation</span>
                          <textarea
                            className="launcher-request-textarea launcher-request-admin-textarea-sm"
                            value={String(selectedAnalysisItem.parsedInterpretation || "")}
                            onChange={(event) => updateAdminDraftItem(selectedAdminAnalysisIndex, (current) => ({ ...current, parsedInterpretation: event.target.value }))}
                          />
                        </label>
                        <label className="launcher-request-admin-field">
                          <span className="launcher-request-filter-label">Implementation Approach</span>
                          <textarea
                            className="launcher-request-textarea launcher-request-admin-textarea-sm"
                            value={String(selectedAnalysisItem.implementationApproach || "")}
                            onChange={(event) => updateAdminDraftItem(selectedAdminAnalysisIndex, (current) => ({ ...current, implementationApproach: event.target.value }))}
                          />
                        </label>
                        <div className="launcher-request-admin-editor-grid">
                          <label className="launcher-request-admin-field">
                            <span className="launcher-request-filter-label">Review Rationale</span>
                            <textarea
                              className="launcher-request-textarea launcher-request-admin-textarea-xs"
                              value={String(selectedAnalysisItem.reviewRationale || "")}
                              onChange={(event) => updateAdminDraftItem(selectedAdminAnalysisIndex, (current) => ({ ...current, reviewRationale: event.target.value }))}
                            />
                          </label>
                          <label className="launcher-request-admin-field">
                            <span className="launcher-request-filter-label">Possible Options</span>
                            <textarea
                              className="launcher-request-textarea launcher-request-admin-textarea-xs"
                              value={Array.isArray(selectedAnalysisItem.reviewOptions) ? selectedAnalysisItem.reviewOptions.join("\n") : ""}
                              onChange={(event) => updateAdminDraftItem(selectedAdminAnalysisIndex, (current) => ({
                                ...current,
                                reviewOptions: event.target.value.split(/\r?\n/).map((entry) => entry.trim()).filter(Boolean),
                              }))}
                            />
                          </label>
                        </div>
                        <label className="launcher-request-admin-field">
                          <span className="launcher-request-filter-label">Notes</span>
                          <textarea
                            className="launcher-request-textarea launcher-request-admin-textarea-xs"
                            value={String(selectedAnalysisItem.notes || "")}
                            onChange={(event) => updateAdminDraftItem(selectedAdminAnalysisIndex, (current) => ({ ...current, notes: event.target.value }))}
                          />
                        </label>
                      </>
                    ) : (
                      <div className="launcher-request-empty">This request does not have a structured analysis item yet.</div>
                    )}
                  </section>
                </div>
              )}
            </section>
          </div>
        </>
      )}
    </section>
  );
}
