/**
 * Types for @bytelyst/marketplace-client.
 * Browser/React Native-safe — no Node.js dependencies.
 */

export interface MarketplaceListingDoc {
  id: string;
  productId: string;
  templateType: string;
  authorId: string;
  authorName: string;
  title: string;
  shortDescription: string;
  description: string;
  tags: string[];
  category: string;
  payload: Record<string, unknown>;
  pricingModel: 'free' | 'paid' | 'freemium';
  priceInCents: number;
  certificationStatus: 'draft' | 'submitted' | 'in_review' | 'approved' | 'rejected' | 'suspended';
  installCount: number;
  reviewCount: number;
  averageRating: number;
  visibility: 'private' | 'unlisted' | 'public';
  featured: boolean;
  version: string;
  createdAt: string;
  updatedAt: string;
}

export interface MarketplaceReviewDoc {
  id: string;
  listingId: string;
  productId: string;
  authorId: string;
  rating: number;
  title: string;
  body: string;
  verified: boolean;
  createdAt: string;
}

export interface MarketplaceInstallDoc {
  id: string;
  listingId: string;
  productId: string;
  userId: string;
  version: string;
  installedAt: string;
  uninstalledAt: string | null;
}

export interface CreateListingInput {
  templateType: string;
  title: string;
  shortDescription: string;
  description: string;
  tags?: string[];
  category: string;
  payload: Record<string, unknown>;
  pricingModel?: 'free' | 'paid' | 'freemium';
  priceInCents?: number;
  visibility?: 'private' | 'unlisted' | 'public';
  version?: string;
}

export interface MarketplaceClientConfig {
  /** Platform-service base URL (e.g. "http://localhost:4003/api"). */
  baseUrl: string;

  /** Product identifier sent as x-product-id header on every request. */
  productId: string;

  /** Returns a JWT access token, or null if not authenticated. */
  getAccessToken: () => string | null;
}

export interface MarketplaceClient {
  // Listings
  listListings(query?: {
    templateType?: string;
    category?: string;
    tags?: string;
    pricingModel?: string;
    sortBy?: string;
    q?: string;
    limit?: number;
    offset?: number;
  }): Promise<{ listings: MarketplaceListingDoc[]; total: number }>;
  getListing(id: string): Promise<MarketplaceListingDoc>;
  createListing(input: CreateListingInput): Promise<MarketplaceListingDoc>;
  updateListing(
    id: string,
    updates: Partial<MarketplaceListingDoc>
  ): Promise<MarketplaceListingDoc>;
  submitForCertification(id: string, notes?: string): Promise<MarketplaceListingDoc>;

  // Installs
  installListing(listingId: string): Promise<MarketplaceInstallDoc>;
  uninstallListing(listingId: string): Promise<void>;
  listMyInstalls(query?: { limit?: number; offset?: number }): Promise<MarketplaceInstallDoc[]>;

  // Reviews
  listReviews(
    listingId: string,
    query?: { sortBy?: string; limit?: number }
  ): Promise<MarketplaceReviewDoc[]>;
  createReview(
    listingId: string,
    input: { rating: number; title: string; body: string }
  ): Promise<MarketplaceReviewDoc>;

  // Reports
  reportListing(listingId: string, input: { reason: string; details: string }): Promise<void>;
}
