// SPDX-License-Identifier: LicenseRef-Commercial
import { invoke } from '@tauri-apps/api/core';

// --- Analytics ---

export interface AnalyticsSummary {
  totalSessions: number;
  totalCostUsd: number;
  totalTokens: number;
  totalTurns: number;
  totalToolCalls: number;
  avgCostPerSession: number;
  avgTokensPerSession: number;
  periodDays: number;
}

export interface DailyStats {
  date: string;
  sessionCount: number;
  costUsd: number;
  tokens: number;
  turns: number;
  toolCalls: number;
}

export interface ModelBreakdown {
  model: string;
  sessionCount: number;
  totalCostUsd: number;
  totalTokens: number;
  avgCostPerSession: number;
}

export const proAnalyticsSummary = (projectId: string, days?: number) =>
  invoke<AnalyticsSummary>('plugin:agor-pro|pro_analytics_summary', { projectId, days });

export const proAnalyticsDaily = (projectId: string, days?: number) =>
  invoke<DailyStats[]>('plugin:agor-pro|pro_analytics_daily', { projectId, days });

export const proAnalyticsModelBreakdown = (projectId: string, days?: number) =>
  invoke<ModelBreakdown[]>('plugin:agor-pro|pro_analytics_model_breakdown', { projectId, days });

// --- Export ---

export interface SessionReport {
  projectId: string;
  sessionId: string;
  markdown: string;
  costUsd: number;
  turnCount: number;
  toolCallCount: number;
  durationMinutes: number;
  model: string;
}

export interface ProjectSummaryReport {
  projectId: string;
  markdown: string;
  totalSessions: number;
  totalCostUsd: number;
  periodDays: number;
}

export const proExportSession = (projectId: string, sessionId: string) =>
  invoke<SessionReport>('plugin:agor-pro|pro_export_session', { projectId, sessionId });

export const proExportProjectSummary = (projectId: string, days?: number) =>
  invoke<ProjectSummaryReport>('plugin:agor-pro|pro_export_project_summary', { projectId, days });

// --- Profiles ---

export interface AccountProfile {
  id: string;
  displayName: string;
  email: string | null;
  provider: string;
  configDir: string;
  isActive: boolean;
}

export interface ActiveAccount {
  profileId: string;
  provider: string;
  configDir: string;
}

export const proListAccounts = () =>
  invoke<AccountProfile[]>('plugin:agor-pro|pro_list_accounts');

export const proGetActiveAccount = () =>
  invoke<ActiveAccount>('plugin:agor-pro|pro_get_active_account');

export const proSetActiveAccount = (profileId: string) =>
  invoke<ActiveAccount>('plugin:agor-pro|pro_set_active_account', { profileId });

// --- Marketplace ---

export interface CatalogPlugin {
  id: string;
  name: string;
  version: string;
  author: string;
  description: string;
  license: string | null;
  homepage: string | null;
  repository: string | null;
  downloadUrl: string;
  checksumSha256: string;
  sizeBytes: number | null;
  permissions: string[];
  tags: string[] | null;
  minAgorVersion: string | null;
  downloads: number | null;
  rating: number | null;
  createdAt: string | null;
  updatedAt: string | null;
}

export interface InstalledPlugin {
  id: string;
  name: string;
  version: string;
  author: string;
  description: string;
  permissions: string[];
  installPath: string;
  hasUpdate: boolean;
  latestVersion: string | null;
}

export const proMarketplaceFetchCatalog = () =>
  invoke<CatalogPlugin[]>('plugin:agor-pro|pro_marketplace_fetch_catalog');

export const proMarketplaceInstalled = () =>
  invoke<InstalledPlugin[]>('plugin:agor-pro|pro_marketplace_installed');

export const proMarketplaceInstall = (pluginId: string) =>
  invoke<InstalledPlugin>('plugin:agor-pro|pro_marketplace_install', { pluginId });

export const proMarketplaceUninstall = (pluginId: string) =>
  invoke<void>('plugin:agor-pro|pro_marketplace_uninstall', { pluginId });

export const proMarketplaceCheckUpdates = () =>
  invoke<InstalledPlugin[]>('plugin:agor-pro|pro_marketplace_check_updates');

export const proMarketplaceUpdate = (pluginId: string) =>
  invoke<InstalledPlugin>('plugin:agor-pro|pro_marketplace_update', { pluginId });

// --- Status ---

export const proStatus = () =>
  invoke<string>('plugin:agor-pro|pro_status');
