export interface PagedResult<T> {
  items: T[]
  total: number
  page: number
  pageSize: number
  totalPages: number
}

export const CounterpartyType = { LegalEntity: 1, Individual: 2 } as const
export type CounterpartyType = (typeof CounterpartyType)[keyof typeof CounterpartyType]

export const BarcodeType = { Ean13: 1, Ean8: 2, Code128: 3, Code39: 4, Upca: 5, Upce: 6, Other: 99 } as const
export type BarcodeType = (typeof BarcodeType)[keyof typeof BarcodeType]

export const Packaging = { Piece: 1, Weight: 2, Liquid: 3 } as const
export type Packaging = (typeof Packaging)[keyof typeof Packaging]
export const packagingLabel: Record<Packaging, string> = {
  [Packaging.Piece]: 'Штучный',
  [Packaging.Weight]: 'Весовой',
  [Packaging.Liquid]: 'Разливной',
}

export interface Country {
  id: string; code: string; name: string
  defaultCurrencyId: string | null
  defaultCurrencyCode: string | null
  defaultCurrencySymbol: string | null
  vatRate: number
}
export interface Currency { id: string; code: string; name: string; symbol: string }
export interface UnitOfMeasure {
  id: string; code: string; name: string; organizationId: string | null;
  isActive: boolean; isEnabledForOrg: boolean
}
export interface PriceType { id: string; name: string; isRequired: boolean; isSystem: boolean; isRetail: boolean; sortOrder: number }
export interface Store {
  id: string; name: string; code: string | null; address: string | null; phone: string | null;
  managerName: string | null; isMain: boolean; isActive: boolean
}
export interface RetailPoint {
  id: string; name: string; code: string | null; storeId: string; storeName: string | null;
  address: string | null; phone: string | null; fiscalSerial: string | null; fiscalRegNumber: string | null; isActive: boolean
}
export interface ProductGroup { id: string; name: string; parentId: string | null; path: string; sortOrder: number; markupPercent: number | null; organizationId: string | null }
export interface Counterparty {
  id: string; name: string; legalName: string | null; type: CounterpartyType;
  bin: string | null; iin: string | null; taxNumber: string | null; countryId: string | null; countryName: string | null;
  address: string | null; phone: string | null; email: string | null;
  bankName: string | null; bankAccount: string | null; bik: string | null;
  contactPerson: string | null; notes: string | null
}
export interface ProductBarcode { id: string; code: string; type: BarcodeType; isPrimary: boolean }
export interface ProductPrice { id: string; priceTypeId: string; priceTypeName: string; amount: number; currencyId: string; currencyCode: string }
export interface Product {
  id: string; name: string; article: string | null; description: string | null;
  unitOfMeasureId: string; unitName: string;
  vat: number; vatEnabled: boolean;
  productGroupId: string | null; productGroupName: string | null;
  defaultSupplierId: string | null; defaultSupplierName: string | null;
  countryOfOriginId: string | null; countryOfOriginName: string | null;
  isService: boolean; packaging: Packaging; isMarked: boolean;
  minStock: number | null; maxStock: number | null;
  referencePrice: number | null; referencePriceUpdatedAt: string | null;
  purchaseCurrencyId: string | null; purchaseCurrencyCode: string | null;
  cost: number; lastSupplyAt: string | null;
  imageUrl: string | null;
  prices: ProductPrice[]; barcodes: ProductBarcode[]
}

export interface StockRow {
  productId: string; productName: string; article: string | null; unitName: string;
  storeId: string; storeName: string;
  quantity: number; reservedQuantity: number; available: number;
}

export interface MovementRow {
  id: string; occurredAt: string;
  productId: string; productName: string; article: string | null;
  storeId: string; storeName: string;
  quantity: number; unitCost: number | null;
  type: string; documentType: string; documentId: string | null; documentNumber: string | null;
  notes: string | null;
}

export const SupplyStatus = { Draft: 0, Posted: 1 } as const
export type SupplyStatus = (typeof SupplyStatus)[keyof typeof SupplyStatus]

export interface SupplyListRow {
  id: string; number: string; date: string; status: SupplyStatus;
  supplierId: string; supplierName: string;
  storeId: string; storeName: string;
  currencyId: string; currencyCode: string;
  total: number; lineCount: number; postedAt: string | null;
}

export interface SupplyLineDto {
  id: string | null; productId: string;
  productName: string | null; productArticle: string | null; productBarcode: string | null;
  unitName: string | null;
  quantity: number; unitPrice: number; lineTotal: number; sortOrder: number;
  retailPriceManuallyOverridden: boolean; retailPriceOverride: number | null;
  currentRetailPrice: number | null;
}

export interface SupplyDto {
  id: string; number: string; date: string; status: SupplyStatus;
  supplierId: string; supplierName: string;
  storeId: string; storeName: string;
  currencyId: string; currencyCode: string;
  notes: string | null;
  total: number; postedAt: string | null;
  lines: SupplyLineDto[];
}

export const RetailSaleStatus = { Draft: 0, Posted: 1 } as const
export type RetailSaleStatus = (typeof RetailSaleStatus)[keyof typeof RetailSaleStatus]

export const PaymentMethod = { Cash: 0, Card: 1, BankTransfer: 2, Bonus: 3, Mixed: 99 } as const
export type PaymentMethod = (typeof PaymentMethod)[keyof typeof PaymentMethod]

export interface RetailSaleListRow {
  id: string; number: string; date: string; status: RetailSaleStatus;
  storeId: string; storeName: string;
  retailPointId: string | null; retailPointName: string | null;
  customerId: string | null; customerName: string | null;
  currencyId: string; currencyCode: string;
  total: number; payment: PaymentMethod; lineCount: number;
  postedAt: string | null;
}

export interface RetailSaleLineDto {
  id: string | null; productId: string;
  productName: string | null; productArticle: string | null; unitName: string | null;
  quantity: number; unitPrice: number; discount: number; lineTotal: number;
  vatPercent: number; sortOrder: number;
}

export interface SalesStatsBucket {
  bucket: string
  revenue: number
  transactions: number
}

export interface SalesStatsResponse {
  revenueToday: number
  revenueThisMonth: number
  revenuePrevMonth: number
  transactionsToday: number
  transactionsThisMonth: number
  avgTicketThisMonth: number
  series: SalesStatsBucket[]
}

export interface RetailSaleDto {
  id: string; number: string; date: string; status: RetailSaleStatus;
  storeId: string; storeName: string;
  retailPointId: string | null; retailPointName: string | null;
  customerId: string | null; customerName: string | null;
  currencyId: string; currencyCode: string;
  subtotal: number; discountTotal: number; total: number;
  payment: PaymentMethod; paidCash: number; paidCard: number;
  notes: string | null; postedAt: string | null;
  lines: RetailSaleLineDto[];
}
