/**
 * Full-Text Search module — types and schemas.
 * Provides cross-entity search across platform data (users, items, notes, etc.).
 * Uses Cosmos DB SQL queries with CONTAINS/LIKE for MVP, upgradable to
 * Azure Cognitive Search or Elasticsearch later.
 */

import { z } from 'zod';

// ── Searchable Entity Types ──────────────────────────────────────

export type SearchEntityType =
  | 'user'
  | 'item'
  | 'broadcast'
  | 'survey'
  | 'changelog'
  | 'audit_log'
  | 'knowledge_article';

export interface SearchResultDoc {
  /** Entity ID */
  entityId: string;
  /** Entity type */
  entityType: SearchEntityType;
  /** Product scope */
  productId: string;
  /** Display title / name */
  title: string;
  /** Text snippet with match highlight markers */
  snippet: string;
  /** Relevance score (0-1) */
  score: number;
  /** Deep link path within the product */
  deepLink: string;
  /** When the entity was last modified */
  updatedAt: string;
}

export interface SearchIndexDoc {
  id: string;
  productId: string;
  entityId: string;
  entityType: SearchEntityType;
  /** Searchable text (title + body + tags combined) */
  searchText: string;
  /** Display title */
  title: string;
  /** Deep link path */
  deepLink: string;
  /** Tags / categories for faceted search */
  tags: string[];
  /** Whether the entity is active / not deleted */
  isActive: boolean;
  indexedAt: string;
  updatedAt: string;
}

export interface SearchSuggestionDoc {
  id: string;
  productId: string;
  /** The suggestion text */
  text: string;
  /** How many times this query was searched */
  queryCount: number;
  /** How many times a result was clicked from this query */
  clickCount: number;
  updatedAt: string;
}

// ── Schemas ──────────────────────────────────────────────────────

export const SearchQuerySchema = z.object({
  q: z.string().min(1).max(500),
  entityTypes: z
    .array(
      z.enum(['user', 'item', 'broadcast', 'survey', 'changelog', 'audit_log', 'knowledge_article'])
    )
    .optional(),
  tags: z.array(z.string().max(64)).max(10).optional(),
  from: z.string().datetime().optional(),
  to: z.string().datetime().optional(),
  limit: z.number().int().min(1).max(100).default(20),
  offset: z.number().int().min(0).default(0),
});

export const IndexEntitySchema = z.object({
  entityId: z.string().min(1),
  entityType: z.enum([
    'user',
    'item',
    'broadcast',
    'survey',
    'changelog',
    'audit_log',
    'knowledge_article',
  ]),
  title: z.string().min(1).max(500),
  searchText: z.string().min(1).max(10000),
  deepLink: z.string().min(1).max(512),
  tags: z.array(z.string().max(64)).max(20).default([]),
  isActive: z.boolean().default(true),
});

export const ReindexRequestSchema = z.object({
  entityTypes: z
    .array(
      z.enum(['user', 'item', 'broadcast', 'survey', 'changelog', 'audit_log', 'knowledge_article'])
    )
    .min(1),
});

export type SearchQueryInput = z.infer<typeof SearchQuerySchema>;
export type IndexEntityInput = z.infer<typeof IndexEntitySchema>;
export type ReindexRequestInput = z.infer<typeof ReindexRequestSchema>;
