import React, { useState } from "react"; import { useMutation } from "@tanstack/react-query"; import { Sparkles, Loader2, ShieldOff, Shield, RefreshCw } from "lucide-react"; import { __ } from "../../lib/i18n"; import { aiApi } from "../../api/ai-api"; import { isAiEligible, isAiModuleEnabled, isAiReady, } from "../../lib/ai-availability"; import { Card, CardContent, CardHeader, CardTitle } from "../ui/card"; import { Button } from "../ui/button"; /** * 3-line AI-generated snapshot of a customer record, surfaced on the * ViewCustomer detail page. Renders only when the operator has the * AI Assistant module on + at least one provider configured (same * gate as every other AI affordance). * * The summary isn't auto-generated on page load — operators click * `Generate summary` so the page stays fast and the AI cost is * spent only when the operator actually wants the view. They can * opt-in to include sensitive PII (medical, dietary, notes) per * generation via a toggle — defaults to OFF so health data doesn't * leak to the LLM unless the operator clearly decides it should. */ interface Props { customerId: number; } export const CustomerSummaryCard: React.FC = ({ customerId }) => { const [includeSensitive, setIncludeSensitive] = useState(false); const [summary, setSummary] = useState(null); const mutation = useMutation({ mutationFn: () => aiApi.getCustomerSummary(customerId, includeSensitive), onSuccess: (resp) => { setSummary(String(resp.data?.text ?? "").trim()); }, }); // Hard gate — don't render the card at all for non-eligible // tiers / unconfigured installs. Saves a visible empty section // on every customer page in the free / Personal tier. // Note: early return MUST come after every hook above, otherwise // the hook-count changes between renders and React's internal // state machine breaks (rules-of-hooks). if (!isAiEligible() || !isAiModuleEnabled()) { return null; } const ready = isAiReady(); return (
{__("AI Customer Summary", "yatra")}

{__( "A 3-line operator-facing snapshot — status, last trip, anything notable for ops.", "yatra", )}

{!summary && ( )}
{!ready && (
{__( "Configure an AI provider key under Yatra → AI Assistant to enable this.", "yatra", )}
)} {/* Sensitive-data opt-in — defaults to OFF so health/dietary notes never reach the LLM unless the operator deliberately flips this for the run. Hidden once we have a result so it doesn't visually compete with the summary itself. */} {!summary && ( )} {mutation.isError && (
{(mutation.error as any)?.response?.data?.message || (mutation.error as any)?.message || __("AI request failed.", "yatra")}
)} {summary && (

{summary}

)}
); }; export default CustomerSummaryCard;