/**
 * Marketplace Module Types
 * Type definitions for signal packs, advisory services, courses, and marketplace products
 */

// ============================================================================
// Enums
// ============================================================================

export type ProductCategory = 'signals' | 'courses' | 'advisory' | 'tools';
export type ProductStatus = 'active' | 'inactive' | 'draft' | 'archived';
export type SubscriptionPeriod = 'monthly' | 'quarterly' | 'yearly' | 'lifetime';
export type AdvisoryAvailability = 'available' | 'busy' | 'away' | 'offline';

// ============================================================================
// Provider Types
// ============================================================================

export interface MarketplaceProvider {
  id: string;
  name: string;
  avatarUrl?: string;
  bio?: string;
  verified: boolean;
  rating: number;
  totalReviews: number;
  totalSubscribers: number;
  joinedAt: string;
  specializations: string[];
  socialLinks?: {
    twitter?: string;
    telegram?: string;
    website?: string;
  };
}

// ============================================================================
// Signal Pack Types
// ============================================================================

export interface SignalPack {
  id: string;
  name: string;
  slug: string;
  description: string;
  shortDescription?: string;
  thumbnailUrl?: string;
  provider: MarketplaceProvider;
  category: ProductCategory;
  status: ProductStatus;

  // Signal Stats
  winRate: number;
  avgProfitPercent: number;
  totalSignals: number;
  avgSignalsPerWeek: number;
  symbolsCovered: string[];
  tradingStyle: string;
  riskLevel: 'low' | 'medium' | 'high';

  // Pricing
  pricing: SignalPackPricing[];
  hasFreeTrial: boolean;
  freeTrialDays?: number;

  // Performance
  monthlyReturns: MonthlyReturn[];
  recentSignals: SignalPreview[];

  // Meta
  totalSubscribers: number;
  rating: number;
  totalReviews: number;
  createdAt: string;
  updatedAt: string;
}

export interface SignalPackPricing {
  id: string;
  period: SubscriptionPeriod;
  priceUsd: number;
  originalPriceUsd?: number;
  discountPercent?: number;
  isPopular?: boolean;
}

export interface MonthlyReturn {
  month: string;
  year: number;
  returnPercent: number;
  totalSignals: number;
  winRate: number;
}

export interface SignalPreview {
  id: string;
  symbol: string;
  direction: 'long' | 'short';
  entryPrice: number;
  takeProfitPrice?: number;
  stopLossPrice?: number;
  status: 'pending' | 'active' | 'closed';
  profitLossPercent?: number;
  createdAt: string;
  closedAt?: string;
}

// ============================================================================
// Advisory Service Types
// ============================================================================

export interface AdvisoryService {
  id: string;
  name: string;
  slug: string;
  description: string;
  shortDescription?: string;
  thumbnailUrl?: string;
  advisor: AdvisorProfile;
  category: ProductCategory;
  status: ProductStatus;

  // Service Details
  specializations: string[];
  serviceTypes: ServiceType[];
  languages: string[];

  // Availability
  availability: AdvisoryAvailability;
  availableSlots: AvailabilitySlot[];
  timezone: string;
  responseTimeHours: number;

  // Pricing
  pricing: AdvisoryPricing[];

  // Meta
  totalConsultations: number;
  rating: number;
  totalReviews: number;
  createdAt: string;
  updatedAt: string;
}

export interface AdvisorProfile extends MarketplaceProvider {
  title?: string;
  experience: string;
  credentials: string[];
  portfolio?: string;
  tradingExperienceYears: number;
}

export interface ServiceType {
  id: string;
  name: string;
  description: string;
  durationMinutes: number;
  priceUsd: number;
}

export interface AvailabilitySlot {
  id: string;
  dayOfWeek: number;
  startTime: string;
  endTime: string;
  isAvailable: boolean;
}

export interface AdvisoryPricing {
  id: string;
  serviceTypeId: string;
  serviceType: ServiceType;
  priceUsd: number;
  discountedPriceUsd?: number;
}

// ============================================================================
// Course Product Types (Marketplace view)
// ============================================================================

export interface CourseProduct {
  id: string;
  title: string;
  slug: string;
  description: string;
  shortDescription?: string;
  thumbnailUrl?: string;
  instructor: MarketplaceProvider;
  category: ProductCategory;
  status: ProductStatus;

  // Course Details
  difficultyLevel: 'beginner' | 'intermediate' | 'advanced';
  totalLessons: number;
  totalDuration: number;
  topics: string[];

  // Pricing
  priceUsd: number;
  originalPriceUsd?: number;
  isFree: boolean;

  // Meta
  totalEnrollments: number;
  rating: number;
  totalReviews: number;
  createdAt: string;
  updatedAt: string;
}

// ============================================================================
// Unified Marketplace Product
// ============================================================================

export type MarketplaceProduct =
  | (SignalPack & { productType: 'signals' })
  | (AdvisoryService & { productType: 'advisory' })
  | (CourseProduct & { productType: 'courses' });

export interface MarketplaceProductListItem {
  id: string;
  name: string;
  slug: string;
  shortDescription?: string;
  thumbnailUrl?: string;
  productType: ProductCategory;
  providerName: string;
  providerAvatar?: string;
  providerVerified: boolean;
  priceUsd: number;
  hasFreeTrial?: boolean;
  rating: number;
  totalReviews: number;
  totalSubscribers?: number;
  tags: string[];
  createdAt: string;
}

// ============================================================================
// Review Types
// ============================================================================

export interface ProductReview {
  id: string;
  productId: string;
  productType: ProductCategory;
  userId: string;
  userName: string;
  userAvatar?: string;
  rating: number;
  title?: string;
  comment: string;
  helpful: number;
  verified: boolean;
  createdAt: string;
  updatedAt: string;
  response?: ProviderResponse;
}

export interface ProviderResponse {
  comment: string;
  respondedAt: string;
}

// ============================================================================
// Subscription Types
// ============================================================================

export interface UserSubscription {
  id: string;
  userId: string;
  productId: string;
  productType: ProductCategory;
  product: MarketplaceProductListItem;
  status: 'active' | 'cancelled' | 'expired' | 'paused';
  period: SubscriptionPeriod;
  priceUsd: number;
  startDate: string;
  endDate: string;
  autoRenew: boolean;
  createdAt: string;
}

// ============================================================================
// Filter Types
// ============================================================================

export interface MarketplaceFilters {
  category?: ProductCategory;
  search?: string;
  minRating?: number;
  maxPrice?: number;
  minPrice?: number;
  sortBy?: 'newest' | 'popular' | 'rating' | 'price_asc' | 'price_desc';
  hasFreeTrial?: boolean;
  verified?: boolean;
  page?: number;
  pageSize?: number;
}

// ============================================================================
// API Response Types
// ============================================================================

export interface PaginatedResponse<T> {
  data: T[];
  total: number;
  page: number;
  pageSize: number;
  totalPages: number;
}

export interface ApiResponse<T> {
  success: boolean;
  data: T;
  error?: {
    message: string;
    code: string;
  };
}

// ============================================================================
// Booking Types
// ============================================================================

export interface ConsultationBooking {
  id: string;
  userId: string;
  advisoryId: string;
  serviceTypeId: string;
  scheduledAt: string;
  durationMinutes: number;
  status: 'pending' | 'confirmed' | 'completed' | 'cancelled';
  meetingLink?: string;
  notes?: string;
  priceUsd: number;
  createdAt: string;
}

// ============================================================================
// Seller Types
// ============================================================================

export interface SellerProduct {
  id: string;
  name: string;
  type: ProductCategory;
  status: ProductStatus;
  price: number;
  subscribers: number;
  rating: number;
  reviews: number;
  revenue: number;
  views: number;
  thumbnailUrl?: string;
  createdAt: string;
  updatedAt: string;
}

export interface SellerProductFilters {
  status?: ProductStatus;
  type?: ProductCategory;
  search?: string;
  page?: number;
  pageSize?: number;
  sortBy?: 'newest' | 'revenue' | 'subscribers' | 'rating';
}

export interface SellerStats {
  totalRevenue: number;
  revenueChange: number;
  totalSubscribers: number;
  subscribersChange: number;
  activeProducts: number;
  averageRating: number;
  pendingPayouts: number;
  nextPayoutDate: string;
}

export interface SellerSalesData {
  date: string;
  revenue: number;
  subscribers: number;
  newOrders: number;
}

export interface SellerPayoutInfo {
  pendingAmount: number;
  nextPayoutDate: string;
  lastPayoutDate?: string;
  lastPayoutAmount?: number;
  payoutMethod: 'bank_transfer' | 'paypal' | 'stripe';
  accountInfo: {
    type: string;
    lastFour?: string;
    email?: string;
  };
}

export interface CreateProductData {
  type: ProductCategory;
  name: string;
  description: string;
  shortDescription: string;
  tags: string[];
  thumbnailUrl?: string;
  pricing: CreateProductPricing[];
  hasFreeTrial: boolean;
  freeTrialDays?: number;
  // Signal-specific
  riskLevel?: 'low' | 'medium' | 'high';
  symbolsCovered?: string[];
  // Course-specific
  difficultyLevel?: 'beginner' | 'intermediate' | 'advanced';
  // Advisory-specific
  specializations?: string[];
  sessionDuration?: number;
}

export interface CreateProductPricing {
  period: SubscriptionPeriod;
  price: number;
  originalPrice?: number;
  isPopular: boolean;
}

export interface ProductDraft {
  id: string;
  data: Partial<CreateProductData>;
  lastSaved: string;
  createdAt: string;
}
