import { Accordion, Alert, Badge, Box, Group, List, Stack, Text, ThemeIcon } from "@mantine/core";
import { IconAlertCircle, IconBulb, IconFileSpreadsheet, IconInfoCircle, IconRoute, IconWriting } from "@tabler/icons-react";

export default function AgentGuidanceBanner() {
  return (
    <Alert variant="light" color="blue" icon={<IconInfoCircle size={18} />} title="Agent Setup Guide" mb="md" styles={{ message: { paddingTop: 4 } }}>
      <Accordion variant="default" chevronPosition="left" defaultValue={null}>
        {/* ── Routing ────────────────────────────────────────────────────────── */}
        <Accordion.Item value="routing">
          <Accordion.Control
            icon={
              <ThemeIcon size="sm" color="blue" variant="light">
                <IconRoute size={13} />
              </ThemeIcon>
            }
          >
            <Text size="sm" fw={600}>
              How agent routing works
            </Text>
          </Accordion.Control>
          <Accordion.Panel>
            <Stack gap="xs">
              <Text size="sm" c="dimmed">
                Every message goes to the <strong>Global Agent</strong> first. If a child agent scores above the threshold it takes over. Scoring is 100 points total:
              </Text>
              <List size="sm" spacing={2}>
                <List.Item>
                  <strong>50 pts</strong> — Embedding similarity between the user query and the agent's prompt text
                </List.Item>
                <List.Item>
                  <strong>30 pts</strong> — Token overlap between the query and agent name / category key
                </List.Item>
                <List.Item>
                  <strong>20 pts</strong> — Column-name overlap between the query and the agent's CSV headers
                </List.Item>
              </List>
              <Text size="xs" c="dimmed">
                Tip: write your prompt to naturally describe what the agent handles — the richer the prompt text, the better the semantic match.
              </Text>
            </Stack>
          </Accordion.Panel>
        </Accordion.Item>

        {/* ── Prompt writing ─────────────────────────────────────────────────── */}
        <Accordion.Item value="prompt">
          <Accordion.Control
            icon={
              <ThemeIcon size="sm" color="violet" variant="light">
                <IconWriting size={13} />
              </ThemeIcon>
            }
          >
            <Text size="sm" fw={600}>
              How to write an agent prompt
            </Text>
          </Accordion.Control>
          <Accordion.Panel>
            <Stack gap="xs">
              <Text size="sm" c="dimmed">
                The prompt is the system instruction sent to the AI for every message this agent handles. It <strong>must</strong> include these placeholders — they are filled in automatically at runtime:
              </Text>
              <Box p="xs" style={{ background: "var(--mantine-color-gray-0)", borderRadius: 6, fontFamily: "monospace", fontSize: 12 }}>
                <Text size="xs" c="dimmed" mb={4}>
                  Required placeholders:
                </Text>
                {[
                  ["{context}", "Retrieved data chunks relevant to the user's query"],
                  ["{user_query}", "The user's actual message"],
                  ["{user_name}", "The user's name (or 'Traveler' as default)"],
                  ["{greeted}", "Whether the user has been greeted already (true/false)"],
                  ["{links_from_excel}", "Source URLs found in the retrieved CSV rows"],
                ].map(([ph, desc]) => (
                  <Group key={ph} gap="xs" wrap="nowrap" mb={2}>
                    <Text component="code" style={{ minWidth: 180 }}>
                      {ph}
                    </Text>
                    <Text size="xs" c="dimmed">
                      {desc}
                    </Text>
                  </Group>
                ))}
              </Box>
              <Alert color="yellow" variant="light" icon={<IconAlertCircle size={14} />} p="xs">
                <Text size="xs">If any required placeholder is missing the system will raise a formatting error. Always include all five.</Text>
              </Alert>
              <Text size="sm" fw={500} mt={4}>
                Good prompt structure:
              </Text>
              <List size="sm" spacing={2}>
                <List.Item>
                  Open with a clear description of <em>what this agent specialises in</em> — e.g. "You are a wine specialist for Vrutik Estate." This text is used for semantic matching.
                </List.Item>
                <List.Item>Define personality and tone.</List.Item>
                <List.Item>List banned phrases and formatting rules.</List.Item>
                <List.Item>Specify the human-escalation contact block if needed.</List.Item>
                <List.Item>End with the USER CONTEXT block containing all five placeholders.</List.Item>
              </List>
            </Stack>
          </Accordion.Panel>
        </Accordion.Item>

        {/* ── Category key ──────────────────────────────────────────────────── */}
        <Accordion.Item value="category">
          <Accordion.Control
            icon={
              <ThemeIcon size="sm" color="orange" variant="light">
                <IconBulb size={13} />
              </ThemeIcon>
            }
          >
            <Text size="sm" fw={600}>
              Category key conventions
            </Text>
          </Accordion.Control>
          <Accordion.Panel>
            <Stack gap="xs">
              <Text size="sm" c="dimmed">
                The category key identifies what this agent handles. Use <strong>lowercase, no spaces</strong>. It is used in routing score and data-service selection.
              </Text>
              <Group gap="xs" wrap="wrap">
                {[
                  ["general", "blue"],
                  ["accommodation", "green"],
                  ["maps", "orange"],
                  ["dining", "pink"],
                  ["concierge", "teal"],
                  ["sales", "grape"],
                ].map(([key, color]) => (
                  <Badge key={key} color={color} size="sm" variant="light">
                    {key}
                  </Badge>
                ))}
              </Group>
              <Text size="xs" c="dimmed">
                These are examples — you can use any lowercase key that describes your agent's domain. The system picks the right data service automatically based on the CSV URL.
              </Text>
              <Text size="xs" c="dimmed">
                <strong>maps</strong> is special: its CSV URL is loaded by the MapsService (requires columns{" "}
                <Text component="code" size="xs">
                  Resort_Name
                </Text>
                ,{" "}
                <Text component="code" size="xs">
                  Source_Link
                </Text>
                ). All other categories use ExcelService and support any CSV structure.
              </Text>
            </Stack>
          </Accordion.Panel>
        </Accordion.Item>

        {/* ── CSV / Data source ─────────────────────────────────────────────── */}
        <Accordion.Item value="csv">
          <Accordion.Control
            icon={
              <ThemeIcon size="sm" color="green" variant="light">
                <IconFileSpreadsheet size={13} />
              </ThemeIcon>
            }
          >
            <Text size="sm" fw={600}>
              CSV / Excel data source format
            </Text>
          </Accordion.Control>
          <Accordion.Panel>
            <Stack gap="xs">
              <Text size="sm" c="dimmed">
                Paste a publicly accessible CSV or Excel URL in the <strong>Data Source URL</strong> field. The system downloads, embeds, and caches it automatically.
              </Text>

              <Text size="sm" fw={500}>
                General agents (accommodation, dining, sales, etc.)
              </Text>
              <List size="sm" spacing={2}>
                <List.Item>Any column structure is supported — the first column is used as the row label.</List.Item>
                <List.Item>Each row becomes one retrievable chunk. Include descriptive column names — they contribute to routing score.</List.Item>
                <List.Item>URLs in cells are recognised and surfaced as source links in the response.</List.Item>
                <List.Item>
                  Recommended columns:{" "}
                  <Text component="code" size="xs">
                    Name
                  </Text>
                  ,{" "}
                  <Text component="code" size="xs">
                    Description
                  </Text>
                  ,{" "}
                  <Text component="code" size="xs">
                    Price
                  </Text>
                  ,{" "}
                  <Text component="code" size="xs">
                    Availability
                  </Text>
                  ,{" "}
                  <Text component="code" size="xs">
                    Source_Link
                  </Text>
                </List.Item>
              </List>

              <Text size="sm" fw={500} mt={4}>
                Maps agent (special format required)
              </Text>
              <List size="sm" spacing={2}>
                <List.Item>
                  <strong>Required:</strong>{" "}
                  <Text component="code" size="xs">
                    Resort_Name
                  </Text>
                  ,{" "}
                  <Text component="code" size="xs">
                    Source_Link
                  </Text>
                </List.Item>
                <List.Item>
                  <strong>Optional:</strong>{" "}
                  <Text component="code" size="xs">
                    Content_Text
                  </Text>
                  ,{" "}
                  <Text component="code" size="xs">
                    Category
                  </Text>
                  ,{" "}
                  <Text component="code" size="xs">
                    Country
                  </Text>
                  ,{" "}
                  <Text component="code" size="xs">
                    State_or_Location
                  </Text>
                </List.Item>
                <List.Item>
                  <Text component="code" size="xs">
                    Source_Link
                  </Text>{" "}
                  should be the Google Maps embed/share URL for that resort.
                </List.Item>
              </List>

              <Alert color="blue" variant="light" icon={<IconBulb size={14} />} p="xs">
                <Text size="xs">
                  <strong>Cache refreshes automatically</strong> when you save an agent with an updated URL. If the URL stays the same but the file content changes, save the agent again to force a reload — the server will re-download and re-embed the latest data on the next request.
                </Text>
              </Alert>
            </Stack>
          </Accordion.Panel>
        </Accordion.Item>
      </Accordion>
    </Alert>
  );
}
