// ── User / Auth ──────────────────────────────────────────────────────────────

export type Role = 'customer' | 'admin';

export interface AuthUser {
  id: string;
  email: string;
  role: Role;
}

// ── Products ─────────────────────────────────────────────────────────────────

export interface ProductImage {
  id: string;
  url: string;
  altText?: string;
  position?: number;
  role?: 'main' | 'gallery';
}

export interface Product {
  id: string;
  name: string;
  slug: string;
  description?: string;
  state: string;
  channels: 'online' | 'offline' | 'all';
  featured: boolean;
  attributes: string[];
  seoTitle?: string;
  seoDescription?: string;
  images: ProductImage[];
  brandId?: string;
  categoryIds?: string[];
  brand?: { id: string; name: string; slug: string };
  imageUrl?: string;
  expirationDate?: string | null;
  createdAt?: string;
  updatedAt?: string;
}

export interface ProductVariant {
  id: string;
  productId: string;
  sku: string;
  ean: string | null;
  attributes: Record<string, unknown>;
}

export interface VariantPrice {
  variantId: string;
  netUnitAmountCents: number;
  offerCents: number | null;
  costCents: number | null;
  vatRate: 'general' | 'reduced' | 'super-reduced';
  currency: string;
}

export interface StockAvailability {
  available: boolean;
  availableQuantity: number;
}

export interface StockItem {
  id: string;
  variantId: string;
  available: number;
  reserved: number;
  sold: number;
  incoming: number;
  createdAt: string;
  updatedAt: string;
}

// ── Orders ────────────────────────────────────────────────────────────────────

export type OrderState =
  | 'PENDING'
  | 'AWAITING_PAYMENT'
  | 'PAID'
  | 'PROCESSING'
  | 'SHIPPED'
  | 'DELIVERED'
  | 'CANCELLED'
  | 'REFUNDED'
  | 'PARTIALLY_REFUNDED';

export interface OrderItem {
  id: string;
  productId: string;
  variantId: string;
  sku: string;
  ean: string | null;
  name: string;
  unitPriceCents: number;
  discountCents: number;
  taxCents: number;
  quantity: number;
  createdAt: string;
}

export interface Address {
  recipientName: string;
  street: string;
  city: string;
  postalCode: string;
  country: string;
}

export interface PaymentInfo {
  provider: string;
  status: string;
  amountCents: number;
  last4: string | null;
  createdAt: string;
}

export interface Order {
  id: string;
  userId: string;
  state: OrderState;
  currency: 'EUR';
  subtotalCents: number;
  discountCents: number;
  taxCents: number;
  totalCents: number;
  trackingNumber?: string | null;
  courier?: string | null;
  idempotencyKey: string | null;
  customerName?: string | null;
  customerPhone?: string | null;
  email?: string | null;
  phone?: string | null;
  items: OrderItem[];
  shippingAddress?: Address | null;
  billingAddress?: Address | null;
  payment?: PaymentInfo | null;
  createdAt: string;
  updatedAt: string;
}

export interface OrderHistoryEvent {
  id: string;
  eventType: 'STATE_CHANGE' | 'SHIPPING_UPDATE' | string;
  message: string;
  trackingNumber: string | null;
  actorEmail: string | null;
  createdAt: string;
}

export interface OrderSummary {
  id: string;
  userId: string;
  state: OrderState;
  totalCents: number;
  currency: 'EUR';
  itemCount: number;
  createdAt: string;
}

// ── Customers ─────────────────────────────────────────────────────────────────

export interface Customer {
  id: string;
  email: string;
  role: Role;
  displayName?: string;
  phone?: string;
  createdAt: string;
  updatedAt?: string;
}

export interface Address {
  id: string;
  userId: string;
  label?: string | null;
  recipientName: string;
  street: string;
  city: string;
  postalCode: string;
  country: string;
  isDefault?: boolean;
  createdAt?: string;
  updatedAt?: string;
}

// ── Categories & Brands ────────────────────────────────────────────────────────

export interface Category {
  id: string;
  parentId: string | null;
  name: string;
  slug: string;
  seoTitle?: string;
  seoDescription?: string;
  imageUrl?: string;
  description?: string;
  isParent?: boolean;
  emoji?: string;
  color?: string;
  bgColor?: string;
  textColor?: string;
  children?: Category[];
}

export interface Brand {
  id: string;
  name: string;
  slug: string;
  logoUrl?: string;
  seoTitle?: string;
  seoDescription?: string;
}

// ── API Errors ────────────────────────────────────────────────────────────────

export interface ApiErrorBody {
  statusCode: number;
  code: string;
  message: string;
}

export class ApiError extends Error {
  constructor(
    public readonly statusCode: number,
    public readonly code: string,
    message: string,
  ) {
    super(message);
    this.name = 'ApiError';
  }
}
