// Core accounting types for the bookkeeping system

export interface Company {
  id: string;
  name: string;
  cvr: string; // Danish CVR number
  address?: string;
  city?: string;
  postalCode?: string;
  country: string;
  vatNumber?: string;
  fiscalYearStart: number; // Month (1-12)
  currency: string;
  createdAt: string;
  updatedAt: string;
}

export interface Account {
  id: string;
  companyId: string;
  accountNumber: string; // e.g., "1000", "2100"
  name: string;
  type: AccountType;
  parentId?: string;
  isActive: boolean;
  description?: string;
  vatCode?: string;
  balance: number;
  createdAt: string;
  updatedAt: string;
}

export type AccountType =
  | 'asset' // Aktiver (1000-1999)
  | 'liability' // Passiver (2000-2999)
  | 'equity' // Egenkapital (3000-3999)
  | 'revenue' // Indtægter (4000-4999)
  | 'cogs' // Vareforbrug (5000-5999)
  | 'expense' // Driftsomkostninger (6000-6999)
  | 'personnel' // Personaleomkostninger (7000-7999)
  | 'financial' // Finansielle poster (8000-8999)
  | 'extraordinary'; // Ekstraordinære poster (9000-9999)

export interface Transaction {
  id: string;
  companyId: string;
  transactionNumber: string;
  date: string;
  description: string;
  lines: TransactionLine[];
  isReconciled: boolean;
  isVoided: boolean;
  attachments: Attachment[];
  createdAt: string;
  updatedAt: string;
  createdBy: string;
}

export interface TransactionLine {
  id: string;
  transactionId: string;
  accountId: string;
  account?: Account;
  description?: string;
  debit: number;
  credit: number;
  vatCode?: string;
  vatAmount?: number;
}

export interface Attachment {
  id: string;
  filename: string;
  mimeType: string;
  url: string;
  size: number;
  uploadedAt: string;
}

export interface BankAccount {
  id: string;
  companyId?: string;
  name: string;
  bankName: string;
  accountNumber: string;
  iban?: string;
  currency: string;
  ledgerAccountId?: string; // Linked to an Account
  isActive: boolean;
  balance?: number;
  lastSyncedAt?: string;
}

export interface BankTransaction {
  id: string;
  bankAccountId: string;
  date: string;
  valueDate: string;
  description: string;
  amount: number;
  balance: number;
  reference?: string;
  counterparty?: string;
  isReconciled: boolean;
  matchedTransactionId?: string;
  importedAt: string;
}

export interface ReconciliationMatch {
  bankTransactionId: string;
  ledgerTransactionId: string;
  matchType: 'auto' | 'manual';
  confidence?: number; // For auto-matches (0-1)
  matchedAt: string;
  matchedBy: string;
}

export interface VATReport {
  companyId: string;
  period: DateRange;
  boxes: VATBox[];
  totalVATDue: number;
  totalVATDeductible: number;
  netVAT: number;
  status: 'draft' | 'submitted' | 'accepted' | 'rejected';
  submittedAt?: string;
}

export interface VATBox {
  boxNumber: number;
  name: string;
  nameDanish: string;
  amount: number;
  basis?: number;
}

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

export interface Employee {
  id: string;
  companyId: string;
  name: string;
  cpr?: string; // Danish CPR (masked)
  employeeNumber: string;
  department?: string;
  position?: string;
  startDate: string;
  endDate?: string;
  isActive: boolean;
}

export interface PayrollEntry {
  id: string;
  employeeId: string;
  employee?: Employee;
  period: string; // YYYY-MM
  grossSalary: number;
  amBidrag: number; // AM-bidrag (8%)
  aSkat: number; // A-skat
  atp: number; // ATP contribution
  pension: number;
  netSalary: number;
  otherDeductions: number;
  status: 'draft' | 'approved' | 'paid';
}

// Utility types
export interface PaginatedResponse<T> {
  items: T[];
  total: number;
  pageInfo: PageInfo;
}

export interface PageInfo {
  hasNextPage: boolean;
  hasPreviousPage: boolean;
  startCursor?: string;
  endCursor?: string;
}

export interface SortConfig {
  field: string;
  direction: 'asc' | 'desc';
}

export interface FilterConfig {
  field: string;
  operator: 'eq' | 'ne' | 'gt' | 'gte' | 'lt' | 'lte' | 'contains' | 'in';
  value: unknown;
}
