CREATE TABLE IF NOT EXISTS calendar_event (
  id BIGSERIAL PRIMARY KEY,
  title VARCHAR(255) NOT NULL,
  description TEXT,
  event_kind VARCHAR(30) NOT NULL DEFAULT 'personal',
  source_type VARCHAR(30) NOT NULL DEFAULT 'manual',
  source_uid VARCHAR(255),
  source_name VARCHAR(255),
  starts_at TIMESTAMPTZ NOT NULL,
  ends_at TIMESTAMPTZ,
  all_day BOOLEAN NOT NULL DEFAULT FALSE,
  location VARCHAR(255),
  status VARCHAR(30) NOT NULL DEFAULT 'geplant',
  priority VARCHAR(20) NOT NULL DEFAULT 'mittel',
  visibility conf_level NOT NULL DEFAULT 'intern',
  owner_user_id BIGINT REFERENCES app_user(id),
  committee_id BIGINT REFERENCES committee(id),
  created_by BIGINT REFERENCES app_user(id),
  updated_by BIGINT REFERENCES app_user(id),
  created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
  updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
  deleted_at TIMESTAMPTZ
);

CREATE INDEX IF NOT EXISTS idx_calendar_event_starts_at ON calendar_event(starts_at);
CREATE INDEX IF NOT EXISTS idx_calendar_event_owner_user_id ON calendar_event(owner_user_id);
CREATE INDEX IF NOT EXISTS idx_calendar_event_committee_id ON calendar_event(committee_id);
CREATE INDEX IF NOT EXISTS idx_calendar_event_source_type_uid ON calendar_event(source_type, source_uid);
