// ============================================
// SHARED TYPES FOR GOOGLE ADS MCP
// ============================================

export interface ToolDefinition {
  name: string;
  description: string;
  category: ToolCategory;
  annotations: ToolAnnotations;
  inputSchema: {
    type: "object";
    properties: Record<string, any>;
    required?: string[];
  };
  _meta?: {
    labels: {
      category: string;
      access: "read" | "write" | "delete";
      complexity: "simple" | "complex" | "batch";
    };
  };
  handler: (args: Record<string, any>, client: GoogleAdsClient) => Promise<ToolResult>;
}

export interface ToolAnnotations {
  title: string;
  readOnlyHint: boolean;
  destructiveHint: boolean;
  idempotentHint: boolean;
  openWorldHint: boolean;
}

export type ToolCategory =
  | "accounts"
  | "campaigns"
  | "ad_groups"
  | "ads"
  | "keywords"
  | "reporting"
  | "bidding"
  | "conversions"
  | "advanced";

export interface ToolResult {
  content: Array<{
    type: "text" | "resource";
    text?: string;
    resource?: any;
  }>;
  isError?: boolean;
  structuredContent?: any;
}

export interface GoogleAdsClient {
  query(customerId: string, gaqlQuery: string): Promise<any[]>;
  mutate(customerId: string, operations: MutateOperation[]): Promise<any>;
  listAccessibleCustomers(): Promise<string[]>;
  getCustomerId(): string;
}

export interface MutateOperation {
  entity: string;
  operation: "create" | "update" | "remove";
  resource?: Record<string, any>;
  resourceName?: string;
  updateMask?: string;
}

export interface GoogleAdsConfig {
  clientId: string;
  clientSecret: string;
  developerToken: string;
  refreshToken: string;
  loginCustomerId?: string;
  customerId?: string;
}

// ============================================
// API RESPONSE TYPES
// ============================================

export interface Campaign {
  resourceName: string;
  id: string;
  name: string;
  status: string;
  advertisingChannelType: string;
  biddingStrategyType: string;
  startDate?: string;
  endDate?: string;
  budget?: CampaignBudget;
  metrics?: CampaignMetrics;
}

export interface CampaignBudget {
  resourceName: string;
  amountMicros: string;
  deliveryMethod: string;
  totalAmountMicros?: string;
}

export interface CampaignMetrics {
  impressions: string;
  clicks: string;
  costMicros: string;
  conversions: number;
  conversionsValue: number;
  ctr: number;
  averageCpc: string;
  averageCpm: string;
}

export interface AdGroup {
  resourceName: string;
  id: string;
  name: string;
  status: string;
  campaignId: string;
  type: string;
  cpcBidMicros?: string;
  metrics?: AdGroupMetrics;
}

export interface AdGroupMetrics {
  impressions: string;
  clicks: string;
  costMicros: string;
  conversions: number;
  ctr: number;
  averageCpc: string;
}

export interface Ad {
  resourceName: string;
  id: string;
  type: string;
  status: string;
  adGroupId: string;
  finalUrls: string[];
  responsiveSearchAd?: {
    headlines: Array<{ text: string; pinnedField?: string }>;
    descriptions: Array<{ text: string; pinnedField?: string }>;
  };
  metrics?: AdMetrics;
}

export interface AdMetrics {
  impressions: string;
  clicks: string;
  costMicros: string;
  conversions: number;
  ctr: number;
}

export interface Keyword {
  resourceName: string;
  criterionId: string;
  keyword: {
    text: string;
    matchType: string;
  };
  status: string;
  adGroupId: string;
  qualityInfo?: {
    qualityScore: number;
    creativityScore: string;
    searchPredictedCtr: string;
    postClickQualityScore: string;
  };
  metrics?: KeywordMetrics;
}

export interface KeywordMetrics {
  impressions: string;
  clicks: string;
  costMicros: string;
  conversions: number;
  ctr: number;
  averageCpc: string;
  averagePosition?: number;
}

export interface SearchTermView {
  searchTerm: string;
  adGroup: string;
  campaign: string;
  status: string;
  metrics: {
    impressions: string;
    clicks: string;
    costMicros: string;
    conversions: number;
    ctr: number;
  };
}

export interface ConversionAction {
  resourceName: string;
  id: string;
  name: string;
  type: string;
  status: string;
  category: string;
  countingType: string;
}

export interface Recommendation {
  resourceName: string;
  type: string;
  impact: {
    baseMetrics: Record<string, any>;
    potentialMetrics: Record<string, any>;
  };
  campaignBudgetRecommendation?: any;
  keywordRecommendation?: any;
  targetCpaOptInRecommendation?: any;
}

export interface ChangeEvent {
  resourceName: string;
  changeDateTime: string;
  changeResourceType: string;
  changeResourceName: string;
  clientType: string;
  userEmail: string;
  oldResource?: any;
  newResource?: any;
}

// ============================================
// FORMATTING HELPERS
// ============================================

export function microsToMoney(micros: string | number): string {
  const num = typeof micros === "string" ? parseInt(micros) : micros;
  return (num / 1_000_000).toFixed(2);
}

export function formatPercent(value: number): string {
  return (value * 100).toFixed(2) + "%";
}

export function formatNumber(value: string | number): string {
  const num = typeof value === "string" ? parseInt(value) : value;
  return num.toLocaleString();
}
