CREATE TABLE IF NOT EXISTS expenses (
  id TEXT PRIMARY KEY,
  item_number TEXT,
  name TEXT NOT NULL,
  sale_price NUMERIC(18,4) DEFAULT 0,
  raw JSONB NOT NULL DEFAULT '{}'::jsonb,
  created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
  updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
);

CREATE TABLE IF NOT EXISTS product_shape_definitions (
  id TEXT PRIMARY KEY,
  item_number TEXT,
  name TEXT NOT NULL,
  sale_price NUMERIC(18,4) DEFAULT 0,
  raw JSONB NOT NULL DEFAULT '{}'::jsonb,
  created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
  updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
);

CREATE TABLE IF NOT EXISTS sales_reps (
  id TEXT PRIMARY KEY,
  name TEXT NOT NULL,
  phone TEXT,
  region TEXT,
  commission_rate NUMERIC(18,4) DEFAULT 0,
  raw JSONB NOT NULL DEFAULT '{}'::jsonb,
  created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
  updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
);

CREATE TABLE IF NOT EXISTS managed_units (
  id TEXT PRIMARY KEY,
  unit_number TEXT,
  description TEXT NOT NULL,
  raw JSONB NOT NULL DEFAULT '{}'::jsonb,
  created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
  updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
);

CREATE INDEX IF NOT EXISTS idx_expenses_name ON expenses (name);
CREATE INDEX IF NOT EXISTS idx_product_shapes_name ON product_shape_definitions (name);
CREATE INDEX IF NOT EXISTS idx_sales_reps_name ON sales_reps (name);
CREATE INDEX IF NOT EXISTS idx_managed_units_description ON managed_units (description);
