/**
 * Zod schemas for analytics tool inputs
 */

import { z } from 'zod';

/**
 * Base schema for search analytics queries
 */
export const SearchAnalyticsSchema = z.object({
  siteUrl: z.string().describe('Google Search Console property URL'),
  startDate: z.string().optional().describe('Start date in YYYY-MM-DD format (optional if using days)'),
  endDate: z.string().optional().describe('End date in YYYY-MM-DD format (optional if using days)'),
  days: z.number().int().min(1).max(480).optional().describe('Relative date range in days (alternative to explicit dates)'),
  dimensions: z.array(z.enum(['query', 'page', 'country', 'device', 'searchAppearance', 'date']))
    .optional()
    .describe('Dimensions to group by'),
  type: z.enum(['web', 'image', 'video', 'news', 'discover', 'googleNews'])
    .optional()
    .default('web')
    .describe('Search type to analyze'),
  aggregationType: z.enum(['auto', 'byProperty', 'byPage'])
    .optional()
    .default('auto')
    .describe('How to aggregate results'),
  rowLimit: z.number().int().min(1).max(25000)
    .optional()
    .default(1000)
    .describe('Maximum number of rows to return (max 25,000)'),
  dataState: z.enum(['all', 'final'])
    .optional()
    .default('final')
    .describe('Data freshness: "all" includes recent unconfirmed data, "final" only confirmed'),
  filters: z.array(z.object({
    dimension: z.enum(['query', 'page', 'country', 'device', 'searchAppearance']),
    operator: z.enum(['equals', 'notEquals', 'contains', 'notContains', 'includingRegex', 'excludingRegex']),
    expression: z.string()
  })).optional().describe('Filter conditions'),
  filterOperator: z.enum(['and', 'or'])
    .optional()
    .default('and')
    .describe('How to combine multiple filters')
});

export type SearchAnalyticsInput = z.infer<typeof SearchAnalyticsSchema>;

/**
 * Compare two time periods
 */
export const ComparePeriodSchema = z.object({
  siteUrl: z.string().describe('Google Search Console property URL'),
  currentStartDate: z.string().optional().describe('Current period start (YYYY-MM-DD)'),
  currentEndDate: z.string().optional().describe('Current period end (YYYY-MM-DD)'),
  previousStartDate: z.string().optional().describe('Previous period start (YYYY-MM-DD)'),
  previousEndDate: z.string().optional().describe('Previous period end (YYYY-MM-DD)'),
  currentDays: z.number().int().min(1).max(480).optional().describe('Current period length in days'),
  previousDays: z.number().int().min(1).max(480).optional().describe('Previous period length in days (defaults to currentDays)'),
  dimensions: z.array(z.enum(['query', 'page', 'country', 'device']))
    .optional()
    .default(['query'])
    .describe('Dimensions to compare'),
  rowLimit: z.number().int().min(1).max(1000)
    .optional()
    .default(100)
    .describe('Number of top rows to compare')
});

export type ComparePeriodInput = z.infer<typeof ComparePeriodSchema>;

/**
 * Identify top movers (gainers and losers)
 */
export const TopMoversSchema = z.object({
  siteUrl: z.string().describe('Google Search Console property URL'),
  currentStartDate: z.string().optional(),
  currentEndDate: z.string().optional(),
  previousStartDate: z.string().optional(),
  previousEndDate: z.string().optional(),
  currentDays: z.number().int().min(1).max(480).optional().describe('Current period (default 7 days)'),
  previousDays: z.number().int().min(1).max(480).optional(),
  dimension: z.enum(['query', 'page'])
    .optional()
    .default('query')
    .describe('What to analyze for movement'),
  metric: z.enum(['clicks', 'impressions', 'ctr', 'position'])
    .optional()
    .default('clicks')
    .describe('Metric to measure movement'),
  minImpressions: z.number().int().min(0)
    .optional()
    .default(10)
    .describe('Minimum impressions to include'),
  limit: z.number().int().min(1).max(100)
    .optional()
    .default(20)
    .describe('Number of top gainers and losers to return')
});

export type TopMoversInput = z.infer<typeof TopMoversSchema>;

/**
 * Mobile vs Desktop performance breakdown
 */
export const MobileVsDesktopSchema = z.object({
  siteUrl: z.string().describe('Google Search Console property URL'),
  startDate: z.string().optional(),
  endDate: z.string().optional(),
  days: z.number().int().min(1).max(480).optional().default(28),
  dimension: z.enum(['query', 'page'])
    .optional()
    .default('page')
    .describe('Group results by query or page'),
  rowLimit: z.number().int().min(1).max(1000)
    .optional()
    .default(100)
});

export type MobileVsDesktopInput = z.infer<typeof MobileVsDesktopSchema>;

/**
 * Country/geo performance breakdown
 */
export const CountryBreakdownSchema = z.object({
  siteUrl: z.string().describe('Google Search Console property URL'),
  startDate: z.string().optional(),
  endDate: z.string().optional(),
  days: z.number().int().min(1).max(480).optional().default(28),
  rowLimit: z.number().int().min(1).max(1000)
    .optional()
    .default(50)
    .describe('Number of countries to return')
});

export type CountryBreakdownInput = z.infer<typeof CountryBreakdownSchema>;
