-- Phase: Checklisten für Aufträge (aus Templates instanziert, im Einsatz abhakbar)

CREATE TABLE IF NOT EXISTS order_check_item (
  id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
  order_id uuid NOT NULL REFERENCES "order"(id) ON DELETE CASCADE,
  sort int NOT NULL DEFAULT 0,
  label text NOT NULL,
  is_required boolean NOT NULL DEFAULT false,
  state text NOT NULL DEFAULT 'open' CHECK (state IN ('open','done','na')),
  done_by_user_id bigint REFERENCES app_user(id) ON DELETE SET NULL,
  done_at timestamptz,
  created_at timestamptz NOT NULL DEFAULT now(),
  updated_at timestamptz
);

CREATE INDEX IF NOT EXISTS idx_order_check_item_order ON order_check_item(order_id);
