-- Feedback-tabell för buggrapporter och önskemål (doc §2).
DO $$
BEGIN
  IF NOT EXISTS (SELECT 1 FROM pg_type WHERE typname = 'feedback_type') THEN
    CREATE TYPE feedback_type AS ENUM ('bug', 'onskemal');
  END IF;
  IF NOT EXISTS (SELECT 1 FROM pg_type WHERE typname = 'feedback_status') THEN
    CREATE TYPE feedback_status AS ENUM ('oppen', 'pagar', 'stangd');
  END IF;
END $$;

CREATE TABLE IF NOT EXISTS feedback (
  id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
  user_id uuid REFERENCES users(id) ON DELETE SET NULL,
  typ feedback_type NOT NULL,
  rubrik varchar(200) NOT NULL,
  text text NOT NULL,
  status feedback_status NOT NULL DEFAULT 'oppen',
  plattform varchar(16),
  app_version varchar(32),
  created_at timestamptz NOT NULL DEFAULT now()
);

CREATE INDEX IF NOT EXISTS feedback_status_created_idx
  ON feedback (status, created_at DESC);

CREATE INDEX IF NOT EXISTS feedback_user_idx
  ON feedback (user_id, created_at DESC);
