// Shared contract between the backend ChatbotToolRunner (PHP) and the widget UI.
// Each tool call produces one or more of these blocks, rendered by BlockRenderer.

export type ChatbotRole = 'user' | 'assistant';

export interface StatBlock {
    type: 'stat';
    label: string;
    value: number | string;
    // Optional follow-up question sent when the card is clicked (drill-down).
    drill?: string;
}

export interface TableBlock {
    type: 'table';
    title?: string;
    columns: string[];
    rows: Array<Record<string, unknown>>;
    sql?: string;
    // Optional per-row follow-up questions (parallel to `rows`); a row is clickable
    // when its entry is a non-empty string.
    rowDrills?: Array<string | null>;
}

export interface ChartDatum {
    label: string;
    value: number;
    drill?: string;
}

export interface ChartBlock {
    type: 'chart';
    title?: string;
    chartType: 'bar' | 'line' | 'pie';
    data: ChartDatum[];
    valueLabel?: string;
}

export interface PropertyItem {
    id: string | number;
    name: string;
    state?: string | null;
    price: number;
    currency: string;
    capacity: number;
    image?: string | null;
    url?: string | null;
    drill?: string;
}

export interface PropertiesBlock {
    type: 'properties';
    title?: string;
    items: PropertyItem[];
}

export interface TextBlock {
    type: 'text';
    html: string;
}

export interface ReservationRow {
    client: string;
    logement: string;
    check_in: string;
    check_out: string;
    nights: number;
    price: number;
    currency: string;
    channel: string;
    status: string;
    paid: string;
    paid_state: 'paid' | 'unpaid' | 'pending' | 'other';
    drill?: string;
}

export interface ReservationsBlock {
    type: 'reservations';
    title?: string;
    total: number;
    shown: number;
    rows: ReservationRow[];
}

export interface MetricItem {
    label: string;
    value: string;
    hint?: string;
}

export interface MetricsBlock {
    type: 'metrics';
    title?: string;
    items: MetricItem[];
}

// --- Rich blocks (phases 2, 3, 5) ---

export interface ProfileSection {
    title: string;
    items: MetricItem[];
}

export interface ProfileBlock {
    type: 'profile';
    entity: 'property' | 'client';
    name: string;
    subtitle?: string;
    image?: string | null;
    sections: ProfileSection[];
    links?: Array<{ label: string; url: string }>;
    // Nested blocks appended under the profile (e.g. recent reservations table).
    blocks?: ChatbotBlock[];
}

export interface DashboardBlock {
    type: 'dashboard';
    title?: string;
    layout?: 'grid-2' | 'grid-3' | 'stacked';
    blocks: ChatbotBlock[];
}

export interface ClarificationBlock {
    type: 'clarification';
    question: string;
    // 0..6 suggested replies; clicking one sends it as the next user message.
    options: string[];
}

export interface ActionBlock {
    type: 'action';
    actionId: string;
    title: string;
    message: string;
    actionLabel: string;
    destructive?: boolean;
    expiresAt?: string;
}

// Scrollable per-entity comparison table (e.g. occupancy this year vs last).
// Always used for multi-property stats instead of one metrics card per row.
export interface ComparisonRow {
    label: string;
    sublabel?: string | null;
    // Primary metric for the current period. Drives the bar: scaled to `barMax`
    // when set (e.g. revenue), otherwise read as an absolute percentage (occupancy).
    current: number;
    // Same metric for the compare period; when present the renderer shows the Δ.
    previous?: number | null;
    // Extra plain columns (revenue, reservations…), rendered right-aligned.
    columns?: Array<{ label: string; value: string | number }>;
    drill?: string;
}

export interface ComparisonBlock {
    type: 'comparison';
    title?: string;
    metricLabel?: string;
    unit?: string;
    periodLabel?: string;
    comparePeriodLabel?: string;
    // When set, bars scale relative to this max (non-percentage metrics like revenue);
    // absent → `current` is treated as an absolute 0–100 percentage (occupancy).
    barMax?: number;
    // Metric the table is sorted by, for the caption ("occupation" | "revenu").
    sortLabel?: string;
    rows: ComparisonRow[];
}

export type ChatbotBlock =
    | StatBlock
    | TableBlock
    | ChartBlock
    | PropertiesBlock
    | TextBlock
    | ReservationsBlock
    | MetricsBlock
    | ComparisonBlock
    | ProfileBlock
    | DashboardBlock
    | ActionBlock
    | ClarificationBlock;

export interface ChatbotMessage {
    id: string;
    role: ChatbotRole;
    content: string;
    blocks?: ChatbotBlock[];
    error?: boolean;
}

export interface ChatbotResponse {
    answer: string;
    blocks: ChatbotBlock[];
    // Server-minted conversation uuid — resend it on the next turn so the
    // backend can rebuild the history server-side.
    conversation_id?: string;
    error?: string;
}

export interface ChatbotHistoryTurn {
    role: ChatbotRole;
    content: string;
}
