/**
 * Usage tracking types — ported from admin-dashboard-web + Python backend.
 */

import { z } from 'zod';

export interface UsageDoc {
  id: string;
  productId: string;
  userId: string;
  date: string;
  dictations: number;
  words: number;
  durationMs: number;
  tokensUsed: number;
  costUsd: number;
  model?: string;
  source?: string;
  createdAt: string;
}

export interface SourceBreakdown {
  source: string;
  tokens: number;
  requests: number;
  cost: number;
}

export interface UsageSummary {
  totalWords: number;
  totalDictations: number;
  totalTokens: number;
  totalCost: number;
  records: UsageDoc[];
  modelBreakdown?: ModelBreakdown[];
  sourceBreakdown?: SourceBreakdown[];
}

export interface ModelBreakdown {
  model: string;
  tokens: number;
  requests: number;
  cost: number;
}

export interface ProductBreakdown {
  productId: string;
  tokens: number;
  requests: number;
  cost: number;
}

export interface MonthlyUsage {
  tokens: number;
  words: number;
  dictations: number;
}

export const UpsertUsageSchema = z.object({
  userId: z.string().min(1),
  date: z.string().regex(/^\d{4}-\d{2}-\d{2}$/),
  dictations: z.number().int().min(0).default(0),
  words: z.number().int().min(0).default(0),
  durationMs: z.number().int().min(0).default(0),
  tokensUsed: z.number().int().min(0).default(0),
  costUsd: z.number().min(0).default(0),
  model: z.string().optional(),
  source: z.string().optional(),
});

export const CheckLimitsSchema = z.object({
  userId: z.string().min(1),
  plan: z.enum(['free', 'pro', 'enterprise']),
});

export type UpsertUsageInput = z.infer<typeof UpsertUsageSchema>;
export type CheckLimitsInput = z.infer<typeof CheckLimitsSchema>;
