export interface UserRoleInfo {
  id: string;
  code: string;
  name_ar: string;
  name_en: string | null;
}

export interface UserDTO {
  id: string;
  email: string | null;
  phone: string | null;
  user_type: "admin" | "employee" | "customer";
  status: "active" | "suspended" | "blocked" | "archived" | "pending" | "draft" | "pending_kyc" | "rejected" | "closed";
  roles: UserRoleInfo[];
  permissions: string[];
  must_change_password: boolean;
  is_admin_approved: boolean;
}

export interface PermissionDTO {
  id: string;
  code: string;
  module: string;
  name_ar: string;
  name_en: string;
}

export interface RoleDTO {
  id: string;
  code: string;
  name_ar: string;
  name_en: string | null;
  description: string | null;
  is_system: boolean;
  permissions: PermissionDTO[];
  user_count?: number;
  is_privileged?: boolean;
}

export interface CustomerDTO {
  id: string;
  user_id: string;
  customer_code: string | null;
  name: string | null;
  email: string | null;
  phone: string | null;
  identity_type: string;
  identity_number: string;
  identity_country_id: string;
  identity_verified_at: string | null;
  identity_expiry_date: string | null;
  kyc_level: string;
  status: "draft" | "pending_kyc" | "active" | "suspended" | "rejected" | "closed" | "blocked" | "archived" | "pending";
  registered_at_branch_id: string | null;
  primary_branch_id: string | null;
  receivable_account_id: string | null;
  deactivated_at: string | null;
  tier: string | null;
  two_factor_enabled: boolean | null;
  linked_devices: number | null;
  last_login_at: string | null;
  last_login_ip: string | null;
  risk_score: number | null;
  cash_wallets: CashWalletDTO[];
  gold_wallets: GoldWalletDTO[];
  created_at: string;
  updated_at: string;
}

export interface KycApplicationDTO {
  id: string;
  user_id: string;
  status: "submitted" | "pending" | "under_review" | "approved" | "rejected" | "expired";
  level: string;
  identity_type: string | null;
  identity_number: string | null;
  identity_country_id: string | null;
  identity_expiry_date: string | null;
  customer_id: string | null;
  customer_code: string | null;
  customer_status: string | null;
  identity_verified_at: string | null;
  reviewed_by: string | null;
  rejection_reason: string | null;
  risk_score: string | number | null;
  submitted_at: string | null;
  reviewed_at: string | null;
  created_at: string;
  updated_at: string;
  user?: {
    id: string;
    email: string | null;
    phone: string | null;
    full_name: string | null;
  } | null;
}

export interface CashWalletDTO {
  id: string;
  user_id: string;
  currency_id: string;
  currency_code: string;
  available_balance: string;
  reserved_balance: string;
  status: string;
  created_at: string;
  updated_at: string;
}

export interface CurrencyOption {
  id: string;
  code: string;
  name_en: string;
  name_ar: string;
  symbol: string | null;
}

export interface SupplierDTO {
  id: string;
  supplier_no: string;
  name_ar: string;
  name_en?: string | null;
  supplier_type: string;
  status: string;
  risk_level?: string | null;
  country_id?: string | null;
  city?: string | null;
  address?: string | null;
  contact_name?: string | null;
  contact_phone?: string | null;
  contact_email?: string | null;
  website?: string | null;
  registration_number?: string | null;
  tax_number?: string | null;
  license_number?: string | null;
  is_approved: boolean;
  is_blocked: boolean;
  is_lbma_certified?: boolean;
  payment_terms?: string | null;
  delivery_terms?: string | null;
  lead_time_days?: number | null;
  currency_id?: string | null;
  minimum_order_amount?: string | null;
  credit_limit?: string | null;
  rating?: string | null;
  notes?: string | null;
  approved_at?: string | null;
  blocked_reason?: string | null;
  created_at?: string;
  updated_at?: string;
}

export interface GoldWalletDTO {
  id: string;
  user_id: string;
  purity_id: string;
  purity_karat: string;
  purity_ratio: string;
  purity_name_ar?: string;
  karat?: number;
  available_grams: string;
  reserved_grams: string;
  status: string;
  created_at: string;
  updated_at: string;
}

export interface OrderDTO {
  id: string;
  order_no: string;
  user_id: string;
  customer_name: string | null;
  branch_id: string | null;
  order_type: string;
  status: string;
  currency_id: string | null;
  product_id: string | null;
  purity_id: string | null;
  requested_grams: string | null;
  requested_amount: string | null;
  price_per_gram: string | null;
  subtotal_amount: string | null;
  fee_amount: string;
  tax_amount: string;
  total_amount: string | null;
  price_snapshot: Record<string, any> | null;
  expires_at: string | null;
  executed_at: string | null;
  created_at: string;
  updated_at: string;
}

export interface TransferDTO {
  id: string;
  order_number: string;
  status: string;
  from_vault_id: string;
  from_vault_name: string | null;
  to_vault_id: string;
  to_vault_name: string | null;
  from_branch_id: string | null;
  to_branch_id: string | null;
  transfer_reason: string | null;
  priority: string;
  scheduled_at: string | null;
  dispatched_at: string | null;
  received_at: string | null;
  requested_by: string | null;
  approved_by: string | null;
  dispatched_by: string | null;
  received_by: string | null;
  total_bars: number;
  total_weight: string;
  notes: string | null;
  created_at: string;
  updated_at: string;
}

export interface DeliveryOrderDTO {
  id: string;
  order_number: string;
  client_user_id: string;
  client_name: string | null;
  delivery_branch_id: string;
  vault_id: string;
  bar_id: string | null;
  custody_id: string | null;
  transfer_order_id: string | null;
  delivery_type: string;
  status: string;
  grams: string;
  otp_code: string | null;
  otp_expires_at: string | null;
  otp_verified_at: string | null;
  identity_document_type: string | null;
  identity_document_number: string | null;
  identity_verified_at: string | null;
  identity_verified_by: string | null;
  prepared_by: string | null;
  prepared_at: string | null;
  handed_over_by: string | null;
  handed_over_at: string | null;
  completed_at: string | null;
  cancelled_at: string | null;
  cancellation_reason: string | null;
  notes: string | null;
  created_at: string;
  updated_at: string;
}

export interface VaultDTO {
  id: string;
  code: string;
  name: string;
  branch_id: string;
  vault_type: string;
  security_level: string;
  max_capacity_grams: string;
  current_hold_grams: string;
  status: string;
  created_at: string;
  updated_at: string;
}

export interface BarDTO {
  id: string;
  bar_code: string;
  serial_number: string | null;
  purity_id: string;
  weight_grams: string;
  status: string;
  current_vault_id: string | null;
  current_owner_type: string | null;
  created_at: string;
  updated_at: string;
}

export interface GoldPriceTickDTO {
  id: string;
  source_id: string;
  currency_id: string;
  purity_id: string;
  price_per_gram: string;
  price_per_unit: string | null;
  bid_price: string;
  ask_price: string;
  mid_price: string;
  captured_at: string;
}

export interface GoldPriceSourceDTO {
  id: string;
  code: string;
  name: string;
  source_type: string;
  base_currency_id: string;
  is_active: boolean;
}

export interface GoldSpreadRuleDTO {
  id: string;
  name: string;
  purity_id: string | null;
  buy_spread_percent: string;
  sell_spread_percent: string;
  is_active: boolean;
}

export interface AuditLogDTO {
  id: string;
  actor_user_id: string | null;
  action: string;
  entity_type: string;
  entity_id: string | null;
  old_values: Record<string, unknown> | null;
  new_values: Record<string, unknown> | null;
  ip_address: string | null;
  created_at: string;
}

export interface NotificationDTO {
  id: string;
  user_id: string | null;
  title: string | null;
  body: string;
  channel: string;
  status: string;
  sent_at: string | null;
  read_at: string | null;
  reference_type: string | null;
  reference_id: string | null;
  created_at: string;
}

export interface PaginatedResponse<T> {
  items: T[];
  total: number;
  page: number;
  page_size: number;
  pages: number;
}

export interface ApiResponse<T> {
  success: boolean;
  message?: string;
  data?: T;
  code?: string;
}

export interface ApiErrorResponse {
  detail: string;
  code: string;
  field_errors?: Record<string, string[]>;
  request_id?: string;
}

export interface ListParams {
  page?: number;
  page_size?: number;
  search?: string;
  sort_by?: string;
  sort_order?: "asc" | "desc";
  filters?: Record<string, string>;
}

export interface DashboardOverview {
  total_customers: number;
  active_customers: number;
  pending_kyc: number;
  total_cash_balance: string;
  total_gold_grams: string;
  total_orders_today: number;
  pending_orders: number;
  pending_redemptions: number;
  open_alerts: number;
  spot_price: string;
  spot_delta: string;
  spot_pct: string;
  buy_price: string;
  sell_price: string;
  spread_bps: string;
  price_source: string;
  price_updated_at: string;
  currency: string;
  price_gram_24k: number;
  price_gram_22k: number;
  price_gram_21k: number;
  price_gram_18k: number;
  silver_spot_price?: string;
  silver_bid_price?: string;
  silver_ask_price?: string;
  silver_delta?: string;
  silver_pct?: string;
  silver_price_source?: string;
  silver_price_updated_at?: string;
  silver_currency?: string;
  silver_price_gram?: number;
  silver_price_gram_925?: number;
  silver_price_gram_900?: number;
  cash_available?: string;
  cash_held?: string;
  gold_available?: string;
  gold_held?: string;
  recent_orders?: any[];
  recent_transfers?: any[];
  recent_redemptions?: any[];
  alerts?: any[];
}

export interface ActivityItem {
  id: string;
  type: "order" | "transfer" | "redemption" | "kyc" | "alert";
  title: string;
  description: string;
  status: string;
  timestamp: string;
  meta?: Record<string, unknown>;
}

export interface BranchDTO {
  id: string;
  code: string;
  name_ar: string;
  name_en: string | null;
  is_receiving_branch: boolean;
  is_delivery_branch: boolean;
  status: string;
  created_at: string;
}

export interface AdminUserDTO {
  id: string;
  email: string | null;
  phone: string | null;
  full_name_ar: string | null;
  full_name_en: string | null;
  user_type: string;
  status: string;
  department: string | null;
  job_title: string | null;
  is_admin_approved: boolean;
  must_change_password: boolean;
  roles: UserRoleInfo[];
  permissions: string[];
  created_at: string;
  updated_at: string;
}

export type {
  AdminRoleRead,
  AdminUserRead,
  CreateUserResponse,
  IpBlacklistCreatePayload,
  IpBlacklistRead,
  IpBlacklistUpdatePayload,
  PendingApprovalUser,
  RegeneratePasswordResponse,
  UserCreatePayload,
  UserUpdatePayload,
  UserWhitelistIpRead,
} from "./users";

export type {
  BuyOrderCreatePayload,
  BuyOrderRead,
  BuyOrderUpdatePayload,
  CombinedOrderRead,
  OrderItemRead,
  OrderPostingResult,
  OrderSide,
  OrderWorkflowAction,
  SellOrderCreatePayload,
  SellOrderRead,
  SellOrderUpdatePayload,
} from "./orders";

export type * from "./sell-orders";
export type * from "./buy-orders";
export type * from "./market-prices";

export type {
  ReceivingBatchCompleteFormValues,
  ReceivingBatchCompletePayload,
  ReceivingBatchCreatePayload,
  ReceivingBatchFormValues,
  ReceivingBatchRead,
  AssayResultCreatePayload,
  ReceivingOrderCreatePayload,
  ReceivingOrderItemCreatePayload,
  ReceivingOrderItemRead,
  ReceivingOrderStatusHistoryRead,
  ReceivingOrderRead,
  ReceivingOrderUpdatePayload,
  ReceivingOrderWorkflowAction,
  ReceivingPostingPreview,
  ReceivingPostingResult,
  ReceivingReceiptRead,
} from "./receiving";

export type * from "./purchase-orders";
export type * from "./supplier-shipments";
export type * from "./inbound-postings";
export type * from "./logistics";
