export interface User {
  id: number
  name: string
  email: string
  phone: string | null
  company: string | null
  admin_notes?: string | null
  status: string
  two_factor_enabled?: boolean
}

export interface UserProfile {
  billing_address_line1: string | null
  billing_address_line2: string | null
  billing_city: string | null
  billing_state: string | null
  billing_zip: string | null
  billing_country: string | null
  tax_id: string | null
  tax_exempt: boolean
  company_name: string | null
  company_vat: string | null
}

export interface AuthProps {
  user: User | null
}

export interface DomainProps {
  marketing: string
  account: string
  admin: string
}

export interface FlashProps {
  success?: string
  error?: string
  info?: string
  new_password?: string
}

export interface SharedPageProps {
  auth: AuthProps
  flash: FlashProps
  domains: DomainProps
}

export interface PlanPrice {
  id: number
  plan_id: number
  billing_cycle: 'monthly' | 'quarterly' | 'semi_annual' | 'annual'
  price: string
  stripe_price_id: string | null
}

export interface Plan {
  id: number
  name: string
  slug: string
  description: string | null
  price: string
  billing_cycle: string
  service_type: string
  features: Record<string, string> | null
  stock_quantity: number | null
  status: string
  sort_order: number
  prices?: PlanPrice[]
  provisioning_config?: { package_id?: number | null; hypervisor_id?: number | null } | null
}

export interface Subscription {
  id: number
  type: string
  stripe_status: string
  gateway: string
  billing_cycle: 'monthly' | 'quarterly' | 'semi_annual' | 'annual' | null
  current_period_start: string | null
  current_period_end: string | null
  ends_at: string | null
  created_at: string
  plan?: Plan
  plan_name?: string | null
  plan_price?: string | null
  plan_billing_cycle?: string | null
  plan_features?: Record<string, string> | null
}

export interface PaymentMethod {
  id: string
  brand: string
  last_four: string
  exp_month: number
  exp_year: number
  is_default: boolean
}

export interface Invoice {
  id: number
  number: string
  status: string
  total: string
  tax: string
  currency: string
  gateway: string | null
  gateway_invoice_id: string | null
  invoice_pdf: string | null
  notes: string | null
  due_date: string | null
  paid_at: string | null
  created_at: string
  updated_at: string
  user?: {
    id: number
    name: string
    email: string
    status?: string
  }
  items?: InvoiceLineItem[]
}

export interface InvoiceLineItem {
  id: number
  invoice_id: number
  description: string
  amount: string
  quantity: number
  created_at: string
  updated_at: string
}

export interface Transaction {
  id: number
  gateway: string
  payment_method: string
  status: string
  description: string
  amount: string
  created_at: string
}

export interface PaginatedResponse<T> {
  data: T[]
  links: PaginationLink[]
  from: number
  to: number
  total: number
  last_page: number
}

export interface PaginationLink {
  url: string | null
  label: string
  active: boolean
}

export interface Service {
  id: number
  user_id: number
  subscription_id: number | null
  plan_id: number
  service_type: string
  platform: string
  platform_service_id: string | null
  status: 'pending' | 'active' | 'suspended' | 'terminated'
  ipv4_address: string | null
  ipv6_address: string | null
  hostname: string | null
  domain: string | null
  auto_renew: boolean
  provisioning_info: Record<string, unknown> | null
  provisioned_at: string | null
  suspended_at: string | null
  terminated_at: string | null
  created_at: string
  updated_at: string
  plan?: Plan
  subscription?: Subscription
}

export interface Coupon {
  id: number
  code: string
  type: 'percentage' | 'fixed'
  value: string
  currency: string | null
  applies_to: number[] | null
  max_uses: number | null
  times_used: number
  active: boolean
  expires_at: string | null
  created_at: string
  redemptions_count?: number
}

export interface CouponWithStats extends Coupon {
  redemptions_count: number
  redemptions_sum_discount_amount: string | null
  redemptions_max_created_at: string | null
}

export interface CouponRedemption {
  id: number
  coupon_id: number
  user_id: number
  subscription_id: number | null
  discount_amount: string
  created_at: string
  user?: {
    id: number
    name: string
    email: string
  }
  subscription?: {
    id: number
    type: string
    stripe_status: string
  }
}

export interface CouponRedemptionStats {
  total_redemptions: number
  total_discount: number
  latest_redemption: string | null
}

export interface Department {
  id: number
  name: string
  slug: string
  email: string | null
  auto_assign_to: number | null
  sla_policy_id: number | null
  sort_order: number
  sla_policy?: SlaPolicy
  auto_assignee?: { id: number; name: string; email: string }
}

export interface SlaPolicy {
  id: number
  name: string
  priority: string
  first_response_hours: number
  resolution_hours: number
  business_hours_only: boolean
}

export interface SlaBusinessHoursEntry {
  id?: number
  day_of_week: number
  start_time: string
  end_time: string
  is_holiday: boolean
}

export interface CannedResponseItem {
  id: number
  title: string
  content: string
  category: string | null
  is_shared: boolean
  created_by: number
  usage_count: number
  created_at: string
  updated_at: string
  creator?: { id: number; name: string }
}

export interface TicketTag {
  id: number
  name: string
  color: string
}

export interface TicketCustomField {
  id: number
  name: string
  type: 'text' | 'select' | 'checkbox' | 'number'
  options: string[] | null
  is_required: boolean
  department_id: number | null
  sort_order: number
  department?: { id: number; name: string }
}

export interface TicketSatisfactionRating {
  id: number
  ticket_id: number
  user_id: number
  rating: number
  comment: string | null
  created_at: string
}

export interface SupportTicket {
  id: number
  user_id: number
  ticket_reference: string | null
  customer_email: string | null
  subject: string
  status: 'open' | 'in_progress' | 'waiting' | 'closed'
  priority: 'low' | 'medium' | 'high' | 'urgent'
  department: 'billing' | 'technical' | 'sales' | 'general'
  department_id: number | null
  assigned_to: number | null
  sla_policy_id: number | null
  first_response_due_at: string | null
  resolution_due_at: string | null
  first_responded_at: string | null
  resolved_at: string | null
  sla_first_response_breached: boolean
  sla_resolution_breached: boolean
  merged_into_ticket_id: number | null
  last_reply_at: string | null
  created_at: string
  updated_at: string
  user?: {
    id: number
    name: string
    email: string
    status?: string
    company?: string | null
  }
  replies?: TicketReply[]
  assignee?: { id: number; name: string; email: string }
  tags?: TicketTag[]
  department_relation?: Department
  sla_policy?: SlaPolicy
  satisfaction_rating?: TicketSatisfactionRating
}

export interface TicketReply {
  id: number
  ticket_id: number
  user_id: number
  body: string
  is_staff_reply: boolean
  is_internal: boolean
  message_id: string | null
  from_email: string | null
  via_email: boolean
  created_at: string
  updated_at: string
  user?: {
    id: number
    name: string
    email: string
  }
}

export interface AuditLog {
  id: number
  user_id: number | null
  admin_id: number | null
  action: string
  resource_type: string | null
  resource_id: number | null
  ip_address: string | null
  user_agent: string | null
  changes: Record<string, unknown> | null
  created_at: string
  user?: {
    id: number
    name: string
    email: string
  }
  admin?: {
    id: number
    name: string
    email: string
  }
}

export interface UpcomingRenewal {
  id: number
  plan_name: string
  plan_price: string | null
  billing_cycle: string | null
  renewal_date: string
  status: string
  auto_renew: boolean
  service_id: number | null
  days_until_renewal: number
}

export interface TaxRate {
  id: number
  name: string
  country_code: string
  region_code: string | null
  rate: string
  type: 'inclusive' | 'exclusive'
  priority: number
  is_active: boolean
  created_at: string
  updated_at: string
}

export interface LoginHistoryEntry {
  id: number
  ip_address: string
  device_type: string | null
  browser: string | null
  os: string | null
  location_country: string | null
  location_city: string | null
  success: boolean
  two_factor_used: boolean
  is_new_device: boolean
  created_at: string
}

export interface TrustedDevice {
  id: number
  user_id: number
  device_hash: string
  device_name: string | null
  ip_address: string
  last_used_at: string | null
  expires_at: string
  created_at: string
}

export interface WinbackCampaign {
  id: number
  name: string
  cancellation_reason: string | null
  email_sequence: Array<{ delay_days: number; subject: string; body: string }>
  offer_type: 'discount' | 'credit' | 'free_upgrade' | 'none'
  offer_value: number | null
  offer_duration_days: number | null
  coupon_code: string | null
  status: 'active' | 'paused' | 'archived'
  recipients_count?: number
  reactivated_count?: number
  reactivation_rate?: number
  created_at: string
  updated_at: string
}

export interface WinbackRecipient {
  id: number
  campaign_id: number
  user_id: number
  current_email_index: number
  last_email_sent_at: string | null
  reactivated: boolean
  reactivated_at: string | null
  unsubscribed_at: string | null
  user?: { id: number; name: string; email: string }
  created_at: string
}

export interface CancellationSurvey {
  id: number
  user_id: number
  subscription_id: number
  cancellation_reason: string
  cancellation_feedback: string | null
  would_return: 'yes' | 'maybe' | 'no' | null
  user?: { id: number; name: string; email: string }
  subscription?: { id: number; type: string }
  created_at: string
}

export type ReportType = 'revenue' | 'profit_loss' | 'tax' | 'aging' | 'refund' | 'subscription'

export interface ReportMeta {
  title: string
  start_date: string | null
  end_date: string | null
  generated_at: string
  service_type?: string | null
}

export interface RevenueReportData {
  total_revenue: number
  by_period: Array<{ period: string; amount: number }>
  by_service_type: Array<{ type: string; amount: number }>
  by_gateway: Array<{ gateway: string; amount: number }>
  by_plan: Array<{ plan: string; amount: number }>
}

export interface ProfitLossReportData {
  revenue: number
  refunds: number
  gateway_fees: number
  infrastructure_cost: number
  net_profit: number
  margin_percentage: number
}

export interface TaxReportData {
  total_tax: number
  by_country: Array<{ country: string; amount: number; invoice_count: number }>
  by_region: Array<{ region: string; country: string; amount: number }>
}

export interface AgingReportData {
  current: { count: number; amount: number }
  days_31_60: { count: number; amount: number }
  days_61_90: { count: number; amount: number }
  days_90_plus: { count: number; amount: number }
  total: { count: number; amount: number }
  invoices: Array<{ id: number; invoice_number: string; customer: string; amount: number; due_date: string; days_overdue: number }>
}

export interface RefundReportData {
  total_refunds: number
  refund_count: number
  by_gateway: Array<{ gateway: string; amount: number; count: number }>
  by_month: Array<{ month: string; amount: number; count: number }>
}

export interface SubscriptionReportData {
  new_subscriptions: number
  cancelled_subscriptions: number
  churn_rate: number
  mrr_start: number
  mrr_end: number
  mrr_change: number
  by_plan: Array<{ plan: string; new: number; cancelled: number; active: number }>
}

export interface KnowledgeBaseCategory {
  id: number
  parent_id: number | null
  name: string
  slug: string
  description: string | null
  icon: string | null
  sort_order: number
  is_visible: boolean
  articles_count?: number
  parent?: KnowledgeBaseCategory
  children?: KnowledgeBaseCategory[]
  created_at: string
  updated_at: string
}

export interface KnowledgeBaseArticle {
  id: number
  category_id: number
  author_id: number
  title: string
  slug: string
  content: string
  excerpt: string | null
  status: 'draft' | 'published' | 'archived'
  is_featured: boolean
  view_count: number
  helpful_count: number
  not_helpful_count: number
  helpful_percentage?: number
  published_at: string | null
  created_at: string
  updated_at: string
  category?: { id: number; name: string; slug?: string }
  author?: { id: number; name: string }
  revisions?: ArticleRevision[]
}

export interface ArticleRevision {
  id: number
  article_id: number
  content: string
  edited_by: number
  editor?: { id: number; name: string }
  created_at: string
  updated_at: string
}

export type StatusColor = 'success' | 'error' | 'warning' | 'info' | 'secondary'

export interface PlanConfigGroup {
  id: number
  name: string
  description: string | null
  mode: 'preset' | 'build_your_own'
  service_type: string | null
  is_active: boolean
  sort_order: number
  options: PlanConfigOption[]
  plans?: Plan[]
}

export interface PlanConfigOption {
  id: number
  group_id: number
  name: string
  description: string | null
  type: 'dropdown' | 'radio' | 'quantity' | 'checkbox' | 'text' | 'slider'
  provisioning_key: string | null
  required: boolean
  is_active: boolean
  min_qty: number | null
  max_qty: number | null
  step: number | null
  unit_label: string | null
  hourly_price: number | null
  monthly_price: number | null
  quarterly_price: number | null
  semi_annual_price: number | null
  annual_price: number | null
  sort_order: number
  values: PlanConfigValue[]
}

export interface PlanConfigValue {
  id: number
  option_id: number
  label: string
  value: string | null
  hourly_price: number
  monthly_price: number
  quarterly_price: number
  semi_annual_price: number
  annual_price: number
  is_default: boolean
  sort_order: number
}

export interface SubscriptionConfigSelection {
  id: number
  subscription_id: number
  option_id: number
  value_id: number | null
  quantity: number | null
  text_value: string | null
  locked_price: number
  locked_hourly_price: number | null
  billing_cycle: string
  is_custom_build: boolean
  option?: PlanConfigOption
  value?: PlanConfigValue
}

export interface Affiliate {
  id: number
  user_id: number
  referral_code: string
  status: 'pending' | 'active' | 'suspended'
  commission_type: 'percentage' | 'flat'
  commission_rate: string
  recurring_commissions: boolean
  minimum_payout: string
  total_earned: string
  total_paid: string
  pending_balance: string
  approved_at: string | null
  created_at: string
  updated_at: string
  user?: { id: number; name: string; email: string }
  referrals_count?: number
  commissions_count?: number
  payouts_count?: number
}

export interface AffiliateReferral {
  id: number
  affiliate_id: number
  referred_user_id: number
  subscription_id: number | null
  status: 'pending' | 'approved' | 'rejected'
  signup_ip: string | null
  referral_url: string | null
  approved_at: string | null
  created_at: string
  updated_at: string
  referred_user?: { id: number; name: string; email: string }
}

export interface AffiliateCommission {
  id: number
  affiliate_id: number
  referral_id: number
  payment_transaction_id: number | null
  amount: string
  currency: string
  type: 'signup' | 'renewal'
  status: 'pending' | 'approved' | 'paid' | 'reversed'
  approved_at: string | null
  paid_at: string | null
  created_at: string
  updated_at: string
  affiliate?: Affiliate
  referral?: AffiliateReferral
}

export interface AffiliatePayout {
  id: number
  affiliate_id: number
  amount: string
  currency: string
  method: 'credit' | 'paypal' | 'bank_transfer'
  status: 'pending' | 'processing' | 'completed' | 'failed'
  reference: string | null
  processed_at: string | null
  created_at: string
  updated_at: string
  affiliate?: Affiliate
}

export interface AffiliateStats {
  total_earned: number
  pending_balance: number
  total_paid: number
  monthly_earnings: number
  referral_count: number
  commission_count: number
}

export interface MonthlyChartEntry {
  month: string
  total: string
}

export interface OrderRiskAssessment {
  id: number
  order_id: number | null
  user_id: number
  risk_score: number
  risk_level: 'low' | 'medium' | 'high' | 'critical'
  checks: Record<string, { score: number; details: string }>
  auto_action: 'approve' | 'hold' | 'reject'
  reviewed_by: number | null
  reviewed_at: string | null
  created_at: string
  updated_at: string
  user?: { id: number; name: string; email: string; status?: string; created_at?: string }
  order?: { id: number; order_number: string; total: string; status: string; currency?: string; created_at?: string }
  reviewer?: { id: number; name: string }
}
