export type ApiResult<T> =
  | { ok: true; data: T }
  | { ok: false; message: string };

export type OrganizationRegistration = {
  id: string;
  organization_name?: string;
  email: string;
  status: string;
  requested_at: string;
  decided_at?: string | null;
  decided_by_user_id?: string | null;
};

export type OrganizationRegistrationDetail = OrganizationRegistration & {
  organization_id?: string | null;
  schema_name?: string | null;
  provisioning_error?: string | null;
  decision_note?: string | null;
};

export type OrganizationUser = {
  user_id: string;
  email: string;
  status: string;
  roles: string[];
};

export type Customer = {
  id: string;
  customer_number: string;
  name: string;
  status: string;
  details: {
    street: string;
    postal_code: string;
    city: string;
    country: string;
    email: string;
    phone: string;
  };
  standard_discount_percent: string;
  cash_discount_term_id?: string | null;
};

export type Supplier = Omit<Customer, "customer_number"> & {
  supplier_number: string;
  payment_days?: number | null;
};

export type Item = {
  id: string;
  item_number: string;
  name: string;
  manufacturer_code?: string | null;
  unit: string;
  tax_rate: string;
  default_purchase_price?: string | null;
  default_sales_price?: string | null;
  status: string;
  supplier_prices: ItemSupplierPrice[];
};

export type ItemSupplierPrice = {
  id?: string;
  supplier_id: string;
  supplier_number?: string;
  supplier_name?: string;
  external_item_number: string;
  purchase_price: string;
  currency: string;
  is_preferred: boolean;
  valid_from?: string | null;
  valid_until?: string | null;
  source?: string;
};

export type ItemPriceHistory = {
  id: string;
  item_id: string;
  purchase_price?: string | null;
  sales_price?: string | null;
  source: string;
  valid_from: string;
  created_by_user_id?: string | null;
  created_at: string;
};

export type CashDiscountTerm = {
  id: string;
  code: string;
  name: string;
  discount_percent: string;
  discount_days: number;
  net_days?: number | null;
  valid_from?: string | null;
  valid_until?: string | null;
  is_default_customer_term: boolean;
  is_default_supplier_term: boolean;
  is_active: boolean;
};

export type Activity = {
  id: string;
  activity_number?: string | null;
  activity_type: string;
  title: string;
  body: string;
  status: string;
  priority: string;
  due_at?: string | null;
};

export type NumberRange = {
  id: string;
  code: string;
  pattern: string;
  counter_value: number;
  counter_padding: number;
  reset_rule?: string | null;
  is_active: boolean;
};

export type QuoteItem = {
  id?: string;
  line_number?: number;
  line_kind?: "item" | "activity";
  item_id?: string | null;
  activity_id?: string | null;
  description: string;
  quantity: string;
  unit_price: string;
  original_unit_price?: string | null;
  discount_percent: string;
  tax_rate: string;
  price_overridden?: boolean;
};

export type Quote = {
  id: string;
  quote_number: string;
  customer_id: string;
  status: string;
  valid_until?: string | null;
  cash_discount_term_id?: string | null;
  customer_discount_percent: string;
  notes: string;
  items: QuoteItem[];
};

export type OutgoingInvoiceItem = QuoteItem;

export type OutgoingInvoice = {
  id: string;
  invoice_number: string;
  customer_id: string;
  status: string;
  cash_discount_term_id?: string | null;
  customer_discount_percent: string;
  issued_at?: string | null;
  due_at?: string | null;
  source_quote_id?: string | null;
  finalized_at?: string | null;
  items: OutgoingInvoiceItem[];
};

export type IncomingInvoiceItem = {
  id?: string;
  line_number?: number;
  item_id?: string | null;
  description: string;
  quantity: string;
  unit_price: string;
  tax_rate: string;
};

export type IncomingInvoice = {
  id: string;
  invoice_number: string;
  supplier_id: string;
  status: string;
  cash_discount_term_id?: string | null;
  invoice_date?: string | null;
  due_at?: string | null;
  items: IncomingInvoiceItem[];
};

export type PriceListImportRow = {
  row_number: number;
  item_number: string;
  name: string;
  manufacturer_code?: string | null;
  unit: string;
  tax_rate: string;
  purchase_price?: string | null;
  sales_price?: string | null;
  supplier_number?: string | null;
  supplier_item_number?: string | null;
  currency?: string | null;
  action: string;
  error?: string | null;
};

export type PriceListImportPreview = {
  rows: PriceListImportRow[];
  total_rows: number;
  valid_rows: number;
  error_rows: number;
};

export type PriceListImportApplyResponse = {
  import_id: string;
  applied_rows: number;
  error_rows: number;
};

export type ApiConnector = {
  id: string;
  code: string;
  name: string;
  connector_type: string;
  config: Record<string, unknown>;
  is_active: boolean;
  sync_interval_minutes?: number | null;
  last_sync_at?: string | null;
};

export type PriceRule = {
  id: string;
  code: string;
  name: string;
  source_type: "import" | "api" | "supplier";
  source_id?: string | null;
  markup_percent: string;
  rounding_mode: "none" | "cent" | "five_cent" | "ten_cent" | "whole";
  is_active: boolean;
};

export type EntityLink = {
  entity_type: string;
  entity_id: string;
};

export type Communication = {
  id: string;
  communication_type: string;
  direction: string;
  subject: string;
  body: string;
  status: string;
  occurred_at?: string | null;
  links: EntityLink[];
};

export type DocumentVersion = {
  id: string;
  version_no: number;
  file_name: string;
  content_type: string;
  file_size: number;
  checksum_sha256: string;
  created_at: string;
};

export type DocumentRecord = {
  id: string;
  title: string;
  description: string;
  status: string;
  latest_version?: DocumentVersion | null;
  links: EntityLink[];
};

export type DocumentDownload = {
  document_id: string;
  version_id: string;
  file_name: string;
  content_type: string;
  content_base64: string;
};

export type DocumentAuditLogEntry = {
  id: string;
  document_id: string;
  version_id?: string | null;
  action: string;
  user_id?: string | null;
  created_at: string;
};

export type LoginResponse = {
  user_id: string;
  access_token: string;
  organization_id?: string | null;
  must_change_password: boolean;
  organizations: Array<{
    id: string;
    schema_name?: string | null;
    status: string;
  }>;
};

export type AuthSession = {
  email: string;
  userId: string;
  accessToken: string;
  organizationId?: string | null;
  mustChangePassword: boolean;
};

export type NavigationMode = "scroll" | "groups";

export type UserNavigationSettings = {
  mode: NavigationMode;
};

export type RecordSummary = {
  id: string;
  title: string;
  updated_at: string;
};

export type ServerMessage =
  | { type: "snapshot"; payload: { records: RecordSummary[] } }
  | { type: "record_changed"; payload: { record: RecordSummary } }
  | { type: "pong" }
  | { type: "error"; payload: { message: string } };

export type ClientMessage =
  | { type: "subscribe"; payload: { topic: string } }
  | { type: "ping" };

export type WireMessage =
  | { type: "hello"; payload: HelloMessage }
  | { type: "hello_ack"; payload: HelloAckMessage }
  | { type: "encrypted"; payload: EncryptedEnvelope }
  | { type: "error"; payload: { message: string } };

export type HelloMessage = {
  protocol_version: number;
  key_id: string;
  session_key: string;
};

export type HelloAckMessage = {
  protocol_version: number;
  key_id: string;
};

export type EncryptedEnvelope = {
  enc: string;
  key_id: string;
  nonce: string;
  ciphertext: string;
};

export type SessionCrypto = {
  keyId: string;
  key: CryptoKey;
  exportedKey: string;
};

export type DevBootstrapResponse = {
  organization_id: string;
  schema_name: string;
  user_id: string;
  email: string;
  password: string;
  dev_mode: boolean;
};
