// Territory Manager System Types
export interface Territory {
  id: string;
  name: string;
  boundary?: GeoJSON.Polygon;
  population?: number;
  market_size?: string;
  assigned_to?: string;
  is_active: boolean;
  created_at: string;
  updated_at: string;
}

export interface TerritoryManager {
  id: string;
  user_id: string;
  territory_id?: string;
  referral_code: string;
  status: 'pending' | 'active' | 'suspended' | 'inactive';
  application_date: string;
  approved_date?: string;
  approved_by?: string;
  profile?: TerritoryManagerProfile;
  documents?: Documents;
  earnings_data?: EarningsData;
  created_at: string;
  updated_at: string;
}

export interface TerritoryManagerProfile {
  full_name: string;
  phone?: string;
  address?: Address;
  has_transportation: boolean;
  has_event_experience: boolean;
  motivation?: string;
  availability?: Availability;
  skills?: string[];
  bio?: string;
}

export interface Address {
  street: string;
  city: string;
  state: string;
  zip_code: string;
  country: string;
}

export interface Availability {
  days_of_week: string[];
  time_ranges: TimeRange[];
  travel_radius: number;
}

export interface TimeRange {
  start: string;
  end: string;
}

export interface Documents {
  id_upload?: FileUpload;
  w9_form?: FileUpload;
  background_check?: FileUpload;
  resume?: FileUpload;
}

export interface FileUpload {
  filename: string;
  url: string;
  uploaded_at: string;
  size: number;
  type: string;
}

export interface EarningsData {
  total_commission: number;
  current_month_commission: number;
  last_payout_date?: string;
  total_events_referred: number;
  success_rate: number;
  average_commission_per_event: number;
}

export interface TerritoryApplication {
  id: string;
  email: string;
  full_name: string;
  phone?: string;
  address?: Address;
  desired_territory: string;
  has_transportation: boolean;
  has_event_experience: boolean;
  motivation?: string;
  documents?: Documents;
  consent?: ApplicationConsent;
  status: 'pending' | 'approved' | 'rejected';
  reviewed_by?: string;
  reviewed_at?: string;
  review_notes?: string;
  created_at: string;
  updated_at: string;
}

export interface ApplicationConsent {
  background_check: boolean;
  data_processing: boolean;
  terms_of_service: boolean;
  privacy_policy: boolean;
}

export interface Lead {
  id: string;
  territory_manager_id: string;
  event_name: string;
  organizer_contact: ContactInfo;
  event_details: EventDetails;
  status: 'cold' | 'contacted' | 'confirmed' | 'converted' | 'dead';
  notes: string[];
  follow_up_date?: string;
  created_at: string;
  updated_at: string;
}

export interface ContactInfo {
  name: string;
  email: string;
  phone?: string;
  organization?: string;
  title?: string;
}

export interface EventDetails {
  event_type: string;
  event_date?: string;
  venue?: string;
  expected_attendance?: number;
  budget_range?: string;
  description?: string;
}

export interface Commission {
  id: string;
  territory_manager_id: string;
  event_id: string;
  tickets_sold: number;
  commission_per_ticket: number;
  total_commission: number;
  status: 'unpaid' | 'paid' | 'pending';
  payout_date?: string;
  stripe_transfer_id?: string;
  created_at: string;
  updated_at: string;
}

export interface TrainingProgress {
  id: string;
  territory_manager_id: string;
  module_id: string;
  completed_at?: string;
  score?: number;
  created_at: string;
}

export interface Achievement {
  id: string;
  name: string;
  description: string;
  requirements: AchievementRequirement;
  reward_amount: number;
  badge_icon: string;
  is_active: boolean;
  created_at: string;
}

export interface AchievementRequirement {
  referrals?: number;
  monthly_referrals?: number;
  success_rate?: number;
  months?: number;
  training_modules?: string;
  recurring_events?: number;
  quarterly_commission?: number;
}

export interface TerritoryManagerAchievement {
  id: string;
  territory_manager_id: string;
  achievement_id: string;
  earned_at: string;
}

export interface ExpenseReport {
  id: string;
  territory_manager_id: string;
  event_id?: string;
  mileage: number;
  receipts?: FileUpload[];
  total_amount: number;
  status: 'pending' | 'approved' | 'paid' | 'rejected';
  submitted_date: string;
  approved_date?: string;
  approved_by?: string;
  created_at: string;
  updated_at: string;
}

export interface TMNotification {
  id: string;
  territory_manager_id: string;
  type: string;
  title: string;
  message?: string;
  data?: any;
  read: boolean;
  created_at: string;
}

// API Response Types
export interface TerritoryManagerStats {
  total_commission: number;
  current_month_commission: number;
  total_events_referred: number;
  success_rate: number;
  rank: number;
  achievements_earned: number;
  pending_payouts: number;
}

export interface LeaderboardEntry {
  rank: number;
  territory_manager_id: string;
  name: string;
  territory_name: string;
  total_commission: number;
  events_referred: number;
  success_rate: number;
}

export interface MarketingMaterial {
  id: string;
  name: string;
  type: 'flyer' | 'email_template' | 'social_media' | 'video' | 'document';
  category: string;
  file_url?: string;
  content?: string;
  customizable: boolean;
  created_at: string;
}

export interface ReferralLink {
  id: string;
  territory_manager_id: string;
  url: string;
  qr_code_url: string;
  event_type?: string;
  campaign_name?: string;
  clicks: number;
  conversions: number;
  created_at: string;
}

// Form Types
export interface ApplicationFormData {
  personalInfo: {
    fullName: string;
    email: string;
    phone: string;
    address: Address;
  };
  preferences: {
    desiredTerritory: string;
    hasTransportation: boolean;
    hasEventExperience: boolean;
    motivation: string;
    availability: Availability;
  };
  documents: {
    idUpload?: File;
    resume?: File;
  };
  consent: ApplicationConsent;
}

export interface LeadFormData {
  eventName: string;
  organizerContact: ContactInfo;
  eventDetails: EventDetails;
  notes?: string;
  followUpDate?: string;
}

export interface ExpenseReportFormData {
  eventId?: string;
  mileage: number;
  receipts: File[];
  description?: string;
}

// API Function Types
export interface TerritoryManagerAPI {
  // Applications
  submitApplication: (data: ApplicationFormData) => Promise<TerritoryApplication>;
  getApplications: (status?: string) => Promise<TerritoryApplication[]>;
  reviewApplication: (id: string, action: 'approve' | 'reject', notes?: string) => Promise<void>;
  
  // Territory Managers
  getTerritoryManager: (id: string) => Promise<TerritoryManager>;
  updateTerritoryManager: (id: string, data: Partial<TerritoryManager>) => Promise<TerritoryManager>;
  getTerritoryManagerStats: (id: string) => Promise<TerritoryManagerStats>;
  
  // Leads
  createLead: (data: LeadFormData) => Promise<Lead>;
  getLeads: (territoryManagerId: string) => Promise<Lead[]>;
  updateLead: (id: string, data: Partial<Lead>) => Promise<Lead>;
  deleteLead: (id: string) => Promise<void>;
  
  // Commissions
  getCommissions: (territoryManagerId: string) => Promise<Commission[]>;
  calculateCommission: (eventId: string) => Promise<Commission>;
  
  // Training
  getTrainingProgress: (territoryManagerId: string) => Promise<TrainingProgress[]>;
  updateTrainingProgress: (territoryManagerId: string, moduleId: string, score?: number) => Promise<void>;
  
  // Achievements
  getAchievements: () => Promise<Achievement[]>;
  getTerritoryManagerAchievements: (territoryManagerId: string) => Promise<TerritoryManagerAchievement[]>;
  
  // Expense Reports
  submitExpenseReport: (data: ExpenseReportFormData) => Promise<ExpenseReport>;
  getExpenseReports: (territoryManagerId: string) => Promise<ExpenseReport[]>;
  
  // Notifications
  getNotifications: (territoryManagerId: string) => Promise<TMNotification[]>;
  markNotificationAsRead: (id: string) => Promise<void>;
  
  // Referrals
  generateReferralLink: (territoryManagerId: string, eventType?: string) => Promise<ReferralLink>;
  getReferralLinks: (territoryManagerId: string) => Promise<ReferralLink[]>;
  
  // Leaderboard
  getLeaderboard: (period?: 'monthly' | 'quarterly' | 'yearly') => Promise<LeaderboardEntry[]>;
  
  // Marketing Materials
  getMarketingMaterials: () => Promise<MarketingMaterial[]>;
  
  // Territories
  getTerritories: () => Promise<Territory[]>;
  assignTerritory: (territoryId: string, territoryManagerId: string) => Promise<void>;
}