export type OrderStatus =
  | "pending"
  | "processing"
  | "shipped"
  | "delivered"
  | "cancelled"
  | "refunded";

export type PaymentStatus = "pending" | "paid" | "failed" | "refunded";

export type PaymentMethod =
  | "credit_card"
  | "debit_card"
  | "bank_transfer"
  | "cash_on_delivery"
  | "wallet";

export interface OrderItem {
  id: number;
  product_id: number;
  product_name: string;
  product_image?: string;
  image_url?: string;
  variant_id?: number;
  variant_name?: string;
  product_variant_id?: number;
  product_variant_name?: string;
  quantity: number;
  unit_price: number;
  total_price: number;
  discount_amount?: number;
  weight?: number;
  final_weight?: number;
}

export interface OrderAddress {
  id: number;
  type?: "billing" | "shipping";
  // legacy fields
  first_name?: string;
  last_name?: string;
  company?: string;
  address_line_1?: string;
  address_line_2?: string;
  city?: string;
  state?: string;
  postal_code?: string;
  country?: string;
  phone?: string;
  // new fields
  name?: string;
  address?: string;
  region?: string;
  plaque?: number;
  unit?: number;
  receiving_address?: string;
}

export interface OrderPayment {
  id: number;
  payment_method: PaymentMethod;
  payment_status: PaymentStatus;
  amount: number;
  transaction_id?: string;
  gateway_response?: Record<string, any>;
  paid_at?: string;
}

export interface OrderPaymentRecord {
  id: number;
  invoice_id: number;
  user_id: number;
  created_at: string;
  updated_at: string;
  payment_type: string;
  status: string;
  step?: string;
  amount: number;
  transaction_id?: string;
  image_urls?: string[];
}

export interface OrderCustomer {
  id: number;
  first_name: string;
  last_name: string;
  email: string;
  phone?: string;
}

export interface Order {
  order: {
    id: number;
    order_number: string;
    customer?: OrderCustomer;
    status: OrderStatus;
    items: OrderItem[];
    billing_address: OrderAddress;
    shipping_address: OrderAddress;
    payment?: OrderPayment;
    // new flat fields from API
    invoice_id?: number;
    user_id?: number;
    user?: {
      id: number;
      phone_number: string;
      first_name: string;
      last_name: string;
      email: string;
      national_code?: string;
      verified: boolean;
      avatar?: string;
      is_deleted?: boolean;
    };
    payment_status?: PaymentStatus;
    payments?: OrderPaymentRecord[];
    net_total?: number;
    vat_total?: number;
    shipping_total?: number;
    discount_total?: number;
    base_gold_price?: number;
    // legacy totals kept for compatibility
    subtotal?: number;
    tax_amount?: number;
    shipping_amount?: number;
    discount_amount?: number;
    total_amount?: number;
    final_total?: number;
    currency: string;
    notes?: string;
    tracking_number?: string;
    estimated_delivery?: string;
    shipping_method_id?: number;
    selected_delivery_slot?: string | {
      date?: string;
      from_hour?: number;
      to_hour?: number;
    };
    created_at: string;
    updated_at: string;
  };
}

export interface OrderFilters {
  // Pagination
  page?: number;
  limit?: number;
  offset?: number;

  // Filter Parameters
  user_id?: number;
  invoice_id?: number;
  status?: OrderStatus;
  payment_status?: PaymentStatus;
  customer_id?: number;
  order_number?: string;
  transaction_id?: string;
  discount_code?: string;

  // Amount Range Parameters
  min_total?: number;
  max_total?: number;
  min_amount?: number; // legacy support
  max_amount?: number; // legacy support

  // Date Range Parameters
  created_from?: string;
  created_to?: string;
  updated_from?: string;
  updated_to?: string;
  date_from?: string; // legacy support
  date_to?: string; // legacy support

  // Search Parameter
  search?: string;
}

export interface PaginatedOrdersResponse {
  orders: Order[];
  total: number;
  page: number;
  limit: number;
  total_pages: number;
}

export interface UpdateOrderStatusRequest {
  status: OrderStatus;
  notes?: string;
  tracking_number?: string;
  estimated_delivery?: string;
}

export interface OrderStats {
  total_orders_count: number;
  total_amount_of_sale: number;
  total_order_pending: number;
  order_avg: number;
}

export type Response<T> = {
  data: T;
  message?: string;
  success?: boolean;
};
