export interface Company {
  symbol: string;
  name: string;
  price: number;
  change: number;
  changePercent: number;
  marketCap: number;
  volume?: number;
  volumeLabel?: string;
  pe?: number;
  eps?: number;
  high52Week?: number;
  low52Week?: number;
  profile?: CompanyProfile;
  priceChart?: CompanyPricePoint[];
  priceChartRanges?: Partial<Record<CompanyPriceChartRange, CompanyPricePoint[]>>;
}

export type CompanyPriceChartRange =
  | '1D'
  | '5D'
  | '1M'
  | '6M'
  | 'YTD'
  | '1Y'
  | '5Y'
  | 'MAX';

export interface CompanyProfile {
  description?: string;
  wikiUrl?: string;
  ceo?: string;
  headquarters?: string;
  employees?: number;
  founded?: number;
  sector?: string;
  website?: string;
}

export interface CompanyPricePoint {
  label: string;
  price: number;
  volume?: number;
  timestamp?: string;
}

export type NoteType =
  | 'fact'
  | 'quote'
  | 'management_signal'
  | 'claim'
  | 'thesis'
  | 'sub_thesis'
  | 'risk'
  | 'catalyst'
  | 'valuation_point'
  | 'scenario_assumption'
  | 'industry_observation'
  | 'competitor_comparison'
  | 'question'
  | 'contradiction'
  | 'uncertainty'
  | 'follow_up_task'
  | 'source_reference'
  | 'event_takeaway'
  | 'channel_check'
  | 'mosaic_insight';

export type EvidenceStatus =
  | 'unsourced'
  | 'source_linked'
  | 'quoted'
  | 'corroborated'
  | 'inferred'
  | 'contradicted'
  | 'stale'
  | 'superseded';

export interface HoldingResearchNote {
  id: string;
  title: string;
  noteType: NoteType;
  evidenceStatus: EvidenceStatus;
  updatedAt: string;
}

export interface Holding {
  symbol: string;
  name: string;
  quantity: number;
  avgCost: number;
  currentPrice: number;
  currentValue: number;
  gainLoss: number;
  gainLossPercent: number;
  costBasis?: number;
  unrealizedGain?: number;
  latestTradeAt?: string;
  recentNotes?: HoldingResearchNote[];
  notesCount?: number;
}

export interface Portfolio {
  holdings: Holding[];
  totalValue: number;
  dayChange: number;
  dayChangePercent: number;
  totalGain: number;
  totalGainPercent: number;
  cashBalance?: number;
  investedCostBasis?: number;
  realizedGain?: number;
  unrealizedGain?: number;
  holdingsCount?: number;
  stalePricingSymbols?: string[];
}

export interface StockAnalysis {
  symbol: string;
  summary: string;
  sentiment: 'bullish' | 'bearish' | 'neutral';
  keyPoints: string[];
  risks: string[];
  opportunities: string[];
  recommendation: 'buy' | 'sell' | 'hold';
  targetPrice?: number;
}

export interface MockFinancialData {
  companies: Company[];
  portfolio: Portfolio;
  analyses: Record<string, StockAnalysis>;
}

export type Frequency = 'annual' | 'quarterly';

export interface FilingRef {
  accessionNumber: string;
  filingDate: string;
  reportDate?: string;
  form: string;
  primaryDocument?: string;
}

export interface FilingListItem {
  accessionNumber: string;
  filingDate: string;
  reportDate?: string;
  form: string;
  primaryDocument: string;
  filingUrl: string;
  overview: string;
}

export interface SourceStatus {
  companyfactsUsed: boolean;
  degradedReason?: string;
}

export interface StatementPeriod {
  label: string;
  fiscalYear?: string;
  fiscalPeriod?: string;
  periodStart?: string;
  periodEnd: string;
  filedDate: string;
  form: string;
  revenue?: number;
  grossProfit?: number;
  operatingIncome?: number;
  netIncome?: number;
  dilutedEps?: number;
  cashAndEquivalents?: number;
  shortTermInvestments?: number;
  accountsReceivableNet?: number;
  inventory?: number;
  otherCurrentAssets?: number;
  totalCurrentAssets?: number;
  propertyPlantAndEquipmentNet?: number;
  goodwill?: number;
  intangibleAssets?: number;
  otherNonCurrentAssets?: number;
  totalAssets?: number;
  accountsPayable?: number;
  accruedExpensesAndOtherCurrentLiabilities?: number;
  shortTermDebt?: number;
  totalCurrentLiabilities?: number;
  longTermDebt?: number;
  otherNonCurrentLiabilities?: number;
  totalLiabilities?: number;
  retainedEarnings?: number;
  accumulatedOtherComprehensiveIncomeLoss?: number;
  totalEquity?: number;
  sharesOutstanding?: number;
  bookValuePerShare?: number;
}

export interface CashFlowPeriod {
  label: string;
  fiscalYear?: string;
  fiscalPeriod?: string;
  periodStart?: string;
  periodEnd: string;
  filedDate: string;
  form: string;
  // Operating Activities
  netIncome?: number;
  depreciationAndAmortization?: number;
  stockBasedCompensation?: number;
  deferredIncomeTaxes?: number;
  impairmentCharges?: number;
  lossGainOnSaleOfAssets?: number;
  otherNonCashItems?: number;
  accountsReceivable?: number;
  inventory?: number;
  prepaidExpensesAndOtherCurrentAssets?: number;
  accountsPayable?: number;
  accruedExpenses?: number;
  deferredRevenue?: number;
  otherWorkingCapitalChanges?: number;
  changeInOperatingAssets?: number;
  changeInOperatingLiabilities?: number;
  otherOperatingAdjustments?: number;
  operatingCashFlow?: number;
  // Investing Activities
  capex?: number;
  proceedsFromSaleOfPpe?: number;
  purchasesOfInvestments?: number;
  salesOfInvestments?: number;
  acquisitions?: number;
  otherInvestingActivities?: number;
  investingCashFlow?: number;
  // Financing Activities
  debtIssuance?: number;
  debtRepayment?: number;
  equityIssuance?: number;
  stockRepurchases?: number;
  dividendsPaid?: number;
  financeLeaseObligations?: number;
  otherFinancingActivities?: number;
  financingCashFlow?: number;
  // Summary
  freeCashFlow?: number;
  exchangeRateEffects?: number;
  netIncreaseDecreaseCash?: number;
  beginningCash?: number;
  endingCash?: number;
  cashPaidInterest?: number;
  cashPaidIncomeTaxes?: number;
}

export interface DividendEvent {
  endDate: string;
  filedDate: string;
  form: string;
  frequencyGuess: string;
  dividendPerShare?: number;
  totalCashDividends?: number;
}

export interface EarningsPeriod {
  label: string;
  displayLabel: string;
  fiscalYear?: string;
  fiscalPeriod?: string;
  periodStart?: string;
  periodEnd: string;
  filedDate: string;
  form: string;
  totalRevenues?: number;
  totalRevenuesYoyChangePercent?: number;
  costOfSales?: number;
  grossProfit?: number;
  grossProfitMargin?: number;
  sellingGeneralAndAdministrativeExpenses?: number;
  researchAndDevelopmentExpenses?: number;
  otherOperatingExpenses?: number;
  operatingProfit?: number;
  operatingMargin?: number;
  nonOperatingIncome?: number;
  totalNonOperatingIncome?: number;
  incomeBeforeProvisionForIncomeTaxes?: number;
  provisionForIncomeTaxes?: number;
  consolidatedNetIncome?: number;
  netIncomeAttributableToCommonShareholders?: number;
  totalSharesOutstanding?: number;
  basicWeightedAverageSharesOutstanding?: number;
  dilutedWeightedAverageSharesOutstanding?: number;
  ebitda?: number;
  effectiveTaxRate?: number;
  revenue?: number;
  netIncome?: number;
  basicEps?: number;
  dilutedEps?: number;
  dilutedWeightedAverageShares?: number;
  revenueYoyChangePercent?: number;
  dilutedEpsYoyChangePercent?: number;
}

export interface EarningsPeriodOption {
  id: string;
  label: string;
  frequency: Frequency;
  fiscalYear?: string;
  fiscalPeriod?: string;
  periodEnd: string;
  sortKey: string;
}

/**
 * Represents an XBRL concept available in a filing for custom mapping.
 */
export interface AvailableXbrlConcept {
  /** The taxonomy namespace (e.g., "us-gaap", "ifrs-full") */
  taxonomy: string;
  /** The concept name within the taxonomy */
  concept: string;
  /** The unit family for this concept */
  unitFamily: string;
  /** A human-readable label for the concept (if available) */
  label?: string;
  /** The period this concept is available for (by period ID) */
  periodId: string;
  /** The numeric value of this concept fact, if available */
  value?: number;
}

export interface FinancialsPanelData {
  symbol: string;
  companyName: string;
  cik: string;
  frequency: Frequency;
  periods: StatementPeriod[];
  latestFiling?: FilingRef;
  sourceStatus: SourceStatus;
  availableConcepts: AvailableXbrlConcept[];
  mappingWarnings?: string[];
}

export interface BalanceSheetPanelData {
  symbol: string;
  companyName: string;
  cik: string;
  frequency: Frequency;
  periods: StatementPeriod[];
  latestFiling?: FilingRef;
  sourceStatus: SourceStatus;
  availableConcepts: AvailableXbrlConcept[];
  selectedCurrency?: string;
  availableCurrencies: string[];
  mappingWarnings?: string[];
  availablePeriods: EarningsPeriodOption[];
  selectedPeriodStart?: string;
  selectedPeriodEnd?: string;
}

export interface CashFlowPanelData {
  symbol: string;
  companyName: string;
  cik: string;
  frequency: Frequency;
  periods: CashFlowPeriod[];
  latestFiling?: FilingRef;
  sourceStatus: SourceStatus;
  availablePeriods: EarningsPeriodOption[];
  selectedPeriodStart?: string;
  selectedPeriodEnd?: string;
}

export interface DividendsPanelData {
  symbol: string;
  companyName: string;
  cik: string;
  ttmDividendsPerShare?: number;
  ttmCommonDividendsPaid?: number;
  latestEvent?: DividendEvent;
  events: DividendEvent[];
  latestFiling?: FilingRef;
  sourceStatus: SourceStatus;
}

export interface EarningsPanelData {
  symbol: string;
  companyName: string;
  cik: string;
  frequency: Frequency;
  periods: EarningsPeriod[];
  availablePeriods: EarningsPeriodOption[];
  selectedPeriodStart?: string;
  selectedPeriodEnd?: string;
  selectedCurrency?: string;
  availableCurrencies: string[];
  latestFiling?: FilingRef;
  sourceStatus: SourceStatus;
  /** Available XBRL concepts from the filing for custom mapping */
  availableConcepts: AvailableXbrlConcept[];
  /** Warnings from formula mapping evaluation (e.g. concept not found) */
  mappingWarnings?: string[];
}

export interface FilingsPanelData {
  symbol: string;
  companyName: string;
  cik: string;
  items: FilingListItem[];
  page: number;
  pageSize: number;
  totalResults: number;
  totalPages: number;
  query?: string;
  latestFiling?: FilingListItem;
}
